how to copy files from multiple locations to a single folder from linux command line

Many times you want to copy files over from several different locations into single location on your system. There could be many reasons why you may want to do it. For example, this may be to aggregate all image files or log files from all over into a single folder.

Copying just the files that may be scattered over several directories and subdirectories several levels deep will also allow you to discard or remove the directory structure after copying the files. If you want to just copy the directory structure without any of the files, then you could do that as well.

The first and important part of the process is to correctly and easily identify all the files that you want to copy over. The find command is one of the most versatile and powerful utility to filter files from multiple locations based on several different criteria.

Are the files in the same parent folder? If the files all reside within the same parent folder,  let’s say folderOne and we want to copy over all the text (.txt) files over to another folder called targetFolder then you can use following command…

bash$ find folderOne -iname "*.txt" -exec cp {} targetFolder \;

If there are multiple folders that you want to copy from then you can still use find and specify multiple input folders, as below

bash$ find /path/to/sourceone /path/to/sourcetwo /path/to/sourcethree -iname "*.txt" -exec cp {} /path/to/target \;

Again, if you want to copy over files matching multiple criteria, you can still use the find command options to do so..

bash$ find /path/to/source -iname "*.txt" -o -iname "*.jpg" -o -iname "my*.gif" -exec cp {} /path/to/target/ \;

The above example will copy over all the files that has an extension of either .txt or .jpg as well as the .gif files whose names start with my into the target folder.

A big issue when copying files over to a single location is the files that have the same name. While having the same name is acceptable in different locations, you will need to decide ahead of time what you want to do if a file name collision occurs. You have mostly three straightforward options in this situation: (a) overwrite the existing file (b) Ignore the new file (c) update only if the file is newer.

The cp command does have some options that can be used to modify the behaviour while copying. The cp command overwrites by default, you can also use -f option to force overwrite. The -u or –update option allows you to only update and overwrite the file only if it is newer. The -n or –no-clobber option will disable the overwrite and ignore the file.

bash$ find /path/to/source -iname "myfiles*.txt" -exec cp -n {} /path/to/target \;

The above example will not overwrite any files after it has been copied over once.

Rather than copying files over, you can also use the mv command to move the files in the above examples.