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:
Here we have used the below command to make a copy of the Linuxhint.txt file:
To verify that the file is successfully copied, use the ls command again.
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:
Here we used the following command to copy Linuxhint.txt and Linuxhintteam.txt in the Linuxtricks folder:
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: omitting foldername 'foldername 1
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.
Here we have used the following command to copy the directories:
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:
Now execute the below command to copy multiple directories using the CP command:
Here we used the following command to copy Linuxtips and Linuxtricks into the LinuxOS folder:
Finally, we verified that the system copied the directories correctly.
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.
cp -b Filename.txt Filename1.txt
Filename.txt Filename1.txt Filename1.txt~
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.
cp -i Filename.txt Filename1.txt
cp: overwrite 'Filename.txt'?
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.
cp -l Filname.txt Filname1.txt
echo 'Filename1 text' > Filename1.txt
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.
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.
'Filename' -> 'Filename1'
'Filename/Filename.txt' -> 'Filename1/Filename.txt'
'Filename/Filename1.txt' -> 'Filename1/Filename1.txt'
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.