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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 3

Another option is available here :

du -sm .[!.]* *

Method 4

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

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

Method 5

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x