Difference between revisions of "Remove all files apart from some specified"
From MyWiki
| Line 24: | Line 24: | ||
============ | ============ | ||
rm PATH/!(*.txt) | 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 | ||
| + | |||
</source> | </source> | ||
Revision as of 15:42, 6 January 2015
## 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