11

I've got a bunch of files scattered across folders in a layout, e.g.:

dir1/somefile.gif
dir1/another.mp4
dir2/video/filename.mp4
dir2/some.file
dir2/blahblah.mp4

And I need to find the total disk space used for the MP4 files only. This means it's gotta be recursive somehow.

I've looked at du and fiddling with piping things to grep but can't seem to figure out how to calculate just the MP4 files no matter where they are.

A human readable total disk space output is a must too, preferably in GB, if possible?

Any ideas? Thanks

nooblag
  • 339
  • 1
  • 5
  • 13

3 Answers3

10

For individual file size:

find . -name "*.mp4" -print0 | du -sh --files0-from=- 

For total disk space in GB:

find . -name "*.mp4" -print0 | du -sb --files0-from=-  | awk '{ total += $1} END { print total/1024/1024/1024 }'
Marco Guerri
  • 932
  • 5
  • 11
  • I was also about to suggest the first command. Since you pass `-s` it should already summarize, but hey it lists sizes for each individual file if the `--files0-from` is passed. This looks like a bug, isn't it? – hek2mgl Mar 06 '15 at 13:57
  • 1
    From man page: `-s, display only a total for each argument`. Here each file passed on standard input is a different argument. `-s` is useful with directories. – Marco Guerri Mar 06 '15 at 14:02
  • Try to remove the `print0` from find and remove the --files0-from option. Now it will calculate the total while we are still passing multiple file arguments – hek2mgl Mar 06 '15 at 14:07
  • 1
    It calculates the total of the current directory, completely ignoring `find` output. Not what was asked. – Marco Guerri Mar 06 '15 at 14:10
  • 1
    Damn, you are right.. `du` simply ignores `stdin` per default. Thanks for showing this – hek2mgl Mar 06 '15 at 14:13
  • Marco, check by answer – Ján Stibila Mar 06 '15 at 14:14
  • 1
    Note: the `--files0-from` is GNU specific, so for example on OS X or BSD you don't have it... – clt60 Mar 06 '15 at 14:40
  • `du -ch` prints the grand total in the end of the output – Martin Pecka Aug 23 '18 at 09:26
4

You can simply do :

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

The -exec option of find command executes a simple command with {} as the file found by find. du -b displays the size of the file in bytes. The awk command initializes a variable at 0 and get the size of each file to display the total at the end of the command.

3

This will sum all mp4 files size in bytes:

find ./ -name "*.mp4" -printf "%s\n" | paste -sd+ | bc
Ján Stibila
  • 619
  • 3
  • 12