In linux, how to delete all files EXCEPT the pattern *.txt?
You can use find
:
find . -type f ! -name '*.txt' -delete
Or bash's extended globbing features:
shopt -s extglob
rm *.!(txt)
Or in zsh:
setopt extendedglob
rm *~*.txt(.)
# || ^^^ Only plain files
# ||^^^^^ files ending in ".txt"
# | \Except
# \Everything
If you just want to delete all files except '*.txt' then you can use the following command:
$ find . -type f ! -name "*.txt" -exec rm -rf {} \;
but if you also want to delete directories along with the files then you can use this:
$ find . ! -name "*.txt" -exec rm -r {} \;
there are many ways could do it. but the most simple way would be (bash):
shopt -s extglob
rm !(*.txt)