How to obtain a list of all the files recursively without listing the directories to which the files belong and sort the list of files alphabetically?

I only managed to list the directories themselves with ls -a or the files sorted by directory with ls -aR

Asked By: Nuno Fonseca

||

You can use the find command’s -printf with the %f format specifier to get just the files’ basenames. From man find:

          %f     Print  the basename; the file's name with any leading di‐
                 rectories removed (only the last element).   For  /,  the
                 result is `/'.

So for example

find . -type f -printf '%fn' | sort

or (to handle filenames that contain newlines)

find . -type f -printf '%f' | sort -z | xargs -r0 -n1

To omit files in . itself, add -mindepth 2. Note that find matches hidden files by default, if that is why you added the -a flag to your ls command.


Alternatively using zsh

print -rC1 -- **/*(.:t:on)

To include hidden files in this case, set the globdots option (setopt globdots), and to omit files at the . level change the glob to */**/*.

Answered By: steeldriver
Categories: Answers Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.