How to get total disk usage by several files in linux

Have you ever had a need to get the disk space used by several files and had to type in something like this:

du -hs file1 file2 file3

then get a calculator and add all those numbers up?

Well, with this command, you won't have to do that anymore...

# To get your total size in kilobytes
du -k file1 file2 file3 | awk '{total=total+$1} END {print total}'
# To get your total size in Megabytes
du -k file1 file2 file3 | awk '{total=total+$1} END {print total/1024"MB"}'
# To get your total size in Gigabytes
du -k file1 file2 file3 | awk '{total=total+$1} END {print total/(1024 * 1024)"GB"}'

Please NOTE: When you use commands above, and will specify folders, you will get folder size twice: one time for each file and another for the actual folder itself!

Another way to get the same information is by using the -c option of du like so:

# To get your total size in kilobytes
du -kc file1 file2 file3
# To get your total size in human readable form
du -hsc file1 file2 file3

If you want to get just the resulting number without anything else, just append | awk 'END {print $1}' to your command!

du -hsc * | awk 'END {print $1}'

Comments