SnapOverflow

SnapOverflow Logo SnapOverflow Logo

SnapOverflow Navigation

  • Home
  • Server
  • Ubuntu

Mobile menu

Close
  • Home
  • System Administrators
    • Hot Questions
    • New Questions
    • Tags
  • Ubuntu
    • Hot Questions
    • New Questions
    • Tags
  • Help
Home / ubuntu / Questions / 1224
Accepted
David Barry
David Barry
Asked: 2010-08-06 10:20:23 +0800 CST2010-08-06 10:20:23 +0800 CST 2010-08-06 10:20:23 +0800 CST

How do I determine the total size of a directory (folder) from the command line?

  • 772

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.
filesystem command-line
  • 13 13 Answers
  • 1446713 Views

13 Answers

  • Voted
  1. Best Answer
    Marcel Stimberg
    2010-08-06T10:27:35+08:002010-08-06T10:27:35+08:00

    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
    
    • 1765
  2. geekQ
    2012-10-11T23:24:38+08:002012-10-11T23:24:38+08:00

    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
    
    • 221
  3. Brad
    2014-12-20T08:32:21+08:002014-12-20T08:32:21+08:00

    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 *
    
    • 84
  4. myusuf3
    2010-08-06T10:22:53+08:002010-08-06T10:22:53+08:00

    Enjoy!

    du foldername
    

    More information on that command here

    • 20
  5. vivekyad4v
    2016-10-07T06:05:49+08:002016-10-07T06:05:49+08:00

    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
    
    • 19
  6. αғsнιη
    2015-01-28T04:37:49+08:002015-01-28T04:37:49+08:00

    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.)
    
    • 16
  7. pdp
    2016-02-05T10:56:45+08:002016-02-05T10:56:45+08:00

    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
    
    • 5
  8. Zombo
    2017-03-28T03:48:15+08:002017-03-28T03:48:15+08:00

    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

    • 4
  9. qwr
    2021-02-22T22:44:26+08:002021-02-22T22:44:26+08:00

    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.

    • 3
  10. WinEunuuchs2Unix
    2021-03-04T13:45:02+08:002021-03-04T13:45:02+08:00

    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.

    • 3

Sidebar

Stats

  • Questions 681965
  • Answers 980273
  • Best Answers 280204
  • Users 287326
  • Popular
  • Answers
  • Marko Smith

    How to add a directory to the PATH?

    • 17 Answers
  • Marko Smith

    How do I install .run files?

    • 7 Answers
  • Marko Smith

    How to list all installed packages

    • 24 Answers
  • Marko Smith

    How do I get the CPU temperature?

    • 21 Answers
  • Marko Smith

    Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

    • 25 Answers
  • Marko Smith

    How can I add a user as a new sudoer using the command line?

    • 7 Answers
  • Marko Smith

    Change folder permissions and ownership

    • 9 Answers
  • Marko Smith

    How do you restart Apache?

    • 13 Answers
  • Marko Smith

    How can I uninstall software?

    • 11 Answers
  • Marko Smith

    How can PPAs be removed?

    • 26 Answers
  • Martin Hope
    justingrif How to add a directory to the PATH? 2009-07-23 12:42:23 +0800 CST
  • Martin Hope
    NES How to enable or disable services? 2010-12-30 13:03:32 +0800 CST
  • Martin Hope
    Ivan How to list all installed packages 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra Unable to lock the administration directory (/var/lib/dpkg/) is another process using it? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    Olivier Lalonde How to keep processes running after ending ssh session? 2010-10-22 04:09:13 +0800 CST
  • Martin Hope
    David B How can I add a user as a new sudoer using the command line? 2010-10-16 04:02:45 +0800 CST
  • Martin Hope
    Hans How do I remove old kernel versions to clean up the boot menu? 2010-08-21 19:37:01 +0800 CST
  • Martin Hope
    David Barry How do I determine the total size of a directory (folder) from the command line? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher "The following packages have been kept back:" Why and how do I solve it? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford How can PPAs be removed? 2010-07-30 01:09:42 +0800 CST

Related Questions

Trending Tags

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • Home
  • Questions
    • Hot Questions
    • New Questions
  • Tags
  • Help

Footer

SnapOverflow

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Help

© 2022 SOF-TR. All Rights Reserve