Difference between revisions of "Remove all files apart from some specified"

From MyWiki
Jump to: navigation, search
(Created page with "< source lang="text"> ## Delete all file except file1 ## rm !(file1) ## Delete all file except file1 and file2 ## rm !(file1|file2) ## Delete all file except all zip fil...")
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
< source lang="text">
+
<source lang="text">
 +
 
 
## Delete all file except file1 ##
 
## Delete all file except file1 ##
 
rm  !(file1)
 
rm  !(file1)
Line 20: Line 21:
 
rm -f  !(*.zip|*.iso)
 
rm -f  !(*.zip|*.iso)
 
rm -v -i  !(*.php)
 
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
 +
===========
 +
cat /x/y/oratab | egrep -i "pattern1|pattern2"
 +
 +
cd /home/george2/Desktop/delete_test_for_Dan/html
 +
ls /home/george2/Desktop/delete_test_for_Dan/html | egrep -v 'skills-careers|rb-openaccess'| xargs rm -rf {} \;
 +
 
</source>
 
</source>

Latest revision as of 16:25, 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
===========
cat /x/y/oratab | egrep -i "pattern1|pattern2"
 
cd /home/george2/Desktop/delete_test_for_Dan/html
ls /home/george2/Desktop/delete_test_for_Dan/html | egrep -v 'skills-careers|rb-openaccess'| xargs rm -rf {} \;