Skip to main content
slight adjustment
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267

*/** will only match directories (and their subdirectories & files); it will not match files (non-directories) in the current directory, because the */ portion of it requires a directory prefix before beginning the ** globstar expansion. As for **/*, the trailing /* is extraneous, since the **/ will, by itself, expand to every file and directory under the current directory (subject to the dotglob option). Since every directory has been expanded by that point, the trailing /* does not match anything.

Be careful using ls to test, since it will "helpfully" read into any directories that you might pass it; consider instead something like:

printf "%s\n" */** printf "%s\n" **/* 

Also note that piping to wc -l could mislead you for actual counts; consider:

$ touch a $'b\nc' $ ls -1 a b?c $ ls | wc -l 3 ## WRONG! 

*/** will only match directories (and their subdirectories & files); it will not match files (non-directories) in the current directory, because the */ portion of it requires a directory prefix before beginning the ** globstar expansion. As for **/*, the trailing * is extraneous, since the **/ will, by itself, expand to every file and directory under the current directory (subject to the dotglob option). Since every directory has been expanded by that point, the trailing /* does not match anything.

Be careful using ls to test, since it will "helpfully" read into any directories that you might pass it; consider instead something like:

printf "%s\n" */** printf "%s\n" **/* 

Also note that piping to wc -l could mislead you for actual counts; consider:

$ touch a $'b\nc' $ ls -1 a b?c $ ls | wc -l 3 ## WRONG! 

*/** will only match directories (and their subdirectories & files); it will not match files (non-directories) in the current directory, because the */ portion of it requires a directory prefix before beginning the ** globstar expansion. As for **/*, the trailing /* is extraneous, since the ** will, by itself, expand to every file and directory under the current directory (subject to the dotglob option). Since every directory has been expanded by that point, the trailing /* does not match anything.

Be careful using ls to test, since it will "helpfully" read into any directories that you might pass it; consider instead something like:

printf "%s\n" */** printf "%s\n" **/* 

Also note that piping to wc -l could mislead you for actual counts; consider:

$ touch a $'b\nc' $ ls -1 a b?c $ ls | wc -l 3 ## WRONG! 
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267

*/** will only match directories (and their subdirectories & files); it will not match files (non-directories) in the current directory, because the */ portion of it requires a directory prefix before beginning the ** globstar expansion. As for **/*, the trailing * is extraneous, since the **/ will, by itself, expand to every file and directory under the current directory (subject to the dotglob option). Since every directory has been expanded by that point, the trailing /* does not match anything.

Be careful using ls to test, since it will "helpfully" read into any directories that you might pass it; consider instead something like:

printf "%s\n" */** printf "%s\n" **/* 

Also note that piping to wc -l could mislead you for actual counts; consider:

$ touch a $'b\nc' $ ls -1 a b?c $ ls | wc -l 3 ## WRONG!