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

Find and Copy Certain Type Of Files

My external hard disk is a mess. Yeah because, I have dumped all files in a single directory.  I was 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 directory. I decided to create folders for each type of file and store the files in the respective folders. Say for example, media files (audio and video) goes to a directory called Multimedia, image files goes to Pictures folder, and official related stuffs goes 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 amount of data. So, I did a quick search in Google and found a suitable solution. If you ever wanted to copy certain type of files from one directory to another in 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 - Sexy Mama.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 - Sexy Mama.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. 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 in a directory to another.

Hope this helps. If you find this guide useful, please share it on your social. professional networks and support OSTechNix. More good stuffs to come. Stay tuned!

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *