197

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 ./*.

Hugo
  • 144
Dr_Bunsen
  • 4,713
  • 4
  • 28
  • 40

5 Answers5

354

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
don.joey
  • 28,662
  • 1
    Thanks, you are a genius. I am not a master in bash, what doe [!.] do? – Dr_Bunsen Oct 22 '13 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. – Tikhon Jelvis May 21 '14 at 17:46
  • 17
    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. – Stéphane Chazelas Jun 22 '15 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 '17 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 '21 at 08:23
78

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

yurikoles
  • 1,262
10

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.

dobey
  • 40,982
  • 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 '13 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 '14 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 -- * .*. – Stéphane Chazelas Jun 22 '15 at 20:11
3

This also works to show hidden files and dirs:

du -sch $(ls -A .) | sort -h
ki9
  • 522
  • 2
    This command doesn't work for files or directories that contain a space character in their names. – karel Oct 07 '21 at 06:22
1

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
moo
  • 878