4

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.

Box Box Box Box
  • 209
  • 2
  • 6
  • 13
  • Recursively or non-recursively? – kos Jan 16 '16 at 16:49
  • Possible duplicate of [Delete all files except files with the extension pdf in a directory](http://askubuntu.com/questions/555318/delete-all-files-except-files-with-the-extension-pdf-in-a-directory) – Alex Jones Jan 16 '16 at 17:33
  • 2
    Possible duplicate of [How can I recursively delete all files of a specific extension in the current directory?](http://askubuntu.com/questions/377438/how-can-i-recursively-delete-all-files-of-a-specific-extension-in-the-current-di) – bain Jan 16 '16 at 18:20

4 Answers4

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 :

Maythux
  • 82,867
  • 54
  • 239
  • 271
  • 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
5

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

kos
  • 35,535
  • 13
  • 101
  • 151
Raphael
  • 7,985
  • 5
  • 34
  • 51
1

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.

Sohail
  • 111
  • 3
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.

daemondave
  • 121
  • 4