191

I was looking through my system with du -sch ./* to find the big useless files I may have stockpiled with no reason, when I found this:

$ du -sch ./*
du: cannot read directory ‘./drbunsen/.gvfs’: Permission denied
du: cannot read directory ‘./drbunsen/.cache/dconf’: Permission denied
18G ./drbunsen
18G total

$ cd drbunsen/
$ du -sch ./*
601M ./Desktop
20K ./Documents
598M ./Downloads
4.0K ./flash
4.0K ./Music
8.0M ./Pictures
4.0K ./Public
4.0K ./Templates
4.0K ./Ubuntu One
8.0K ./Videos
11G ./VirtualBox VMs
6.9M ./workspace
12G total

How do I make hidden files visible? du -sch ./.* gives the same result as du -sch ./*.

2
  • 1
    du -hs .[^.]* * for all hidden files, excluding . and .. which other answers seem to be missing. Apr 12, 2015 at 14:12
  • Not enough rep here to answer this highly active question, so a quick comment instead. If under bash, you can "fix" this by using the dotglob shell option. I use a subshell here so that it does not affect globally for other commands => (shopt -s dotglob; du -shc *) Nov 24, 2020 at 15:44

5 Answers 5

347

Use

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

in your home folder.

Alternatively, the command I use most frequently is

ncdu

Easy to install if needed:

sudo apt-get install ncdu
5
  • 1
    Thanks, you are a genius. I am not a master in bash, what doe [!.] do?
    – Dr_Bunsen
    Oct 22, 2013 at 14:43
  • 11
    @Dr_Bunsen: It's a glob that lists all the files that start with a single .. Here's a neat trick: if you don't know what a glob-looking thing does, try running echo .[!.]* or whatever. The shell will then expand the glob and pass it into echo, printing out the list of files that results. May 21, 2014 at 17:46
  • 16
    That fails to list files named like ..foo and may cause problems with files whose name starts with -. It could also fail if the arg list is too long. du -ahd1 would have none of those problems. Jun 22, 2015 at 20:08
  • 1
    Irrelevant to the original question, but relevant to day to day usage: It might be useful to include 'c' switch to display the total and to reorder the options a bit to make this command a little bit easier to remember: du -cksh .[!.]* * |sort -h.
    – Daddy32
    Feb 21, 2017 at 13:06
  • Nice glob. But this does only work if you actually have hidden files on the top level. Hidden files inside directories will still not be counted.And thnak you for ncdu.
    – Hinz
    Feb 19, 2021 at 8:23
75

I have same question in coreutils mailing list, because it was hard to me to remember this weird command by @don.joey. And Bob Proulx proposed better, more natural command: du -ahd1 | sort -h

If you want to list all of the files in the current directory then either use '.' or don't give it any file arguments at all. Also you may want to use the -d, --max-depth=N option.

Try this:

du -hd1

2
  • 8
    Add the -a option for it to be equivalent. Jun 22, 2015 at 20:16
  • 8
    This should be the accepted answer because it's simpler and easier to remember and understand.
    – Bastien
    Sep 10, 2015 at 8:47
9

When you run that same command inside the directory, it is not including the hidden files which start with . in the count. If you have Steam for example installed, it default to installing games under ~/.local/share/Steam/ and it itself is installed there as well.

Under bash you apparently need to run du -sch .[!.]* * as it does not properly expand the .* glob. Under zsh or other shells, du -sch * .* should work, as .* should be expanded to include the list of all hidden files in the current directory.

3
  • 2
    OK. There is apparently a problem with bash then, as it doesn't seem to properly expand .*. But du -sch * .* does do the correct thing under zsh. Under bash, du -sch .* seems to count . but doesn't expand to show files individually.
    – dobey
    Oct 22, 2013 at 13:39
  • For me .* also expands to ../* and therefore du goes out of the directory that I want to see its size! I had to use ./.[^.]* instead.
    – Ari
    Sep 19, 2014 at 22:58
  • It's zsh and all the shells derived from the Forsyth shell (nowadays that's mostly only pdksh and its derivatives) that do the right thing and don't expand . and .. here. All the other Bourne-like shells do (bash, Bourne, AT&T ksh, dash, yash...). In zsh, you'd use du -sch -- *(D) though, not du -sch -- * .*. Jun 22, 2015 at 20:11
2

This also works to show hidden files and dirs:

du -sch $(ls -A .) | sort -h
1
  • 1
    This command doesn't work for files or directories that contain a space character in their names.
    – karel
    Oct 7, 2021 at 6:22
0

I don't think the du utility has a command line switch to process hidden files by default.

One way of achieving this is to use the find utility to find the hidden files that you are interested in and then run the du utility on each entry:

find ./ -maxdepth 1 -name '.*' -exec  du -hs {} \;

This gives you additional flexibility as you may only be interested in directories:

find ./ -maxdepth 1 -type d -name '.*' -exec  du -hs {} \;

Or you may want to see all hidden files and directories deep to the current working directory:

find ./ -name '.*' -exec  du -hs {} \;

It's not much harder to sort the output, so you can easily identify large hidden files that could help you save disk space:

{ find ./ -maxdepth 1 -iname '.*' -exec  du -hs \{\} \; ;} | sort -h

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