146

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.

16 Answers 16

47

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 *?

6
  • 28
    +1 for ... you maybe using "du *" ?
    – sehe
    Commented Oct 3, 2011 at 11:34
  • 4
    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. Commented Oct 3, 2011 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
    Commented Oct 3, 2011 at 11:56
  • 4
    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. Commented Oct 3, 2011 at 12:47
  • 2
    ah, I see, glad that you found it, cheers.
    – stivlo
    Commented Oct 3, 2011 at 13:03
295

This command shows you the summarized size of hidden directories using a regular expression:

du -hs .[^.]*
8
  • 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
    Commented Nov 5, 2019 at 14:21
  • 6
    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
    Commented May 30, 2020 at 4:51
  • 24
    It seems that du -hs .[^.]* * will include both dot files/directories and non-hidden directories together. I don't know if there are cases where this is not correct.
    – steveb
    Commented Jan 27, 2021 at 23:32
  • 2
    If you are crazy enough to have filesnames like "..tralala", you have to include them too du -hs ..[^.]* .[^.]* *
    – Hrobky
    Commented Jul 9, 2021 at 15:25
  • 1
    RegEx is still like a magic black box to me. Doesn't make any sense at all to me. But when used properly, it's pretty useful. Thanks!!
    – Mmm
    Commented Jan 19, 2023 at 18:53
85

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!

9
  • 16
    For file names with spaces, this command will produce an error
    – Kariem
    Commented Jan 22, 2021 at 13:04
  • 2
    This gives du for both hidden (.*) and non-hidden. Additionally, it ignores the parent. Accurate command. Commented Oct 5, 2021 at 7:58
  • 3
    The only good answer here. Commented May 12, 2022 at 9:08
  • 2
    I suggested an alternative ls -A-based solution, but this time using xargs so as to make sure filenames with spaces are properly handled (see my other answer below)
    – lajarre
    Commented Aug 29, 2022 at 12:27
  • 1
    Does this work for non-pwd? It seems to give me an error if I use (ls -A /other/directory/here) saying it cannot access.
    – omsrisagar
    Commented Mar 17, 2023 at 15:07
19

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.

2
  • 2
    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. Commented Oct 4, 2011 at 10:15
  • 2
    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
    Commented Mar 17, 2017 at 10:06
14
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

2
  • 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
    Commented Nov 26, 2018 at 12:31
  • @fixer1234 That is obvious, -s is forbidden to use with -a, but -d1 is not. Commented Jun 18 at 6:55
9

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.

8

A solution using ls -A to list files and xargs to manipulate output:

ls -A | xargs -I {} du -sh {}

This solution makes sure that filenames with spaces are correctly handled.

1
  • 2
    Use -s instead of -sh to ensure "560K" doesn't sort above "2.0G".
    – Noumenon
    Commented May 24, 2023 at 20:37
4

You can use the following command:

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.

2

This command will help you to check the disk usage, go to your directory and execute the following:

du -sch .[!.]* * | sort -h

Here is a demo of how to calculate disk usage of a particular directory.

I have created a directory called Du_sh and created two files inside of it (one hidden [10 MB] and one normal [20 MB]).

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$
0
2

The correct command is:

du -hs .* *

From man pages:

   -h, --human-readable
          print sizes in human readable format (e.g., 1K 234M 2G)

   -s, --summarize
          display only a total for each argument

You can read more about du command here.

2
  • 1
    Some additional information would make this an acceptable answer.
    – Ramhound
    Commented Sep 23, 2014 at 12:07
  • For me du * .* -sh works instead.
    – tejasvi
    Commented Jan 29, 2022 at 9:05
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 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; }
1

The above answer by Guillaume Missonnier, du -hs $(ls -A), does not work if you want sizes of files in a directory other than pwd e.g., du -hs $(ls -A /other/dir/) gives du: cannot access error. This happens because the output of ls command is not valid under pwd.

I figured a simple way to fix this, first cd to that dir, execute the command and then cd back to pwd. Assume you are in a non-home directory and would like to get sizes of files in home directory (different from pwd). In that case, use:

cd ~; du -sh $(ls -A) | sort -h; cd -

A better way would be to create an alias/function to take the /other/dir as argument, so it will be easy to call as: myfunc /other/dir.

1
  • Thanks for the suggestion, I have now added a link to the referenced answer.
    – omsrisagar
    Commented Mar 17, 2023 at 17:54
0

du accepts paths. With this command you can check the disk usage of a different path:

du -sh /path/to/directory/{.[^.],}*

If you want the result sorted (bigger files/dirs first):

du -sh /path/to/directory/{.[^.],}* | sort -hr
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 return the following error for one type but still continue with the other type.

No such file or directory
0

From my tests the safest approach is this...

du -sm -- * .[^.]*

TIP: Prefer megabytes (-m option).

NOTE: Occasional side effect du: cannot access '.[^.]*': No such file or directory. When there is no hidden files/folders? 🤔

Thanks! 😎

PLUS

Sortly list by megabytes the 15 largest files/folders...

du -sm -- * .[^.]* | sort -nr | head -15

FURTHER

It seems to me that an approach with ls -A might be safer as this command is suitable for listing files (-A including hiddem ones). However, we have discrepancies in the outputs and problems with single quotes even using xargs to try to handle the situation.

SAMPLES

[eduardolac-pc eduardolac]# du -sm -- * .[^.]* | sort -nr | head -15
553002  Data1
140123  Data0
37718   .PlayOnLinux
17349   .local
13424   .cache
8654    .config
7931    .var
3423    .pyenv
2833    .npm
2051    .stremio-server
2001    .Phoenicis
1896    .wine
1479    .conda
1451    .nvm
1402    .vscode
[eduardolac-pc eduardolac]# ls -A | xargs -I {} du -sm {} | sort -nr | head -15
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
553002  Data1
140123  Data0
37718   .PlayOnLinux
17349   .local
13416   .cache
8654    .config
2833    .npm
2001    .Phoenicis
1479    .conda
1451    .nvm
962     .goldendict
932     GoldenDict_Glossaries
803     .mozilla
785     .m2
570     .googleearth

[Ref(s).: https://serverfault.com/a/1116989/276753 , https://superuser.com/a/1739605/195840 ]

-1

You may use braces expansion with wildcarding and human readable sorting:

du -hd2 {.,[^.]}* | sort -h

d2 is a depth option

0

You must log in to answer this question.

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