Bash: Copy Multiple Specific Files from One Folder to Another


Often you may want to copy multiple specific files from one folder to another using Bash.

You can use the following syntax to do so:

cp -t /c/users/bobbi/data2/folder2/ coaches.txt teams.txt

This particular example copies the files named coaches.txt and teams.txt from the current folder to the folder located at the following path:

  • c:/users/bobbi/data2/folder2

Here is what the various commands do:

  • cp: This command allows you to copy one or more files to another location
  • -t: This prevents Bash from creating a directory or subdirectory

The following example shows how to use this syntax in practice.

Example: Copy Multiple Specific Files from One Folder to Another

Suppose that we have a folder named folder1 with several text files and we would like to copy several of these files to another folder named folder2.

We can use the ls command to view the contents of the current folder named folder1:

We can see that this folder contains the following files:

  • cities.txt
  • coaches.txt
  • managers.txt
  • teams.txt

Suppose that we would like to copy only the files named coaches.txt and teams.txt to a different folder named folder2.

We can use the following syntax to do so:

cp -t /c/users/bobbi/data2/folder2/ coaches.txt teams.txt

The following screenshot shows how to use this syntax in practice:

Bash copy multiple specific files from one folder to another

Notice that when we use the ls command again to view the contents of folder2 we can see that the files named coaches.txt and teams.txt have both been copied to folder2.

Note: In this particular example we copied two specific files from one folder to another but you can list out as many specific files as you’d like to copy even more files.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to List All Files in Folder by Date
Bash: How to List All Files in Folder by Size
Bash: How to Delete All Files in Directory

Leave a Reply