Skip to content
Advertisement

How to remove files without certain extension?

How to remove all files without the .txt and .exe extensions recursively in the current working directory? I need a one-liner.

I tried:

find . ! -name "*.txt" "*.exe" -exec rm -r {} 
find -type f -regextype posix-extended -iregex '.*.(txt|exe)$'

Advertisement

Answer

Try this.

find . -type f ! -name "*.exe" ! -name "*.txt" -exec rm {} ;

The above command will remove all the files other than the .exe and .txt extension files in the current directory and sub directory recursively.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement