How to determine the total size of a directory from the terminal?

The command du “summarizes disk usage of each FILE, recursively for directories,” e.g.,

du -hs /path/to/directory
  • -h is to get the numbers “human readable”, e.g. get 140M instead of 143260 (size in KBytes)
  • -s is for summary (otherwise you’ll get not only the size of the folder but also for everything in the folder separately)

As you’re using -h you can sort the human readable values using

du -h | sort -h

The -h flag on sort will consider “Human Readable” size values.


If want to avoid recursively listing all files and directories, you can supply the --max-depth parameter to limit how many items are displayed. Most commonly, --max-depth=1

du -h --max-depth=1 /path/to/directory

How to determine the total size of a directory from the terminal?

Below is what I am using to print total, folder, and file size:

$ du -sch /home/vivek/* | sort -rh

Details

 ------------------------------------------------------------
   -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
 -------------------------------------------------------------
   -h, --human-numeric-sort
          compare human readable numbers (e.g., 2K 1G)
   -r, --reverse
          reverse the result of comparisons

Output

 70M    total
 69M    /home/vivek/Downloads/gatling-charts-highcharts-bundle-2.2.2/lib
992K    /home/vivek/Downloads/gatling-charts-highcharts-bundle-2.2.2/results
292K    /home/vivek/Downloads/gatling-charts-highcharts-bundle-2.2.2/target
 52K    /home/vivek/Downloads/gatling-charts-highcharts-bundle-2.2.2/user-files

Answer #3:

Recently I found a great, ncurses based interactive tool, that quickly gives you an overview about directory sizes. Searched for that kind of tool for years.

  • quickly drilldown through file hierarchy
  • you can delete e.g. huge temporary files from inside the tool
  • extremely fast

Think of it as baobab for the command line:

apt-get install ncdu

Answer #4:

This finds the size recursively and puts it next to each folder name, along with total size at the bottom, all in the human format

du -hsc *

Answer #5:

tree is another useful command for this job:

Just install it via sudo apt-get install tree and type the following:

tree --du -h /path/to/directory
...
...

33.7M used in 0 directories, 25 files

From man tree:

-h    Print  the size of each file but in a more human readable way, e.g. appending a size letter for kilo‐
      bytes (K), megabytes (M), gigabytes (G), terabytes (T), petabytes (P) and exabytes (E).

--du  For each directory report its size as the accumulation of sizes of all its files and  sub-directories
      (and their files, and so on). The total amount of used space is also given in the final report (like
      the 'du -c' command.)

Answer #6:

The answers have made it obvious that du is the tool to find the total size of a directory. However, there are a couple of factors to consider:

  • Occasionally, du output can be misleading because it reports the space allocated by the filesystem, which may be different from the sum of the sizes of the individual files. Typically the filesystem will allocate 4096 bytes for a file even if you stored just one character in it!
  • Output differences due to power of 2 and power of 10 units. The -h switch to du divides the number of bytes by 2^10 (1024), 2^20 (1048576) etc to give a human readable output. Many people might be more habituated to seeing powers of 10 (e.g. 1K = 1000, 1M = 1000000) and be surprised by the result.

To find the total sum of sizes of all files in a directory, in bytes, do:

find <dir> -ls | awk '{sum += $7} END {print sum}'

Example:

$ du -s -B 1
255729664

$ find .  -ls | awk '{sum += $7} END {print sum}'
249008169

How to determine the total size of a directory from the command line?

I’m conditioned to the ll command which is aliased to ls -alF. It is just missing a file count and size of files at the bottom. I played with du and tree but could not get the totals I needed. So I created lll to do that for me.

In your ~/.bashrc place the following:

lll () {
    ls -alF "$@"
    arr=($(ls -alF "$@" | awk '{TOTAL+=$5} END {print NR, TOTAL}'))
    printf " \33[1;31m ${arr[0]}\33[m line(s).  "
    printf "Total size: \33[1;31m ${arr[1]}\33[m\n"
#    printf "Total size: \33[1;31m $(BytesToHuman <<< ${arr[1]})\33[m\n"
}

Save the file and resource it using . ~/.bashrc (or you can restart your terminal).


Sample output

The nice thing about ll output is it’s colors. This is maintained with lll but lost when using find or du:


TL;DR

A bonus function you can add to ~/.bashrc is called BytesToHuman(). This does what most console users would expect converting large numbers to MiB, GiB, etc:

function BytesToHuman() {

    # https://unix.stackexchange.com/questions/44040/a-standard-tool-to-convert-a-byte-count-into-human-kib-mib-etc-like-du-ls1/259254#259254

    read StdIn

    b=${StdIn:-0}; d=''; s=0; S=(Bytes {K,M,G,T,E,P,Y,Z}iB)
    while ((b > 1024)); do
        d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
        b=$((b / 1024))
        let s++
    done
    echo "$b$d ${S[$s]}"

} # BytesToHuman ()

Next flip the comment between two lines in lll () function to look like this:

#    printf "Total size: \33[1;31m ${arr[1]}\33[m\n"
    printf "Total size: \33[1;31m $(BytesToHuman <<< ${arr[1]})\33[m\n"

Now your output looks like this:

As always don’t forget to re-source with . ~/.bashrc whenever making changes. (Or restart the terminal of course)

PS – Two weeks in self-quarantine finally gave me time to work on this five-year-old goal.

Answer #8:

For only the directory size in a readable format, use the below:

du -hs directoryname

This probably isn’t in the correct section, but from the command line, you could try:

ls -sh filename

The -s is size, and the -h is human readable.

Use -l to show on ls list, like below:

ls -shl

Answer #9:

To see the sizes of all files and directories, use

du -had1 dir/

(maybe like “do you had 1”)

  • -h: human readable sizes
  • -a: show files, not just directories
  • -d1: show totals only at depth 1, i.e. the current directory’s contents

For the current directory, the directory argument can be left off.

du -sh dir/* has the same effect but doesn’t show hidden files and directories due to shell globbing.

Answer #10:

If your desired directory has many sub-directories then, use the following:

$ cd ~/your/target/directory
$ du -csh 

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

which would then produce a overall total of the memory usage by all files/folders in the current directory.

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →