Archive for the ‘find’ tag
How big are those files in that directory on my *nix system?
Ah, the command line. It can be awesome and a pain in the ass at the same time. In a GUI file manager, you can just highlight the files you want and get a total size of the selected files. On a *nix command line? Not so simple.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb | cut -f1 | xargs echo | sed "s/ /+/g" | bc
That does it
Breaking this monster down gives us the following.
First cd to the directory you want to check in.
cd Pictures
List the files. find finds files.
find . -iname '*.jpg'
./Picture3 001.jpg
./Picture3 002.jpg
...
./Picture4 099.jpg
List the files, excaping spaces. sed allows you to perform actions against the string being passed to it.
find . -iname '*.jpg' | sed "s/ /\\\\ /g"
./Picture4\ 001.jpg
./Picture4\ 002.jpg
./Picture4\ 003.jpg
...
./Picture4\ 099.jpg
List the files, getting file size. xargs allows you to take a line of input and execute it. du is used to get the disk usage size.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb
696009 ./Picture4 001.jpg
675879 ./Picture4 002.jpg
666862 ./Picture4 003.jpg
...
658225 ./Picture4 099.jpg
Cut the file size out of the listings. cut -f1 gives us the first field using ‘space’ as a delimeter.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb | cut -f1
700696
702453
703594
...
696009
Take all of our lines of file sizes and concatenate them to a single line.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb | cut -f1 | xargs echo
700696 702453 703594 ... 696009
Replace the spaces in our single line with ‘+’.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb | cut -f1 | xargs echo | sed "s/ /+/g"
700696+702453+703594+ ... +696009
Now take the output above and use bc to calculate it. bc is a simple command line calculator.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb | cut -f1 | xargs echo | sed "s/ /+/g" | bc
54631548
How big are all the jpg files in my pictures directory? 54,631,548 bytes.
So, command line? Pain in the ass to get something so simple, but pretty awesome that there are so many different, tiny programs for *nix that can work together to get you the results you want.