Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the total size of certain files only, recursive, in linux

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

like image 397
nooblag Avatar asked Mar 06 '15 13:03

nooblag


People also ask

How do you recursive find a file in Linux?

Finding Files Recursively in Linux The find command does not need flags to search the files recursively in the current directory. You only need to define the main directory and the file name using the –name option. This command will search the file within the main directory and all subdirectories.


2 Answers

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 }'
like image 126
Marco Guerri Avatar answered Oct 13 '22 19:10

Marco Guerri


This will sum all mp4 files size in bytes:

find ./ -name "*.mp4" -printf "%s\n" | paste -sd+ | bc
like image 34
Ján Stibila Avatar answered Oct 13 '22 18:10

Ján Stibila