Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rm -rf /base-dir-path/*/work isn't the same as /base-dir-path/*/*/work

If I want to delete all subdirectories with a given name from a given root directory in linux/unix, you would think that you could just issue a command like:

rm -rf /base-dir-path/*/work 

However, the above command will only go 1 directory deep when searching for any subdirectories named 'work'. To achieve what I want, I end up repeating the same command with an extra '*/' until the rm returns 'rm: No match.', EG:

rm -rf /base-dir-path/*/*/*/work

Is there a way to get commands like rm to match / in its wildcard search so that I only have to issue a single wildcard * character?

like image 359
user3462694 Avatar asked Jan 20 '26 16:01

user3462694


1 Answers

On tcsh 6.18.00 or newer:

set globstar
rm -rf /base-path/path/**/work

On bash 4.0 or newer:

shopt -s globstar
rm -rf /base-dir/path/**/work

On ksh:

set -o globstar # or set -G
rm -rf /base-dir/path/**/work

On zsh:

rm -rf /base-dir/path/**/work

Alternately, with a find compliant with the 2006 revision of POSIX:

find /base-dir/path -type d -name work -exec rm -rf -- '{}' +

If you don't have a find with -exec ... {} +, then you likely don't have an xargs -0 either, and need to do this the inefficient way to be safe:

find /base-dir/path -type d -name work -exec rm -rf -- '{}' ';'
like image 61
Charles Duffy Avatar answered Jan 23 '26 13:01

Charles Duffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!