Is there a way in bash to sort my files from a directory down in depth order, for example first print the files in the present directory, then the files in the sub directory or sub directories and so forth, always in perspective to their depth.
Is there a way in bash to sort my files from a directory down in depth order, for example first print the files in the present directory, then the files in the sub directory or sub directories and so forth, always in perspective to their depth.
Use find's "-printf" feature in combination with sort. See for yourself:
find . -printf "%d %p\n"|sort -n
It generates a depth-sorted list (displaying the depth in the first column, file path in the second). This prints in my current dir:
0 .
1 ./bin
1 ./log
1 ./templates
2 ./bin/cc_env
3 ./files/test/mail.txt
If you want to strip the first column, we can use perl:
find . -printf "%d %p\n"|sort -n|perl -pe 's/^\d+\s//;'
and off you go. The perl filter will remove all leading numbers. In case you want to omit directories themselves, use the '-type f' parameter:
find . -type f -printf "%d %p\n"|sort -n|perl -pe 's/^\d+\s//;'
Hint: Study the find manpage for more printf %d-like tricks.