Linux command - Select multiple files and copy | Cp* in Hindi Linux tutorial by mr coder rohit

How do I Copy Multiple Files Using CP in Linux

Click Here to Watch In Hindi

👇




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.

0 seconds of 14 secondsVolume 0 In our case, the file is available in the Documents directory, so we use the following command:Cd Documents

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

ls

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

cp <filename> <filename2>

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.

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 <filename> <filename1> <foldername>/

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

cp Linuxhint.txt Linuxhintteam.txt Linuxtricks/

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

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

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:

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.


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~

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'?

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'


Please Share

It Looks like you have completed your course!! We have created Online series based on the above content. You can give a test and check your skill.




Be a Part of CollegeSpike by giving your precious comment here and help us in improving our content.