Remove all files apart from some specified
From MyWiki
## 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 {} \;