Skip to main content

Recursive filename globbing in bash


I first came across recursive globbing in zsh and there are situations where the functionality comes in handy. A double star will match not only files but files inside arbitrary parent directories as well. As I am still using bash as my daily interactive shell, I am happy that this feature was also picked up starting from version 4.0 end of 2011.

The recursive globbing is disabled by default and needs to be enabled explicitly through shoopt:

dzu@krikkit:/tmp$ wget
--2021-11-22 22:49:19--
Auflösen des Hostnamens (…, 2001:470:142:3::b
Verbindungsaufbau zu (||:443 … verbunden.
HTTP-Anforderung gesendet, auf Antwort wird gewartet … 200 OK
Länge: 10533715 (10M) [application/x-gzip]
Wird in »bash-5.1.8.tar.gz« gespeichert.

bash-5.1.8.tar.gz           100%[=========================================>]  10,04M  7,29MB/s    in 1,4s    

2021-11-22 22:49:21 (7,29 MB/s) - »bash-5.1.8.tar.gz« gespeichert [10533715/10533715]

dzu@krikkit:/tmp$ tar xf bash-5.1.8.tar.gz 
dzu@krikkit:/tmp$ cd bash-5.1.8/
dzu@krikkit:/tmp/bash-5.1.8$ shopt globstar
globstar       	off
dzu@krikkit:/tmp/bash-5.1.8$ (for i in ** ; do echo "./$i" ; done ) | wc -l
dzu@krikkit:/tmp/bash-5.1.8$ shopt -s globstar
dzu@krikkit:/tmp/bash-5.1.8$ (for i in ** ; do echo "./$i" ; done ) | wc -l


Comments powered by Disqus