2

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.

5

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 :

  • To be noted that file[01]* matches file01 because the * matches anything after file0 or file1, the square brackets in globbing patterns always match a single character in the class and this behavior can't be changed with {}, * or + like in regular expressions. – kos Jan 16 '16 at 16:56
3

To remove files with the same extension in one-go, just use find command.

find /path/to/directory -type f -iname '*.gch' -delete
  • -type tells find if you are searching for files or directories, here:

    f = file

    d = directory

  • iname tells find about the name of the file you are looking for.

    Note: name does the same thing but iname is recommended because it ignores cases whereas name doesn't.

  • -delete deletes those files

0

Simply type the following command in terminal (shell)

rm -rf  /path/to/files/*.<extension>

Be careful with the -f parameter. It will not warn before deleting.

0

If you are using C and Makefiles, you can also make a .PHONY recipe target and rm by default and add the byproduct "cruft" files that one deletes from a clean source directory. You still use the wildcard * token.

make clean

for example

   .PHONY clean


    clean:  
         rm -f *.gch  *.o *.temp *.log

then you have multiplied your productivity in cruft elimination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.