Tuesday 3 April 2012

Delete all files in directroy except....

You have multiple options to achieve this. If you want to delete all the files in a directory except the ones that start with the letter "a", the simplest option is to use:
rm [!a]*
But if you want to delete everything except the files that contain "to_keep" in their names you can use grep's inverse matching capability like this:
rm $(ls * | grep -v to_keep)
If what you're looking for is to delete every file except a particular one named "my_file" the previous option might not work for you because it won't delete any file that contains "my_file" as part of their filenames. The following will ensure that this doesn't happen:
rm $(ls * | grep -v '^my_file$')

Possibly Related Posts

No comments:

Post a Comment