Reputation: 155
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)$'
Upvotes: 12
Views: 8647
Reputation: 23
Try the following:
rm -f $(find . -type f ! \( -name "*.txt" -o -name "*.exe" \))
This will first recursively find all files that do not end with .txt
or .exe
extensions, and then delete all of these files.
Upvotes: 0
Reputation: 52112
If you have GNU find
with the -delete
action:
find . -type f ! \( -name '*.txt' -o -name '*.exe' \) -delete
And if not:
find . -type f ! \( -name '*.txt' -o -name '*.exe' \) -exec rm -f {} +
using -exec ... {} +
to execute rm
as few times as possible, with the arguments chained.
Upvotes: 4
Reputation: 599
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.
Upvotes: 21