Copy and Remove Files With Specific File Extensions

Copy And Remove Files With Specific File Extensions

In this tutorial, we are going to learn about copying files with a specific extension from one directory to another, and later we will remove the original files. For this tutorial, we are going to learn more about the cp & find command, and we’ll learn how can we use them together so that it is beneficial to us.

The cp command

cp command stands for copy, its command structure looks something like this :

cp {OPTION} <Source> <Destination>

For example, If you are copying only files (let’s say file1 & file2) to a new directory in the home folder, then you can simply type :

cp file1 file2 ~/new

And if you’re copying a whole directory, then you have to use the -r (recursive) option.

cp -r Folder1 ~/new

The find Command

The find command in Linux is used to list out all the files in the current or a specific directory. Using its options, we can also list files with a specific extension. Let’s say we only want to copy mp3 files to another directory, we will first list all the mp3 files using the find command, and then we will redirect its output to the cp command. To do this, type the following command in the terminal :

find  -name "*.mp3" -type f
List All Mp3 Files
List All the Mp3 Files

Copy All the Files With Specific Extension

As discussed above, we can simply pipe the output of the find command to the cp command. But, there are two ways through which you can copy files. Either you can maintain the directory structure (If a file is in a folder at the source, after copying it will remain in that folder at the destination) or you can copy all the files within a folder without maintaining the directory structure. We will discuss both methods one by one.

Maintain the Directory Structure While Copying

To maintain the directory structure, we will use the cp and find command as follows :

cp --parents `find -name \*.mp3*` /Destination/Folder
Maintaining Directory Structure While Copying
Maintaining Directory Structure While Copying

The --parents option allows us to maintain the directory structure while copying.

Don’t Maintain Directory Structure While Copying

To just copy all the mp3 files we have to a single directory, we can use the find and cp command as follows :

find . -name "*.mp3" -type f | xargs cp -vt Destination/Folder
Copy Without Maintaining The File Structure
Copy Without Maintaining The File Structure

Remove Files With a Specific Extension

Now, let’s say that you want to remove all the mp3 files from the original (source) folder now that you have copied all of them to a new destination. To execute this, we will simply use the find command to list all the mp3 files, and then just add -delete option at the end of the command.

find . -name "*.mp3" -type f -delete
Deleting All The Mp3 Files
Deleting All The Mp3 Files

Summary

Copying and deleting files using the GUI can be a hassle, as searching for individual files can take a lot of time. We hope that using this article, you were able to learn more about the find and cp command and were able to copy files in the terminal. For more details and options, you can always refer to the man pages of cp and find command.