How to calculate the total size of specific files, recursive, on Linux

I have a bunch of files scattered across folders in a layout, for example:

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 only for MP4 files. This means that it must be recursive in some way.

I looked at du and scrolled things up to grep , but couldn't figure out how to calculate only MP4 files no matter where they are.

If possible, should the readable total disk space be preferably in the UK?

Any ideas? Thanks

+6
source share
3 answers

For a single file size:

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

For shared disk space in GB:

 find . -name "*.mp4" -print0 | du -sb --files0-from=- | awk '{ total += $1} END { print total/1024/1024/1024 }' 
+5
source

This sums the entire size of mp4 files in bytes:

 find ./ -name "*.mp4" -printf "%s\n" | paste -sd+ | bc 
+2
source

You can simply do:

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

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

+2
source

Source: https://habr.com/ru/post/983426/


All Articles