I am trying to get the total size of files satisfying a find
, e.g.:
ls $(find -maxdepth 2 -type f)
However, this kind of invocation of ls
does not produce the total size as well.
bashfindls
I am trying to get the total size of files satisfying a find
, e.g.:
ls $(find -maxdepth 2 -type f)
However, this kind of invocation of ls
does not produce the total size as well.
Best Answer
Believe it or not you can do this with
find
anddu
. I used a similar technique that I wrote up on my blog a while a go. That article is titled: [one-liner]: Calculating Disk Space Usage for a List of Files Using du under Linux.The gist of that post is a command such as this:
Example
This will list the size of all the files along with a summary total.
NOTE: This solution requires that
du
support the--files0-from=
switch which is a GNU switch, to my knowledge.excerpt from du man page
Also this method suffers from not being able to deal with special characters in file names, such as spaces and non-printables.
Examples
These could be dealt with by introducing more
tr .. ..
commands to substitute them with alternative characters. However there is a better way, if you have access to GNU'sfind
.Improvements
If your version of
find
offers the--print0
switch then you can use this incantation which deals with files that have spaces and/or special characters that aren't printable.