In a Linux terminal, how to delete all files from a folder except one or two?
For example.
I have 100 image files in a directory and one
find supports a -delete
option so you do not need to -exec
. You can also pass multiple sets of -not -name somefile -not -name otherfile
user@host$ ls
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt josh.pdf keepme
user@host$ find . -maxdepth 1 -type f -not -name keepme -not -name 8.txt -delete
user@host$ ls
8.txt keepme