Jay Taylor's notes

back to listing index

bash - What does the double-asterisk (**) wildcard mean? - Stack Overflow

[web search]
Original source (stackoverflow.com)
Tags: bash asterisk shell-scripting globbing stackoverflow.com
Clipped on: 2024-10-15

    1. Image (Asset 1/27) alt= Matches any string, including the null string. When the 'globstar' shell option is enabled, and '*' is used in a filename expansion context, two adjacent '*'s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a '/', two adjacent '*'s will match only directories and subdirectories.

      In recent versions of bash the 'globstar' shell option is disabled by default. Enabled via:

      shopt -s globstar
      

      I believe zsh also supports this syntax.

      It's important to keep in mind that wildcards are expanded by the shell, not by the ls command. If you type ls **, or ls *.txt, the ls command itself never sees the * characters; it only sees an expanded list of files matching the pattern, just as if you had typed the entire list on the command line.

      Image (Asset 2/27) alt=current dir anywhere anywhere
      under x/o anywhere
      under any o only directly
      under any o *.md **/*.md x/o/**/*.md **/o/**/*.md **/o/*.md f.js f.md ✅ ✅ x x/f.js x/f.mdx/o x/o/f.js x/o/f.md ✅ ✅ ✅ ✅ x/o/z x/o/z/f.js x/o/z/f.md ✅ ✅ ✅ x/y x/y/f.js x/y/f.mdx/y/o x/y/o/f.js x/y/o/f.md ✅ ✅ ✅

      **.md is the same as *.md

      **.md works like *.md, not like **/*.md. If you append or prepend anything to ** other than /, it will work exactly the same as *.

      answered Mar 22, 2021 at 10:37
      Inigo
      14.5k55 gold badges4848 silver badges7979 bronze badges
      13

      The exact behavior of this particular wildcard has been well covered by the other answers, but information on the general case may be useful.

      This behavior is not limited to ls, and is referred to as "globbing", which is the expansion of patterns based on matches with existing filenames. It is important to note that these patterns do not use regular expression syntax.

      The shell pre-processes the arguments before they are sent to the program. There are generally multiple levels of expansion, some of these involve globbing.

      A great resource for more information on the other wildcards available in a file glob pattern is the unix manpage. A online version for glob can be found here.

      Finally, a simple example of what this can do for you, especially when combined with other shell expansion goodies, in this case those provided by the bash shell. Information about the expansions used in this example can be found in the Bash Guide for Beginners - which is my goto resource, despite the title.

      ls *{01..04}.{txt,csv} becomes ls *01.txt *01.csv *02.txt *02.csv *03.txt *03.csv *04.txt *04.csv

      Which could output something like this:

      input_01.txt input_02.txt input_03.txt input_04.txt output_01.csv output_02.csv output_03.csv output_04.csv
      

      While skipping these:

      input_05.txt input_06.txt input_07.txt input_08.txt input_09.txt input_10.txt output_05.csv output_06.csv output_07.csv output_08.csv output_09.csv output_10.csv
      

      A trivial example, but if you know that this behavior is not specific to ls, then you can imagine the utility when coupled with mv, cp, rsync, etc.

      answered Jan 28, 2015 at 18:38
      Morgen
      1,03811 gold badge1111 silver badges1616 bronze badges

      Your Answer

      Sign up or log in

      Sign up using Google
      Sign up using Email and Password

      Post as a guest

      Name
      Email

      Required, but never shown

      By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

      Not the answer you're looking for? Browse other questions tagged or ask your own question.

Hot Network Questions