Linux remove all files by extension

How to delete all the files with a certain extension from a folder and all of its subfolders? [duplicate]

I want to remove all the .jpg files from my Music folder in order to save room. My Music folder contains subfolders, and I would like to know if there is a command to remove all the .jpg files from all these folders regardless of their level. Thanks for your help!

4 Answers 4

A quick and clean solution for the command line would be

cd find . -type f -iname \*.jpg -delete 
  • . tells to start searching in the current folder.
  • -type f tells find only to look for files.
  • -iname makes the search case insensitive.
  • -delete tells find to delete/remove all files found.

CAUTION! I recommend running the command without -delete first to get a list of the files that will be removed when -delete is included in the command. This way a small typo won’t delete anything you didn’t intend to.

For more information on how to use find and what it can do see man find

Note that find will not produce any output when -delete is used.

Regarding the comment on multiple extensions

find . -type f \( -name \*jpg -o -name \*png \) -delete

  • ( .. ) Group expression. Needs to be escaped from the shell, hence \( .. \) in the example
  • -o logical or

So the above example will match any file which has either jpg or png at the end of it’s name. More extensions can be added in the same way. Just make sure to remember -o between every -name statement, if -o is not specified find will default to -a (and) which won’t work since a file can’t end in both jpg and png at the same time.

Источник

Remove All Files with Extension Linux Command Line

Every file consists of a specific extension. If we want to delete files of the same or different extensions from our Linux system, we must follow many different types of commands. In this article, we will see how to remove all files with the extension Linux command line.

Remove All Files with Extension Linux Command Line

This section will explain different ways and methods to remove all files with extensions using the command line in Linux.

Using rm Command

The ‘rm’ command is a basic command-line utility in Linux to remove sockets, pipes, device nodes, symbolic links, directories, system files, etc. To remove a file with a particular extension, use the command ‘rm’. This command is very easy to use, and its syntax is something like this.

Читайте также:  Recover it all linux

In the appropriate command, ‘filename1’, ‘filename2’, etc., refer to the names, plus their full paths. When the files are in the same directory, we do not need to write the full path, but we must mention the full path if this is not the case.

We can use wildcard expressions to specify incremental, same-name files or files with specific file extensions. So, let’s take an example in which we will remove DATA.txt, DATA1.txt, and DATA2.txt files. These files are available in the Documents directory, so first, we will open them in the terminal using the following command:

After that, execute the below command

Now let’s verify that system has successfully deleted the files, so execute the following command:

Using Substring Remove Files

With the help of the following command, we can remove those files containing the substring ‘test’.

Here ‘*’ denotes any string. That’s why here ‘*test*“ considers all files named which have substring ‘test’.

We can easily remove files of particular extensions from any folder. We will delete files with the gif extension in this example. We can delete all GIF files from the folder using the following command.

The above syntax only works for files. Along with files we can also delete folders using argument ‘-r’:

The main thing to note is that it deletes the folder as a whole recursively, i.e., all the files, subfolders, etc., of that folder in the entire folder structure. This concludes that there is no way to recursively delete files with specific extensions or files with filename patterns.

Find Command

Find is the most effective and popular command to search files. The find command is used to remove file extensions in Linux. The find command searches the files recursively based on size, extension, name, and file parameters. Using the find command, we can pipe its output to ‘rm’.

Backup and Verify Files

Different commands are also used to verify which files we are deleting, and their location is correct. However, this step is not required once it is right to be sure.

Backup files

For backup, we use the tar command. Unless we are 100% sure that we have the right files to delete or not, we should make a backup.

We can add the -v option to see the list of files that the tar command backs up.

Remove files with find – delete

If we are hesitant while using ‘rm’, we can use ‘find’ apart from that. It has to be used with caution. It is something like this.

First of all, we make sure which file we have to remove. For this, we use the following command.

We have to take special care that -delete is the last argument in our command. If by mistake we put it before the -name *.bak argument, it removes everything.

Читайте также:  Linux настройка интернет через прокси

Remove files with find and xargs

It does not support the ‘-delete’ option. We can pipe it into ‘rm’ in the following way with the output that comes before us on searching.

Here we pass an argument to ‘rm’ using the ‘xargs’ command. We can recursively remove the entire folder structure by using this method.

Removes files with find-exec

We use the rm with different commands (such as trash) or additional options to remove files.

Using find with -exec gives us the advantage of using any option and command to delete files. At the same time, it also allows us to perform other bulk operations on a set of files.

Conclusion

This article taught us how to delete any file from folders or folders with any specific extension by using various commands. We hope that from this article explained by us, you will get complete information in one place and you must have understood it very well.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.

Источник

How to Remove Files with Specific Extension in Linux

To remove files with a specific extension, we use the ‘rm‘ (Remove) command, which is a basic command-line utility for removing system files, directories, symbolic links, device nodes, pipes, and sockets in Linux.

The command is quite simple to use and the basic syntax is:

Here, ‘filename1‘, ‘filename2‘, etc. are the names of the files including full path. If the files are in the same directory then, as you might already know, there is no need to write down full paths.

We can also use wildcard expressions to specify files that have similar or incremental names or to specify files with a specific file extension.

Remove Files in Linux Using Substring

For example, to remove all files contain the substring ‘test‘, we can run:

Here, the ‘*’ implies ‘any string‘. Hence the pattern ‘*test*’ considers all files with names containing the substring ‘test‘.

Remove File Using SubString of Filename

Remove Files with File Extension in Linux

Let’s take another example. We will try to delete all GIF files from the folder using the following:

Delete Files with Particular Extension

However, this syntax works only for files. Using the argument ‘-r’ we can delete both files as well as folders:

$ rm -r   .  

Note that this will delete the folder recursively in its entirety, i.e., it will delete the entire folder structure beneath it; the subfolders and all the files. Hence, there is no way to delete specific files with a pattern of filenames, or files with a specific extension recursively. Every time the entire folder structure is deleted.

Remove Files Recursively with File Extension in Linux

To achieve this, we can make use of the find command and pipe its output to ‘rm’. The find command is simply used to search for files recursively based on parameters like the filename, extension, size, etc.

Читайте также:  Линукс обновляет биос на

For example, to search recursively for files with extension “.png” , we run the following:

Search Files Recursively with Extension

Now, we simply pipe this output to ‘rm’ in the following way:

Delete Files Recursively with Extension

The ‘xargs’ command is simply used to pass the output of ‘find’ to ‘rm‘ as arguments. In this way, we have recursively deleted files of the extension PNG from the whole folder structure.

Conclusion

In this article, we learned about the ‘rm‘ command, how to delete files and folders using it, and how to delta files recursively with specific extensions.

If you have any questions or feedback, let us know in the comments below.

Источник

Recursively delete all files with a given extension [duplicate]

@Futal no it’s not. The answer’s title is a mismatch, but its accepted solution deletes files by glob pattern.

@roaima In meta.stackoverflow.com/questions/292329/…, there is some discussion on whether the same solution means exact duplicate. I don’t know which side is the «official» stance though.

2 Answers 2

That is evil: rm -r is not for deleting files but for deleting directories. Luckily there are probably no directories matching *.o .

What you want is possible with zsh but not with sh or bash (new versions of bash can do this, but only if you enable the shell option globstar with shopt -s globstar ). The globbing pattern is **/*.o but that would not be limited to files, too (maybe zsh has tricks for the exclusion of non-files, too).

But this is rather for find :

find . -type f -name '*.o' -delete 

or (as I am not sure whether -delete is POSIX)

find . -type f -name '*.o' -exec rm <> + 

@AubreyRobertson See the man page for find . -exec + (in contrast to -exec ; ) handles several files at once. The <> is the place holder for the file name / path (in both cases).

That’s not quite how the -r switch of rm works:

-r, -R, --recursive remove directories and their contents recursively 

rm has no file searching functionality, its -r switch does not make it descend into local directories and identify files matching the pattern you give it. Instead, the pattern ( *.o ) is expanded by the shell and rm will descend into and remove any directories whose name matches that pattern. If you had a directory whose name ended in .o , then the command you tried would have deleted it, but it won’t find .o files in subdirectories.

What you need to do is either use find :

Alternatively, if you are using bash you can enable globstar :

NOTE: all three options will delete directories whose name ends in .o as well, if that’s not what you want, use one of these:

find . -type f -name '*.o' -delete find . -type f -name '*.o' -exec rm <> \; rm -- **/*.o 

Источник

Оцените статью
Adblock
detector