37

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.

2
  • 1
    Try for just hidden maybehere*/.* and append to above for all
    – Costas
    Feb 22, 2015 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, 2015 at 13:09

5 Answers 5

36

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
10
  • 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, 2015 at 13:12
  • @vidhan see the edit, and look look at man bash for "Brace Expansion" chapter.
    – jimmij
    Feb 22, 2015 at 13:18
  • 7
    .??* fails to match .a, .b... .[^.]* fails to match ..foo. Feb 22, 2015 at 13:53
  • 4
    .foo matches both .[^.]* and .??*. You want {.[!.],..?,}*. Feb 22, 2015 at 14:25
  • 2
    How has no one who writes code for Unix ever bothered to create a command line switch that handles this? I mean, it's a really simple concept...take all files and folders that are actually files and folders, not pointers to the parent directly, and move them over there? Instead we have to do this complex brace expansion stuff that no one should have to learn just to move some files. Typical *nix, written by and for programmers who don't get much Vitamin D and live in a bubble.
    – Mageician
    Jun 22, 2020 at 18:20
19

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.

11

Another option is available here :

du -sm .[!.]* *
3
  • Why would you want to skip filenames starting with ..?
    – Kusalananda
    Aug 10, 2018 at 6:53
  • 2
    Presumably, because they don't want to list everything in ../ and below. Aug 10, 2018 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. Oct 26, 2019 at 15:15
1

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

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

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

6
  • can you please see the question once again @SailorCire I have edited it a bit
    – vidhan
    Feb 22, 2015 at 13:03
  • @vidhan so you only want to work on hidden inside one directory? Is that correct?
    – SailorCire
    Feb 22, 2015 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, 2015 at 13:06
  • @vidhan du -b picks up both hidden and non hidden.
    – SailorCire
    Feb 22, 2015 at 13:12
  • no :( unfortunately @SailorCire
    – vidhan
    Feb 22, 2015 at 13:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .