Skip to main content

Recursive filename globbing in bash

bash-1-logo-png-transparent.png

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 https://ftp.gnu.org/gnu/bash/bash-5.1.8.tar.gz
--2021-11-22 22:49:19--  https://ftp.gnu.org/gnu/bash/bash-5.1.8.tar.gz
Auflösen des Hostnamens ftp.gnu.org (ftp.gnu.org)… 209.51.188.20, 2001:470:142:3::b
Verbindungsaufbau zu ftp.gnu.org (ftp.gnu.org)|209.51.188.20|: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
122
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
1411
dzu@krikkit:/tmp/bash-5.1.8$

Comments

Comments powered by Disqus