How do I Copy Multiple Files Using CP in Linux
CP means that you can replica directories and command information the usage of the command line. With this command, you’ll switch more than one information or folders, hold characteristic knowledge and create their backups. CP copies document independently from their originals. So, we will be able to say that the CP command comes in handy for Linux. People nonetheless don’t understand how to make use of this command and seek for solutions referring to copying more than one information the usage of CP in Linux. That’s why we now have written this text to in short describe the best way to replica more than one information the usage of CP in Linux.
How do I Copy Multiple Files Using CP in Linux?
Now we can give an explanation for other use CP for appearing quite a lot of duties.
Copy a File Using CP Command
The document, which we can replica with the cp command, passes its identify and vacation spot. We will replica the Linuxhint.txt document to a brand new document named Linuxhintteam.txt the usage of the cp command. During the operation, the cp command may even create a brand new document a part of the operation.
First, choose a folder/listing wherein the document is to be had. In our case, the document is to be had within the Documents listing, so we use the next command:
After the usage of the ls command to show the to be had document within the listing.
Now, execute the underneath command to keep a copy of a particular document:
cp
Here we now have used the underneath command to keep a copy of the Linuxhint.txt document:
cp Linuxhint.txt Linuxhintteam.txt
To check that the document is effectively copied, use the ls command once more.
Copy Multiple Files Using CP Command
You will have to supply each the document identify and the vacation spot listing when the usage of the cp command to replicate more than one information.
First, open the precise listing within the terminal and execute the tree command. If you don’t know concerning the tree command, then please take a look at this weblog.
In the Documents folder, we now have two information, i.e., Linuxhint.txt and Linuxhintteam.txt, and one folder named Linuxtricks. Now, execute the underneath command within the terminal:
cp
Here we used the next command to replicate Linuxhint.txt and Linuxhintteam.txt within the Linuxtricks folder:
cp Linuxhint.txt Linuxhintteam.txt Linuxtricks/
To check that the information are copied, use the tree command once more.
Copy a Directory Using CP Command
The CP command, by way of default, does now not replica directories. On copying the listing, it presentations an error.
cp foldername/ foldername 1
cp: omitting foldername ‘foldername 1
To replica the listing with the assistance of the cp command, we need to move -R flag. It creates a replica by way of copying the flagged folder recursively.
cp -r foldername/ foldername 1
Here we now have used the next command to replicate the directories:
cp -r Linuxtricks/ Linuxtips
Finally, execute the tree command to make sure that the machine has effectively created more than one directories.
Copy Multiple Directories Using CP Command
To replica the more than one directories with the cp command, replica the trail of the directories and move it after the vacation spot listing.
First, execute the tree command to look information about information and folders to be had within the listing:
Now execute the underneath command to replicate more than one directories the usage of the CP command:
cp -r Foldername Foldername1 Foldername2 Foldername3
Here we used the next command to replicate Linuxtips and Linuxtricks into the LinuxOS folder:
cp -r Linuxtips Linuxtricks LinuxOS
Finally, we verified that the machine copied the directories as it should be.
Take a Backup when Copying a File
We can use the -b flag to again up the document if anyone overwrites the copied document. It additionally creates a backup document by way of copying the document in position.
ls
Filename.txt Filename1.txt
cp -b Filename.txt Filename1.txt
ls
Filename.txt Filename1.txt Filename1.txt~
In the above instance, Linuxhintteam.txt ~ represents the backup document.
Prompt for Confirmation when Copying a File
We can use the -i flag to advised affirmation after we replica the document. Usually, a vacation spot document is overwritten when the usage of the CP command. This occurs in that situation when the document is provide on the time of copying. The command will advised the usage of the -i flag for overwriting the document.
ls
Filename.txt Filename1.txt
cp -i Filename.txt Filename1.txt
cp: overwrite ‘Filename.txt’?
Create a Hard Link Instead of Copying
We can move the -l flag whilst growing a difficult hyperlink as a substitute of copying with the assistance of the cp command. A brand new document is created by way of now not copying the document, which is a difficult hyperlink to the knowledge at the disk. This is a primer on comfortable or symbolic and tough hyperlinks.
ls
Filename.txt
Filname1.txt
cp -l Filname.txt Filname1.txt
echo ‘Filename1 textual content’ > Filename1.txt
Filname.txt
Filename textual content
Preserve File Attributes
The hold choice is handed to hold the document attributes (i.e., consumer possession, team, and permissions) in conjunction with the houses to be preserved, by way of default, a style that can hold timestamp and possession.
-rw——- 1 Linux customers 0 Sept 14 04:00 bar.txt
cp –preserve bar.txt foo.txt
-rw——- 1 Linux customers 0 Sept 14 04:00 foo.txt
-rw——- 1 Linux customers 0 Sept 14 04:00 bar.txt
Display All Files Copied
The -v choice is used within the Cp command to turn the information which might be copied. This choice prints folders and information which might be copied to plain 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 lets replica a number of information, folders, and directories the usage of the CP command. We defined to you some ways to make use of the CP command for copying the information. If there may be any question or questions on your thoughts referring to this text, then be happy to touch us.