0

How can we delete all files inside directory example/ and not .env file?

maybe rm -f !(.env) example/ ?

Kusalananda
  • 333,661
  • https://stackoverflow.com/questions/17184956/how-exclude-files-folders-for-remove https://stackoverflow.com/questions/4210042/how-to-exclude-a-directory-in-find-command – nobody Oct 02 '20 at 05:47

1 Answers1

1

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:

Kusalananda
  • 333,661