I am trying to use the find command to find all files 'M*' from my working directory and display results in directory order.
Instead it keeps displaying results in sorted order which causes some deeper directories to be listed first because they are alphabetically in order.
$ find -name 'M*'
./MyFourth
./s/MyFirst
./s/v/b/MyThird
./s/v/MySecond
I would like it to be in this order:
./MyFourth
./s/MyFirst
./s/v/MySecond
./s/v/b/MyThird
Thanks for your help
If I understand correctly what you mean by "directory order", this should help:
find -name 'M*' -printf '%p\t%d\n' | sort -n -k2 | cut -f 1
It prints the files sorted by their depth in the directory tree.
$ find . -name 'M*' | awk -F/ '{print NF,$0}' | sort -k1,1n -k2 | cut -d' ' -f 2-
./MyFourth
./s/MyFirst
./s/v/MySecond
./s/v/b/MyThird
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With