Home Linux Commands How To Find and Copy Certain Type Of Files From One Directory To Another In Linux

How To Find and Copy Certain Type Of Files From One Directory To Another In Linux

By sk
Published: Last Updated on 61.3K views

My external hard disk is a mess. Yeah because, I have dumped all files in a single directory.  I was just too lazy to organize files and folders in the right order. It took me more time when I am looking for a particular file. So, I thought it would be much better if I organize the certain type of files in separate directories. I decided to create folders for each type of file and store the files in the respective folders. Say for example, all media files (audio and video) should go to a directory called Multimedia, image files to Pictures folder, and official related stuffs to a folder called ostechnix.

However, manually finding and copying a each particular type of file is tedious and quite time-consuming task. It will take hours to find and copy all files if you have large number of files. So, I did a quick Google search and found a suitable solution. If you ever wanted to copy certain type of files from one directory to another in Linux and Unix-like operating systems, here is one way to do it. There could be many ways to do this, but I found that the following method is easy and simple to follow.

Find And Copy Certain Type Of Files From One Directory To Another In Linux

We are going to use the 'find' command to do organize files. Find command comes pre-installed on most Unix-like distributions, so let us not bother installing it.  For the purpose of this guide, I will show how to quickly find and copy mp3 files from a directory called test1 to another directory called test2.

Let us check the contents of test directory.

$ ls /home/sk/test1

Sample output:

'Bombay Rockers.mp3' 'Marconi Union - Sleepless.mp3' wiua9.jpg
 books.txt Maruvaarthai.mp3

As you see in the above result, there are three mp3 files in the test1 directory. Let us copy these mp3 files to test2 directory. To do so, go to test1 directory using command:

$ cd /home/sk/test1/

Then, run the following command to find and copy all files that matches with extension .mp3.

$ find -iname '*.mp3' -exec cp {} /home/sk/test2/ \;

Let us break down the above command and see what each option does.

  • find - It's the command to find files and folders in Unix-like systems.
  • -iname '*.mp3' - Search for files matching with extension .mp3.
  • -exec cp - Tells you to execute the 'cp' command to copy files from source to destination directory.
  • {} - is automatically replaced with the file name of the files found by 'find' command.
  • /home/sk/test2/ - Target directory to save the matching files.
  • \; - Indicates it that the commands to be executed are now complete, and to carry out the command again on the next match.

Clear? Good! Now, let us check the test2 directory to verify if the files were copied properly.

$ ls /home/sk/test2

Sample output would be:

'Bombay Rockers.mp3' Maruvaarthai.mp3
'Marconi Union - Sleepless.mp3'

As you can see, all files with extension .mp3 have been copied from test1 to test2 directory. Similarly, you can copy other types of files to different directories of your choice as described above.

The above command will copy all *.mp3 files from the source directory and its sub-directories as well. If you don't want to do that, use -maxdepth flag. For example the following command will copy all .mp3 files from the source directory only, but not from its sub-directories.

$ find -maxdepth 1 -iname '*.mp3' -exec cp {} /home/sk/test2/ \;

This way you can save a lot of time when you want to find a particular type of file. This trick could be helpful when you have to transfer large amount of different types of files from one directory to another.

Related read:

For more details, refer man pages.

$ man find

There is also a script named 'Classifier' that will automatically organize your files based on the file extension to different directories. More details can be found in the link given below.

Hope this helps.

You May Also Like


Sankar March 27, 2019 - 9:27 pm

Maruvaarthai 🙂 A useful post on find command.. Thank you!!

asdf February 18, 2020 - 3:03 am

Isn’t this recursive? Wouldn’t it them copy all *.mp3 files in all sub-directories as well? What if we don’t want that.

sk February 18, 2020 - 11:37 am

In that case, use -maxdpeth flag like below. This will copy the files only from the source directory not from its sub-directories.

$ find -maxdepth 1 -iname ‘*.mp3’ -exec cp {} /home/sk/test2/ \;

Nick March 18, 2020 - 8:30 pm

i want to copy the source directory structure also with files in target folder how do i do that?

Bob Vlaggetjes April 1, 2020 - 11:04 pm

great man, so easy to use

Avinash January 28, 2021 - 6:00 am

how to list number of files moved as a log


Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More