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.

vidhan
  • 665
  • 1
    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

5 Answers5

35

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
jimmij
  • 47,140
  • 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
  • @vidhan See the edit once again, should be enough to understand I believe. – jimmij Feb 22 '15 at 13:52
  • 7
    .??* 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
  • Note: [^.] and [!.] are equivalent (source: man bash). – Evgeni Sergeev Nov 24 '17 at 11:14
  • 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 '20 at 18:20
  • Lol. Way to insult half the regular users of the site. – B Layer Aug 20 '20 at 16:58
  • This pattern has many unhandled edge cases, but is good enough for most cases and a bit easier to understand and type: {.,}[^.]* or {*,.[^.]*} – xeruf Dec 14 '20 at 11:14
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 .[!.]* *
Fábio
  • 249
  • 3
  • 7
1

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

find maybehere -maxdepth 1 -exec du -sh {} \;
k3a
  • 231
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*/.*/

SailorCire
  • 2,503