Remove all files apart from some specified

From MyWiki
Revision as of 15:42, 6 January 2015 by George2 (Talk | contribs)

Jump to: navigation, search
## Delete all file except file1 ##
rm  !(file1)
 
## Delete all file except file1 and file2 ##
rm  !(file1|file2)
 
## Delete all file except all zip files ##
rm  !(*.zip)
 
## Delete all file except all zip and iso files ##
rm  !(*.zip|*.iso)
 
## You set full path too ##
rm /Users/vivek/!(*.zip|*.iso|*.mp3)
 
## Pass options ##
rm [options]  !(*.zip|*.iso)
rm -v  !(*.zip|*.iso)
rm -f  !(*.zip|*.iso)
rm -v -i  !(*.php)
 
============
rm PATH/!(*.txt)
==============
 
    ls shows me everything
    ls | grep pdf shows me the files I want to keep. Hmm.
    ls | grep -v pdf shows the opposite: all except what I want to keep. In other words, it shows the list of things I want to delete. I can confirm this before doing anything dangerous.
    ls | grep -v pdf | xargs rm sends exactly that list to rm for deletion