I am trying to figure out how to sort a list of files by name and size.
How do I sort by file name and size using "du -a" and not show directories?
Using "du -a"
1 ./locatedFiles
0 ./testDir/j.smith.c
0 ./testDir/j.smith
1 ./testDir/sampleFunc/arrays
2 ./testDir/sampleFunc
0 ./testDir/j.smith.txt
0 ./testDir/testing
0 ./testDir/test2
0 ./testDir/test3
0 ./testDir/test1
0 ./testDir/first/j.smith
0 ./testDir/first/test
1 ./testDir/first
1 ./testDir/second
1 ./testDir/third
6 ./testDir
How can I list all files without directories, add files sizes, and sort by files name first, then by size?
Thanks for your help
You can use this:
find -type f -printf "%f %s %p\n"|sort
Explanation:
Then run through sort which sorts in the order given above (i.e. file name, then file size, then file path). The output would be something like this (part of the output shown):
...
XKBstr.h 18278 ./extensions/XKBstr.h
XlibConf.h 1567 ./XlibConf.h
Xlib.h 99600 ./Xlib.h
Xlibint.h 38897 ./Xlibint.h
Xlocale.h 1643 ./Xlocale.h
xlogo11 219 ./bitmaps/xlogo11
....
Hope this helps
You can use the sort
command
$ find -type f -printf $'%s\t%f\n' | sort -k2,2 -k1,1n
sort by second field(name), then first field(size) numerically.
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