How can we delete all files inside directory example/
and not .env
file?
maybe
rm -f !(.env) example/
?
How can we delete all files inside directory example/
and not .env
file?
maybe
rm -f !(.env) example/
?
If you set the extglob
and dotglob
shell options in the bash
shell with
shopt -s extglob dotglob
then the pattern example/!(.env)
would match all names in the example
directory that is not .env
.
Note that we need to set dotglob
to allow globbing patterns to match hidden names.
Using
rm -f example/!(.env)
would attempt to remove those matching names.
If that pattern expands to too many names you'll get an "argument list too long" error. Running it in a simple loop would be an alternative solution:
for name in example/!(.env); do rm -f "$name"; done
Related question: