//View Tip #189
Similar Tips
» Find and Grep
» Finding log files X number of days old and deleteing them.
» Deleting difficult filenames
» Highlight Grepped Text
» Filter stderr for cron jobs


Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
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

View Comments »


Add your comment

Comments are currently disabled
piping the output of ls is unsafe, better use extglobs:

 $ ls
 1.txt  2.txt  3.txt  4.txt
 $ shopt -s extglob
 $ rm !(4.txt)
 $ ls
Posted 2009-02-16 03:57:45

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit