14

Tip #189   Remove every file but one

It's easy to remove (or copy, move etc.) all files that match a given criteria, but harder to move all but ones excluded by a criteria.

To do this we can combine grep's -v option with Unix command substitution:

$ ls
1.txt   2.txt   3.txt   4.txt
$ rm `ls | grep -v 4\.txt`
$ ls
4.txt