I am trying to get the total size of files satisfying a
ls $(find -maxdepth 2 -type f)
However, this kind of invocation of
ls does not produce the total size as well.
Believe it or not you can do this with
The gist of that post is a command such as this:
This will list the size of all the files along with a summary total.
NOTE: This solution requires that
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.
These could be dealt with by introducing more
If your version of
To also handle spaces in filenames, the delimiting symbol that
If you expect to find many files which burst the number of maximum arguments, xargs will split these into multiple
Another approach : we just need the file size, and don't care about the file names, so we can get rid of any "weird" file names such as "names with CR in them, names with spaces, etc" :
The trick is:
1) we print each file's "-ls" output, FOLLOWED by a "\000" caracter (on the next line, but it's not a problem, see step 2)
This is very portable (don't need "-print0", etc)
This is a simple way that handles whatever odd file names that can be found:
If there is a really large number of files under the current directory, you might have more than one total line displayed. There might be also unwanted total lines if some file names contain an isolated "
If you're only going to compute the size of maximum two directory levels, why not call
This makes the shell expand the two levels of directories to a list of names and passes them as arguments to