Bash – Delete All Files with a Particular Extension in a Folder

bashdirectoryrmshell

If I set the current/working directory (navigating to it using cd) to some particular directory and then type:

rm *.xvg

What will this command do? Is it true that the above command will only delete files with the extension .xvg only in the working directory?

I was nervous about trying this before asking, because I want to be absolutely sure that the above command will only delete .xvg files LOCATED IN THE WORKING DIRECTORY.

Best Answer

Yes, rm *.xvg will only delete the files with the specified extension in your current directory.

A good way to make sure you are indeed in the directory you want delete your files is to use the pwd command which will display your current directory and then do an ls to verify you find the files you are expecting.

If you are bit apprehensive about issuing the rm command, there are 2 things you can do:

  1. type ls *.xvg to see a list of what files would be affected by this command.

  2. Unless you have a lot of files, you could always also use the -i command line switch for rm (also exists for cp and mv). Using rm -i *.xvg would prompt you for each individual file if it was ok to delete it, so you could be sure nothing you didn't expect was getting deleted. (This will be tedious if you have a lot of files though :)

Related Question