How to match the hidden files inside the given directories

for example

If I give the below command it's not giving the result of the hidden files,

 du -b maybehere*/*

how to achieve this simple using a single command instead of using

du -b maybehere*/.* maybehere*/*

as I need to type maybehere twice.

  • Try for just hidden maybehere*/.* and append to above for all – Costas Feb 22 '15 at 12:49
  • 1
    Your edit makes a new question with additional restrictions, that makes the Q into a moving target, possible invalidating the answer(s) already given. That is bad manners, just ask a new question if you have one. – Anthon Feb 22 '15 at 13:09

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
  • what is the use of the { } I have no idea :( can you give me a direction to get more info about it – vidhan Feb 22 '15 at 13:12
  • @vidhan see the edit, and look look at man bash for "Brace Expansion" chapter. – jimmij Feb 22 '15 at 13:18
  • would you mind giving me more explanation for the above ans little bit more clearly explaining how every thing works as I am new with these things @jimmij – vidhan Feb 22 '15 at 13:27
  • 6
    .??* fails to match .a, .b... .[^.]* fails to match ..foo. – Stéphane Chazelas Feb 22 '15 at 13:53
  • 4
    .foo matches both .[^.]* and .??*. You want {.[!.],..?,}*. – Stéphane Chazelas Feb 22 '15 at 14:25

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 .[!.]* *
  • Why would you want to skip filenames starting with ..? – Kusalananda Aug 10 '18 at 6:53
  • 2
    Presumably, because they don't want to list everything in ../ and below. – Shadur Aug 10 '18 at 7:35
  • This does not count files like e.g. ..myfile; add ..?* for that. But +1 for using the proper glob syntax for negative matches. – Pedro Gimeno Oct 26 at 15:15

If you want to just list hidden directories or operate on hidden directories then as Costas said you can use

du -b maybehere*/.*

This will allow you to operate on hidden files and directories. If you want only hidden directories then you can specify that with

du -b maybehere*/.*/

  • can you please see the question once again @SailorCire I have edited it a bit – vidhan Feb 22 '15 at 13:03
  • @vidhan so you only want to work on hidden inside one directory? Is that correct? – SailorCire Feb 22 '15 at 13:04
  • no I want to match both hidden and non-hidden files inside the one directory instead of using both du maybehere*/.* and maybehere*/* one of hidden and another for non hidden – vidhan Feb 22 '15 at 13:06
  • @vidhan du -b picks up both hidden and non hidden. – SailorCire Feb 22 '15 at 13:12
  • no :( unfortunately @SailorCire – vidhan Feb 22 '15 at 13:20

While not shell directly, you can use find with limited depth like this

find maybehere -maxdepth 1 -exec du -sh {} \;

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.