Tip of the Week: A useful tip for the rm command

Blog Post created by ken_foster on Jan 19, 2014

Everyone is probably familiar with using a wild card with the rm command to delete a large number of files in one go. For example, this command would remove all files that start with 'test' or end with '.c':


rm test.* *.c


It's very useful, but if you have a lot of files with dissimilar names, it can still be a bit tedious. In this Tip of the Week, I'll show you how to use an operator to specify which files not to remove.


Using the ! operator, I can make rm remove everything except what matches my command line arguments. The following command removes all files except those that start with 'test' or end with '.c':


rm !(test.*|*.c)


Give it a try!