//View Tip #189
Similar Tips
» Grepping for processes
» List non-system users
» Get latest stable version of the Linux kernel
» Argument list too long
» Subtract one file from another

 

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
4.txt


View Comments »




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
 4.txt
Posted 2009-02-16 03:57:45

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit