CP allows you to copy directories and command files using the command line. With this command, you can transfer multiple files or folders, preserve attribute information and create their backups. CP copies file independently from their originals. So, we can say that the CP command is useful for Linux. People still don’t know how to use this command and search for answers regarding copying multiple files using CP in Linux. That’s why we have written this article to briefly describe how to copy multiple files using CP in Linux.

How do I Copy Multiple Files Using CP in Linux?

Now we will explain different methods to use CP for performing various tasks.

Copy a File Using CP Command

The file, which we will copy with the cp command, passes its name and destination. We will copy the Linuxhint.txt file to a new file named Linuxhintteam.txt using the cp command. During the operation, the cp command will also create a new file part of the operation.

First, select a folder/directory in which the file is available. In our case, the file is available in the Documents directory, so we use the following command:

After using the ls command to display the available file in the directory.

Now, execute the below command to make a copy of a specific file:

cp

Here we have used the below command to make a copy of the Linuxhint.txt file:

cp Linuxhint.txt Linuxhintteam.txt

To verify that the file is successfully copied, use the ls command again.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Copy-Multiple-Files-Using-CP-Linux-01.png" data-lazy- height="228" src="data:image/svg xml,” width=”936″>

Copy Multiple Files Using CP Command

You must provide both the file name and the destination directory when using the cp command to copy multiple files.

First, open the specific directory in the terminal and execute the tree command. If you don’t know about the tree command, then please check out this blog.

In the Documents folder, we have two files, i.e., Linuxhint.txt and Linuxhintteam.txt, and one folder named Linuxtricks. Now, execute the below command in the terminal:

cp /

Here we used the following command to copy Linuxhint.txt and Linuxhintteam.txt in the Linuxtricks folder:

cp Linuxhint.txt Linuxhintteam.txt Linuxtricks/

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Copy-Multiple-Files-Using-CP-Linux-02.png" data-lazy- height="432" src="data:image/svg xml,” width=”936″>

To verify that the files are copied, use the tree command again.

Copy a Directory Using CP Command

The CP command, by default, does not copy directories. On copying the directory, it shows an error.

cp foldername/ foldername 1


cp: omitting foldername ‘foldername 1

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Copy-Multiple-Files-Using-CP-Linux-03.png" data-lazy- height="56" src="data:image/svg xml,” width=”936″>

To copy the directory with the help of the cp command, we have to pass -R flag. It creates a copy by copying the flagged folder recursively.

cp -r foldername/ foldername 1

Here we have used the following command to copy the directories:

cp -r Linuxtricks/ Linuxtips

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Copy-Multiple-Files-Using-CP-Linux-04.png" data-lazy- height="312" src="data:image/svg xml,” width=”936″>

Finally, execute the tree command to verify that the system has successfully created multiple directories.

Copy Multiple Directories Using CP Command

To copy the multiple directories with the cp command, copy the path of the directories and pass it after the destination directory.

First, execute the tree command to see details about files and folders available in the directory:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Copy-Multiple-Files-Using-CP-Linux-05.png" data-lazy- height="326" src="data:image/svg xml,” width=”936″>

Now execute the below command to copy multiple directories using the CP command:

cp -r Foldername Foldername1 Foldername2 Foldername3

Here we used the following command to copy Linuxtips and Linuxtricks into the LinuxOS folder:

cp -r Linuxtips Linuxtricks LinuxOS

Finally, we verified that the system copied the directories correctly.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Copy-Multiple-Files-Using-CP-Linux-06.png" data-lazy- height="538" src="data:image/svg xml,” width=”936″>

Take a Backup when Copying a File

We can use the -b flag to back up the file if someone overwrites the copied file. It also creates a backup file by copying the file in place.

ls


Filename.txt Filename1.txt


cp -b Filename.txt Filename1.txt


ls


Filename.txt Filename1.txt Filename1.txt~

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Copy-Multiple-Files-Using-CP-Linux-07.png" data-lazy- height="154" src="data:image/svg xml,” width=”936″>

In the above example, Linuxhintteam.txt ~ represents the backup file.

Prompt for Confirmation when Copying a File

We can use the -i flag to prompt confirmation when we copy the file. Usually, a destination file is overwritten when using the CP command. This happens in that condition when the file is present at the time of copying. The command will prompt using the -i flag for overwriting the file.

ls


Filename.txt Filename1.txt


cp -i Filename.txt Filename1.txt


cp: overwrite ‘Filename.txt’?

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Copy-Multiple-Files-Using-CP-Linux-08.png" data-lazy- height="50" src="data:image/svg xml,” width=”936″>

Create a Hard Link Instead of Copying

We can pass the -l flag while creating a hard link instead of copying with the help of the cp command. A new file is created by not copying the file, which is a hard link to the data on the disk. This is a primer on soft or symbolic and hard links.

ls


Filename.txt


Filname1.txt


cp -l Filname.txt Filname1.txt


echo ‘Filename1 text’ > Filename1.txt


Filname.txt


Filename text

Preserve File Attributes

The preserve option is passed to preserve the file attributes (i.e., user ownership, group, and permissions) along with the properties to be preserved, by default, a mode that will preserve timestamp and ownership.

-rw——- 1 Linux users 0 Sept 14 04:00 bar.txt


cp –preserve bar.txt foo.txt


-rw——-  1 Linux users     0 Sept 14 04:00 foo.txt


-rw——-  1 Linux users     0 Sept 14 04:00 bar.txt

Display All Files Copied

The -v option is used in the Cp command to show the files that are copied. This option prints folders and files that are copied to standard outputs.

cp -R -v Filename Filename1


‘Filename’ -> ‘Filename1’


‘Filename/Filename.txt’ -> ‘Filename1/Filename.txt’


‘Filename/Filename1.txt’ -> ‘Filename1/Filename1.txt’

Conclusion

This article described how we could copy one or more files, folders, and directories using the CP command. We explained to you many ways to use the CP command for copying the files. If there is any query or questions in your mind regarding this article, then feel free to contact us.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/a6dca3a5394c9d066a8078ddf9ede366?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.