9

Running du -shc * in the top directory gives 110G for a particular folder, whereas running the same command inside that folder gives a total size of 11G. How is that possible?

Platform Details: OS: CentOS 6.6 x86_64 Drive type: solid-state Volume type: RAID 6 array RAID Controller: LSI MegaRAID SAS Filesystem: ext3

8

Most probably you have hidden files in the folder. The point is that glob * selects only files and folders that do not start with .. So, if they do they are not passed to du command. On the other hand from top directory you get size of the directory as a whole, including dot files.

To match all files in given folder, including hidden ones try (with bash)

du -shc -- {.[!.],..?,}*

or set option dotglob so that * matches hidden files too:

shopt -s dotglob
du -shc -- *
4
  • Boy, am I embarrassed. I didn't think of that. I wish there were a simpler shell syntax for including hidden files in the expansion. Especially with commands like du, where you usually want to see everything in the folder. Thanks.
    – orodbhen
    Jul 23 '15 at 22:23
  • @orodbhen This glob is a little bit nerdy to take care of any "unusual" possibilities like files which start from two dots ..abc or which start from hyphen -abc. Most of the times you could just do du -shc * .??* or in shorter form du -shc {.??,}*. You cannot get rid of ? to not take parent directory .. into account. You can also set dotglob option in bash, so that it includes hidden files by default (I will add that to the answer).
    – jimmij
    Jul 23 '15 at 22:30
  • @orodbhen In zsh: *(D) (or make the inclusion of dot files the default with setopt glob_dots). Jul 23 '15 at 23:01
  • Been using Linux command line for 12 years, never even heard of the shopt command!
    – HDave
    Jun 13 '16 at 12:59

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.