1163

Is there a simple command to display the total aggregate size (disk usage) of all files in a directory (folder)?

I have tried these, and they don't do what I want:

  • ls -l, which only displays the size of the individual files in a directory, nor
  • df -h, which only displays the free and used space on my disks.
1
  • 1
    Friendly reminder that it may make nearly zero sense on CoW filesystem like BTRFS. Jun 1, 2019 at 18:46

14 Answers 14

1907

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
5
  • 148
    I use du -sh or DOOSH as a way to remember it (NOTE: the command is the same, just the organization of commandline flags for memory purposes) Aug 5, 2010 at 18:56
  • 4
    There is a useful option to du called the --apparent-size. It can be used to find the actual size of a file or directory (as opposed to its footprint on the disk) eg, a text file with just 4 characters will occupy about 6 bytes, but will still show up as taking up ~4K in a regular du -sh output. However, if you pass the --apparent-size option, the output will be 6. man du says: --apparent-size print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in (‘sparse’) files, internal fragmentation, indirect blocks Jun 3, 2016 at 4:45
  • 4
    This works for OS X too! Thanks, I was really looking for a way to clear up files, both on my local machine, and my server, but automated methods seemed not to work. So, I ran du -hs * and went into the largest directory and found out which files were so large... This is such a good method, and the best part is you don't have to install anything! Definitely deserved my upvote
    – Dev
    Sep 14, 2016 at 17:57
  • @BandaMuhammadAlHelal I think there are two reasons: rounding (du has somewhat peculiar rounding, showing no decimals if the value has more than one digit in the chosen unit), and the classical 1024 vs. 1000 prefix issue. du has an option -B (or --block-size) to change the units in which it displays values, or you could use -b instead of -h to get the "raw" value in bytes. May 23, 2017 at 9:12
  • Sounds like asking a question 'Du hast'
    – Bobz
    Feb 6 at 11:15
238

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

Edit: You can restrict the search to files in the same filesystem, use -x:

ncdu -x /

this will ignore other mounted filesystems outside where the root filesystem is hosted.

A slightly optimization is using the '-q' option to increase the time between screen updates. The options can be combined:

ncdu -xq /var

As always, reading man ncdu is recommended.

3
  • 5
    ncdu is awesome! After installing it, just do this ncdu /. You will very quickly find the biggest files on the system. Also press h while inside ncdu's console interface. It has very useful shortcuts Dec 12, 2017 at 16:34
  • 2
    cool tool. use the man page to find all the cool shortcuts like a which shows the apparent size of all the files. That's the biggest problem I have with du it takes too much typing to see the apparent size, which is usually what I really want to know.
    – gMale
    Jan 5, 2021 at 21:15
  • To see information about the shortcuts just press ? within ncdu (not h).
    – MEMark
    Mar 15, 2023 at 7:37
100

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 *
1
  • 3
    Use du -hsc * | sort -hr if you want to get a sorted list of folders Dec 16, 2020 at 13:31
25

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
22

Enjoy!

du foldername

More information on that command here

1
  • 3
    Yep. I prefer adding -h to make "Human readable" folder size. du -h foldername
    – ibnɘꟻ
    May 14, 2020 at 2:22
17

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.)
8

To see the sizes of all files and directories, use

du -had1 dir/

(maybe like "do you had 1")

  • du: device/disk usage
  • -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.

7

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
3
  • The find-ls-awk will return a wrong value for large folders #1. For newer awk you can add --bignum or -M ; if that is not an option use find . -ls | tr -s ' '|cut -d' ' -f 7| paste -sd+ |bc #2.
    – goozez
    Jun 29, 2016 at 20:25
  • 1
    If powers of 2 being used is a problem, there's the --si option: "like -h, but use powers of 1000 not 1024"
    – muru
    Dec 15, 2017 at 3:25
  • found here the explanation of why an rcloned directory had significantly different total sizes, between the original usb disk (512K blocks/sectors/whatever) and the destination directory on an internal disk (4k). Used tree to get number of sub-directories and files and validated they were the same. Jan 18 at 21:13
7

You can use the tool Dust:

PS C:\git> dust
   0B       ┌── templates           │                                      █ │   0%
   0B     ┌─┴ git-core              │                                      █ │   0%
   0B   ┌─┴ share                   │                                      █ │   0%
  76B   ├── readme.md               │                                      █ │   0%
 156K   │   ┌── less.exe            │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█ │   2%
 2.7M   │   ├── git-remote-https.exe│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒█████████████████ │  42%
 3.6M   │   ├── git.exe             │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██████████████████████ │  56%
 6.5M   │ ┌─┴ git-core              │███████████████████████████████████████ │ 100%
 6.5M   ├─┴ libexec                 │███████████████████████████████████████ │ 100%
 6.5M ┌─┴ .                         │███████████████████████████████████████ │ 100%

My example is from Windows, but Linux and Apple are also supported:

https://github.com/bootandy/dust

2
  • 2
    I like how it uses a single screen to show biggest subdirs.
    – jfs
    Nov 7, 2020 at 4:51
  • cool use of a CLI bar chart.
    – qwr
    Jul 20, 2022 at 18:06
3

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.

3

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:

lll sample output.png


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:

lll sample output 2.png

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.

2

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
4
  • -s will not show directory sizes
    – qwr
    Feb 22, 2021 at 6:50
  • @qwr you are not correct. i just tested : prnt.sc/103jxmq
    – Shiv Singh
    Feb 22, 2021 at 9:09
  • that's not a directory!
    – qwr
    Feb 22, 2021 at 9:13
  • @qwr use du -hs for directory : example prnt.sc/103oxza
    – Shiv Singh
    Feb 22, 2021 at 11:46
2

du /foldername is the standard command to know the size of a folder. It is best practice to find the options by reading the man page:

man du

You should read the man page (available online) before you use the command.

0

One can use 7z (sudo apt-get install p7zip-full) to determine the total apparent size of a directory from the command line. Example:

7z -mx=0 a temp16584.zip /path/to/directory

It will show the total size of a directory (as well as the number of files):

(base) user@server:~$ 7z -mx=0 a temp16584.zip /path/to/directory

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=C.UTF-8,Utf16=on,HugeFiles=on,64 bits,8 CPUs Intel(R) Xeon(R) Platinum 8275CL CPU @ 3.00GHz (50657),ASM,AES-NI)

Scanning the drive:
7829 folders, 684576 files, 6914280912739 bytes (6440 GiB)                     

Creating archive: temp16584.zip

Items to compress: 692405

To get both the actual and apparent total size of a directory, one may use ncdu (sudo apt-get install ncdu):

enter image description here

Keyboard shortcut in ncdu to toggle between showing disk usage and showing apparent size: a. Same folders as above but now showing the apparent sizes for each folder:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .