How to match * with hidden files inside a directory

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.

Asked By: vidhan

||

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

Answered By: SailorCire

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
Answered By: jimmij

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.

Answered By: Stéphane Chazelas

Another option is available here :

du -sm .[!.]* *
Answered By: Fábio

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

find maybehere -maxdepth 1 -exec du -sh {} ;
Answered By: k3a
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.