How to find and sort a directory by the longest file path

directoryfilenamesosxsort

I'm on OS X, trying to connect Microsoft OneDrive, which has a hard path limit. I need to find the longest paths in a directory.

Best Answer

The following command will give you the character count of every regular file (remove the -type f for all types of files including directories) underneath the directory you execute it in, and sort them so the longest ones are output last:

find . -type f -print|awk '{print length($0), $0}' | sort -n

It assumes file and directory names don't contain newline characters.

Related Question