43

I want to estimate the amount of disk space used by a directory using the following command.

du -sh dir_name

which does not calculate the hidden directories. In the man page of du there is no info regarding it. How to calculate the amount of disk space used by the directories including the hidden files.

11 Answers 11

21

Actually it does, here is the proof:

mkdir .test
echo "hi" > .test/appo
du -a
4       ./.test/appo
8       ./.test
12      .

The -a option is used to explicitly show which files were counted.

Are you using du *?

| improve this answer | |
  • 6
    +1 for ... you maybe using "du *" ? – sehe Oct 3 '11 at 11:34
  • 2
    I am not using "du *". I forgot to mention in the question that i need the total summary summary. so if i use both -a and -s as in du -as i get an error message saying "du: cannot both summarize and show all entries". This is one issue. The other thing is that i even with du -a i get to see only the first level files. i.e unable to see files inside hidden directories. – Talespin_Kit Oct 3 '11 at 11:50
  • 1
    I just added -a to show all files being counted, just don't add that flag, it only affect the display, not the totals. I showed you a counter-example that instead hidden directories are counted. – stivlo Oct 3 '11 at 11:56
  • 1
    now i found the problem. The problem was the hidden directory was a symbolic link, So i had to use "du -Lsh dir_name". Thanks. – Talespin_Kit Oct 3 '11 at 12:47
  • 1
    ah, I see, glad that you found it, cheers. – stivlo Oct 3 '11 at 13:03
101

This command shows you the summarized size of hidden directories

du -hs .[^.]*

| improve this answer | |
  • 3
    This is exactly what I was looking for, thanks :) – Nikana Reklawyks Dec 7 '16 at 0:54
  • this is the correct solution – 3pns Apr 15 '19 at 6:24
  • it would be awesome if you could explain a bit the magic behind it. it would help to form an answer which includes both hidden and non hidden files and directories. – kumetix Nov 5 '19 at 14:21
  • Glob expression: Take the 1st dot but don't take the 2nd dot => don't go to your parent. Find your (hiding / playing peek-a-boo) children and report their size back to me. – Ashfaq May 30 at 4:51
22

The correct command is : du -hs $(ls -A)

$ du -hs $(ls -A)
0   test
0   .test

du -hs .* *, as mentioned in another answer, is not correct if you want to list all files and subdirectories (including hidden ones).

Example :

$ touch test
$ touch .test
$ echo *
test
$ echo .* *
. .. .test test
$ du -hs .* *
4,0K    .
1,8G    ..

Why does du behave like this? Because you use -s that summarize the result and that all files and subdirectories are children of . so du -hs does not list them!

| improve this answer | |
12

FYI, for estimating the size occupied by various directories, its much better to use ncdu

You can navigate in the ncurses GUI between various directories and it will show the size of each directories. If I am using du, I would have to execute du command for each directory I want to check for which can be cumbersome. You can sort the directories according to the size occupied too in the ncurses GUI.

| improve this answer | |
  • 1
    Please edit your answer to indicate why you believe ncdu to be "much better". The linked page doesn't really say anything about it other than that it's an ncurses-based version of du. Aside from a fancier display method, there's no obvious indication of any functional difference. – Dave Sherohman Oct 4 '11 at 10:15
  • I think the ability to dynamically adjust your query is a huge benefit. I can run ncdu on ~, learn which directories are using the most space and then dive directly into those. It also gives you the d binding to delete directories, so you can clean up directly from the interface. – user88974 Mar 17 '17 at 10:06
5

It does and it does not. Example:

In the home directory: (only one user exist)

du -sh /home/*
2.6G    /home/user

in the user directory: (huge difference between the sums)

du -sh *
61M     bin
2.0M    dump-20130124104823.tar.gz
651M    public_html
472K    twitter-2.0.0.gem
11M     wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2

and the reason is:

du -sh /home/user/.rvm/
1.9G    /home/user/.rvm/

du will calculate hidden directories while descending into subdirectories, but in the current directory the * simply does not match to .directory_name pattern so the current directory hidden elementes will be omitted.

It took me some time to figure out, and as shadyabhi recommends it would have been obvious if i had used ncdu.

| improve this answer | |
2
du -ahd1 | sort -hr | head -10

following is the description of -d option in du --help:

-d, --max-depth=N print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize

| improve this answer | |
  • Although this may help to solve the problem, it doesn't explain why and/or how it addresses the issue. Providing this additional context would significantly improve its long-term educational value. Please edit your answer to add explanation, including what limitations and assumptions apply. Thanks. – fixer1234 Nov 26 '18 at 12:31
1

The correct command is : du -hs .* *

| improve this answer | |
  • 1
    Some additional information would make this an acceptable answer. – Ramhound Sep 23 '14 at 12:07
1

find -maxdepth 1 -exec du -sh "{}" \; | sort -h

This gives you:

  • Size of hidden files/directories
  • Size of non-hidden files/directories
  • Grand total size of the current directory

It also sorts the output to make it easy to see what is the largest. I also made this an alias in my ~/.bash_aliases file.

alias big='find -maxdepth 1 -exec du -sh "{}" \; | sort -h'
alias sbig='sudo find -maxdepth 1 -exec du -sh "{}" \; | sort -h'

Now I can just execute big to find the biggest directories in a directory and sbig when I need sudo permissions.

| improve this answer | |
1

This command will help you to check the disk usage get into the directory and execute the following.
"du -sch .[!.]* * | sort -h"

Here is the demo of how to calculate a disk usage of a particular directory. I have created a directory called Du_sh and create two files (one hidden 10 MB and normal 20 MB) inside.

nikhil@debian:~$ mkdir Du_sh nikhil@debian:~$ cd Du_sh/ nikhil@debian:~/Du_sh$ dd if=/dev/zero of=.10MB bs=1024 count=10240 10240+0 records in 10240+0 records out 10485760 bytes (10 MB, 10 MiB) copied, 0.0299941 s, 350 MB/s nikhil@debian:~/Du_sh$ nikhil@debian:~/Du_sh$ dd if=/dev/zero of=20MB bs=2048 count=10240 10240+0 records in 10240+0 records out 20971520 bytes (21 MB, 20 MiB) copied, 0.0342258 s, 613 MB/s nikhil@debian:~/Du_sh$ du -sch .[!.]* * 10M .10MB 20M 20MB 30M total nikhil@debian:~/Du_sh$ du -sch .[!.]* * | sort -h 10M .10MB 20M 20MB 30M total nikhil@debian:~/Du_sh$

| improve this answer | |
1

Most things I tried (including other answers) didn't work in one way or another, so here is my (currently) final solution for using du:

ls -A | while read file; do du -sh "$file"& done | sort -h;

  • ls -A All files including hidden, without . and ..
  • while read file; do du -sh "$file"& done Run the du command on each entry from ls -A in parallel. Had no problem yet regarding too much parallelism, but you can either replace & by ; making it synchronous or add [ $( jobs | wc -l ) -ge $( nproc ) ] && wait before done
  • sort -h Sort by size

While I'm at it: I always have a function named "dus" added to my profile to keep this function at hand:

dus () { ls -A | while read file; do du -sh "$file"& done | sort -h; }

| improve this answer | |
0

I found this comment by Stéphane Chazelas worth a separate answer for zsh users.

On zsh, use:

du -sch *(D) | sort -h

The solutions with .[^.]* * or .[!.]* * do not work on zsh if a folder contains only .* files or * files. It will throw a no matches found error. So you cannot use one command universally.

On bash, it would say No such file or directory for one type but still continue with the other type.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.