How to match * with hidden files inside a directory

Take advantage of the brace expansion:

du -b maybehere*/{*,.[^.],.??*}

or alternatively

du -b maybehere*/{,.[^.],..?}*

The logic behind this is probably not obvious, so here is explanation:

  • * matches all non-hidden files
  • .[^.] matches files which names started with single dot followed by not a dot; that are only 2 character filenames in the first form.
  • .??* matches hidden files which are at least 3 character long
  • ..?* like above, but second character must be a dot

The whole point is to exclude hard links to current and parent directory (. and ..), but include all normal files in such a way that each of them will be counted only once!

For example the simplest would be to just write

du -b maybehere*/{.,}*

It means that that the list contains a dot . and "nothing" (nothing is between , and closing }), thus all hidden files (which start from a dot) and all non-hidden files (which start from "nothing") would match. The problem is that this would also match . and .., and this is most probably not what you want, so we have to exclude it somehow.


Final word about brace expansion.

Brace expansion is a mechanism by which you can include more files/strings/whatever to the commandline by writing fewer characters. The syntax is {word1,word2,...}, i.e. it is a list of comma separated strings which starts from { and end with }. bash manual gives a very basic and at the same time very common example of usage:

$ echo a{b,c,d}e
abe ace ade

Since you're already using GNU specific syntax (-b):

du -abd1 maybehere*/

That way, it's du that lists the files in the maybehere* directories (and it doesn't exclude dot files). -d1 limits the reporting of disk usage to one level down (including non-directories with -a).

Otherwise, for globs to include hidden files (except . and ..), each shell has its own syntax:

  • zsh:

    du -b maybehere*/*(D)
    
  • ksh93:

    (FIGNORE='@(.|..)'; du -b maybehere*/*)
    
  • bash:

    (shopt -s dotglob; du -b maybehere*/*)
    
  • tcsh:

    (set globdot; du -b maybehere*/*)
    
  • yash:

    (set -o dot-glob; du -b maybehere*/*)
    

    though beware it includes . and .. on systems that include them in the result of readdir() which makes it hardly usable.


Another option is available here :

du -sm .[!.]* *