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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












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/!(.|..)









share|improve this question






















  • 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
















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/!(.|..)









share|improve this question






















  • 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














1












1








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/!(.|..)









share|improve this question














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/!(.|..)






bash shell-script shell wildcards






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 4 at 7:42









TrevTheDevTrevTheDev

1134




1134












  • 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


















  • 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

















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






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






I meant globstar in my previous comment... not extglob

– Kusalananda
Mar 4 at 8:00











2 Answers
2






active

oldest

votes


















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 +





share|improve this answer
































    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





    share|improve this answer























      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504211%2fhow-can-one-stat-an-entire-directory-using-a-single-command-line-starting-with-b%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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 +





      share|improve this answer





























        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 +





        share|improve this answer



























          2












          2








          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 +





          share|improve this answer















          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 +






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 4 at 8:43









          Kusalananda

          139k17259429




          139k17259429










          answered Mar 4 at 7:48









          InianInian

          5,2051529




          5,2051529























              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





              share|improve this answer



























                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





                share|improve this answer

























                  1












                  1








                  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





                  share|improve this answer













                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 4 at 8:25









                  FreddyFreddy

                  1,444210




                  1,444210



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504211%2fhow-can-one-stat-an-entire-directory-using-a-single-command-line-starting-with-b%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown






                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay