In a large directory on my Ubuntu system (>140000 files and > 200 subdirectories), I know that somewhere there are two files with names too long to copy to a Windows (NTFS) folder. I tried it and got two error messages, but I didn't pay attention to what subfolders the files were in.
How can I find the two files with the longest names?
I guess the @steeldriver's solution is a better choice however here is my alternative solution, you can use a combinations of commands to find exactly two (or more) longest file names.
the output would be like:
Here a real example:
Notes
find
gives us a list of all files within that directory like:using
awk
we add file length to start of each line (it's exactly the file length not the path's):finally we sort it based on file length and get the first two line using
head
.Based on comments, what you really need in this case is a list of all the files whose names are longer than some maximum number of characters - and fortunately that's relatively easy using a
find
regex:For such a large number of files and directories, you probably want to avoid sorting - instead let's just keep a running record of the longest and second longest filenames, and their full pathnames:
or with GNU awk (which supports 2D arrays)