Ubuntu – How to delete files with the same extension in one go using rm

command line

I had compiled many C programs together, and each of the file had a .gch created. Now I did not want the .gch files so I decided to delete them. But it would take lots of time using rm for deleting each file. So is there a way to remove files with the same extension in one go?

NOTE: I work in Ubuntu 14.04. I want the solution to be in shell or terminal. The question is not a duplicate as I want to use rm only.

Best Answer

easily use the expansion"*":

rm /path-to-directory/*.gch

This what is called filename expansion which use some special characters called wildcards.

suppose you have a directory containing the files (file, file0, file1, file01)

Some know wildcards:

The question mark (?) is a special character that causes the shell to generate filenames. It matches any single character in the name of an existing file.

example:

ls file?

The shell expands the "file?" argument and generates a list of files in the working directory that have names composed of "file" followed by any single character.

Then the output would be file0 and file1

The asterisk (*) performs a function similar to that of the question mark but matches any number of characters, including zero characters, in a filename.

now the output of the command:

ls file*

would be file, file0, file1 and also file01

The [ ] Special Characters causes the shell to match filenames containing the individual characters within the brackets.

for example the output of the command:

ls file[01]*

would be:

file0 file1 file01

This is just a simple introduction for shell expansion, you can read more :