The cp command in Linux is one of the most widely used commands and it can be used to copy files and directories to the desired location. If you are also interested in learning the powerful Linux operating system and its application commands, we suggest this and other Content in the field of server management Do not miss. In this tutorial, we will introduce you to the cp command.
Use the cp command and its different types
With the help of the cp command and the definition options for it, copying can be done in different ways. For example, we can move multiple files to a specific path with one command. Or we can define properties and attributes such as access level and ownership for them while copying. It can be said that we can execute the cp command in the following ways:
1: cp file file1 2: cp file /home/test 3: cp file1 file2 file3 /home/test 4: cp [OPTION] file file1 5: cp [OPTION] file /home/test
- Mode 1: Copy the file with another name in the same path. For example, the my.cnf file as my.cnf.backup in the same path
- Second mode: copy the file in another path. For example, copy a file from the root path to the home / test path
- Mode 3: Copy multiple files to another path
- Fourth mode: copy as in the first mode, mentioning the desired conditions and properties. For example, copy the file while maintaining the access level and…
- Fifth mode: Copy the same as the second mode with the desired conditions and properties. For example, copy the file in another path while retaining access and…
Practical learning of cp command in ssh environment with the help of putty software
To get started, we need to connect to the server using the putty tool. Before starting, we suggest that you pay attention to the following:
Due to the fact that entering commands incorrectly in Linux can cause data loss or crash of the operating system, we recommend that you use a server that does not have important information to practice. We recommend training on a server with raw centos operating system.
Create files and directories to practice working with cp
We want to create files as file1.txt file2.cnf file3.back. Use the following commands in the root path to create the file:
echo > file1.txt echo > file2.cnf echo > file3.back
Now we create directories named test1 and test2 using the following command:
mkdir test1 test2
For example, we want to copy the file1.txt file to test1. To do this, we must enter the following command:
cp file1.txt /test1
If we want to copy the file to folders inside another path such as test1 / dir1 /, we do the following:
cp file1.txt /test1/dir1
Now if we want to copy the file1.txt file in the same path as file2.txt, we do the following:
cp file1.txt file2.txt
Now if we want to copy all three created files to test1 directory, we use the following command:
cp file1.txt file2.cnf file3.back test1
Now if we want to move the test1 directory into the test2 directory, we proceed with the following command:
cp -r test1 test2
Since directories are places to place files, we need to copy all its contents to the desired directory with the r-attribute with the cp command. In fact, it is not possible to copy a directory without entering r-.
Also, if we want to see the result in the cp command, we must use the v-property. As:
cp -v file1.txt /test1
With the p-property we can also command that the properties and accesses of the files are protected and do not change:
cp -p file1.txt /test1
Command to copy all files
Another commonly used model is to copy all files or all specific extensions such as txt. Copy!
For example, we want to copy all the existing path files to the test2 directory. To do this, we use the following command:
cp * /test2
Or, for example, we want to copy all files with the txt extension to test2:
cp *.txt /test2
As it turns out, the cp command can be combined with other features and we can do the things we want with it. If you have any questions or concerns, please post them in the comments section so that they can be answered.