How to Copy and Move Files with Specific File Extensions in Linux

The simple command line syntax of Linux makes it simple to copy and move files with specific file extensions.

When you want to move or copy files with a specific file extension, then you can use the same mv and cp commands with a wildcard character, which is a more dynamic way to move the file at once.

Not only that, you can also leverage your find command knowledge to look for a specific type of file extension and copy or move it to some other directory to organise your current directory structure.

Copy Files with Specific File Extensions

As you know, cp commands require source and destination to copy files from one directory to another directory, and the same command can be used with the wildcard character to copy files with a specific file extension.

Move or Copy only Specific extension

For example, if you want to copy or move all the “*.txt” files from your current directory to the ~/Documents folder, then you can use a asterisk (*) wildcard character with the cp command as shown below

$ cp -v *.txt ~/Documents
or
$ mv -v  *.txt ~/Documents

The output of the above command is shown below:

Move or Copy only Specific extension
Move or Copy only Specific extension

This is especially useful if you have multiple files with the same extension and need to copy them all at once.

Move or Copy Multiple Specific Extensions 

But what if you need to copy several files with different extensions? Simply specify all of the extensions you want to copy or move, as well as their destination paths, and the cp or mv command will handle the rest.

Let’s say in my current directory I have four different types of documents available, and I want them all to get copied to my ~/Documents folder.

To accomplish this, I would use the following command:

$ cp -v *.txt *.odt *.docx *.pdf ~/Documents
or
$ mv -v *.txt *.odt *.docx *.pdf ~/Documents

The output of the above command is shown below:

Move or Copy Multiple Specific Extensions
Move or Copy Multiple Specific Extensions

And the output of the command that was just executed is as follows:

Move or Copy Specific file Extension using Wildcard Character in Filename

It would be great if we could do things like say, “I want to move or copy “.txt” or any specific file extension to another directory, but I don’t want to move all of the “.txt” based extensions to the new destination.”

Yes, you can move any file with the “.txt” or a similar extension from one directory to another, but you have to make sure that the names of the files you’re moving match a pattern or change the "*" wildcard characters to match the pattern you want.

To give you an example, let’s say I need to copy a “.txt” file whose first letter is between “a” and “g,” but the rest of the text file should not be copied to the new location, then the below command will work flawlessly:

$ cp -v [a-g]*.txt ~/Documents
or
$ mv -v [a-g]*.txt ~/Documents

The output of the above command is shown below:

Move or Copy Specific file Extension using Wildcard Character in Filename
Move or Copy Specific file Extension using Wildcard Character in Filename

And here is the wildcard character that you can use to copy or move a file from the directory:

  • ^ Exclude from match
  • * Match one or number of more occurrence
  • ? Match a single occurrence
  • [] Specify a range for matching

Find and Copy Files with Specific File Extension

If you want to move all the specific files with certain extensions from multiple directories to a specific directory, then what will you do? 

Most likely, you will manually look for the specific file extension in each directory, and then you will copy it to another location, right? But I don’t recommend this because you can use the find command to find the file based on your given pattern.

And here is the syntax which you can use to find and copy file:

$ find /path/where/to/look -name "file.extension" | cp -vt /destionation/path

So let’s take a simple example where you will learn how to use the find command to locate a file on the basis of a particular file extension, and after getting the result, you can use the cp or mv commands to copy or move the file to the desired location.

$ find . -name "*.txt" | xargs cp -vt ~/Documents
OR
$ find . -name "*.txt" | xargs mv -vt ~/Documents

The output of the above command is shown below:

Find and Copy Files with Specific File Extensions
Find and Copy Files with Specific File Extensions

To avoid errors, make sure your system has access to the destination path.

Also Read: 20+ Find command which you can use daily

The above command will not work if you source file path contain white space, so you can use the following command to overcome from the situation.

$ find . -iname '*.txt' -type f -print0 | xargs -0 mv -t ~/Documents

The issue with the above command arises when you have two files with the same filename, such as “dir1/filename.txt” and “dir2/filename.txt,” and when you use the above command, it will not copy the second file due to a file name clash.

Another issue with the commands is that when you copy a file recursively, the above command does not preserve the directory structure.

To fix this, you can use the below command, which will allow you to easily copy any files with a “.txt” extension, no matter where they are located in the directory structure, while keeping their original directory intact

$ cp --parents --verbose `find -name \*.txt` /home/shen/Documents/

The output of the above command is shown below:

Copy specific files with extension by preserving parent directory structure
Copy specific files with extension by preserving parent directory structure

Wrap up

I hope you found this discussion on how to copy files with the “.txt” extension while preserving the original directory structure and moving a specific type of file extension to another directory useful.

If you want, you can also explore another topic, which is also full to delete files with specific extensions from the Linux command Line.

Anyway, if there is something I need to consider, please use the space below to provide any additional information or feedback.

Till then, put your feet up and relax. As always, the next article will be up shortly

Leave a Reply