1

How can one stat an entire directory using a single command line starting with bash -c?

This returns all files except for those prefixed with .

 bash -c "stat /path/todir/**"

This returns all files starting with . including . & .. which should be excluded.

bash -c "stat /path/todir/.*"

This works but not under bash -c and requires two lines

shopt -s dotglob
stat /path/todir/**

This too

stat /path/todir/!(.|..)
  • stat /path/** would be identical to stat /path/* if the extglob shell option was not set. You don't set this for your bash -c shell, so I don't know how . and .. could be stat'ed. – Kusalananda Mar 4 at 7:53
  • I meant globstar in my previous comment... not extglob – Kusalananda Mar 4 at 8:00
2

The extended glob options are visible only to the current shell and not to the sub-shells launched. You need to set them inside the sub-shell also for the glob option to be available. Also ** does a recursive descent only if another extended option globstar is set. For your requirement of the current directory alone, you can just use *

bash -c 'shopt -s dotglob; stat /path/todir/*'

Notice the use of single quotes around the entire shell shell command list. It is much more safe in a way you can avoid unnecessary variable expansion (passing a literal string) and use quoted strings with much ease.

If you have control over the part outside the '..' you can set the extended shell option in the invocation itself as

bash -O dotglob -c 'stat /path/todir/*'

That said, if an option to use external utilities like find is available, you could just do below which just excludes the . (current directory name) and includes all the files in the current directory and passes it stat in one shot.

find . ! -path . -exec stat {} +
1
bash -c -O extglob 'stat /path/todir/!(.|..)'

-O extglob enables additional pattern matching operators, including !(pattern-list) for negation.

[-+]O [shopt_option]
         shopt_option is one of the shell options accepted by the shopt builtin  (see  
         SHELL  BUILTIN  COMMANDS  below). If shopt_option  is  present, -O sets the 
         value of that option; +O unsets it.

If the extglob shell option is enabled using the shopt builtin, several extended pattern matching
operators  are recognized.  In the following description, a pattern-list is a list of one or more
patterns separated by a |.  Composite patterns may be formed using one or more of  the  following
sub-patterns:

      ?(pattern-list)
             Matches zero or one occurrence of the given patterns
      *(pattern-list)
             Matches zero or more occurrences of the given patterns
      +(pattern-list)
             Matches one or more occurrences of the given patterns
      @(pattern-list)
             Matches one of the given patterns
      !(pattern-list)
             Matches anything except one of the given patterns

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.