Size of all files of a certain extension in a directory tree

You're looking for pipes (|). They are a way of connecting multiple commands and passing the output of one command as input to another. In this case, you want to pass all the file names you find as input to du (which calculates size). However, because du expects file names and the results of find are just a list of text (yes, the text consists of file names, but du can't know that, all it sees is text), you need to use something like xargs which will take each line of text, treat it as a file name and pass that to du. Putting all this together, we get:

find . -name "*.o" | xargs du -sch
  • you should always quote the patterns you give to find (as I did above: "*.o"). If you don't, the shell will expand the *.o to the names of any matching files in the current directory. It worked in this case only because you had no matching files.

  • The -sch flags for du are documented in man du:

    -c, --total
          produce a grand total
    -h, --human-readable
          print sizes in human readable format (e.g., 1K 234M 2G)
    -s, --summarize
          display only a total for each argument
    

Note, however, that this will fail for filenames containing whitespace. This is almost certainly not going to be an issue for object files, but in the future, if you also need to deal with spaces, use:

find . -name "*.o" -print0 | xargs -0 du -sch

The -print0 makes find print NULL-separated lines and the -0 makes xargs take such lines as input.

Alternatively, you can have find print the sizes itself and then sum them:

find . -name "*.o" -printf '%s\n' | awk '{c+=$1}END{print c}'

This will also get around the problem mentioned by @Serg in the comments where there are too many arguments and the command is broken into separate commands.


If you're using bash (you probably are), there's a simpler way:

shopt -s globstar 
du -sch **/*.o

The shopt globstar command makes ** match all files and or more subdirectories. After enabling it, **/*.o will match all files (and directories) whose name ends in .o, so we can pass that directly to du.

Note that, unlike the find approach, this won't match hidden files (those whose name starts with a .). To match those as well, do:

shopt -s dotglob globstar
du -sch **/*.o

Use -exec flag to run du command with ; ( meaning per each file)

find . -name "*.o" -exec du -b {} \; | awk '{total+=$1}END{print total}' 

Sample output:

$ find . -name "*.txt"  -exec du -b {} \; | awk '{total+=$1}END{print total,"bytes" }'                                     
find: ‘./.cache/gvfs-burn’: Permission denied
find: ‘./.cache/unity’: Permission denied
852690242 bytes

find is recursive - meaning that it walks through all subdirectories. If you just want to get total of all *.o files in the current directory, just do

du -b -c *.o

Tags:

Command Line