du command does not parse hidden directories

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.

13 Answers

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

5

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

du -hs .[^.]*
6

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!

3

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

1

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.

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.

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

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; }

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

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

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

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

d2 is a depth option

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like