Cheap VPS host selection
Provide server host evaluation information

How to use the cp command in Linux (introduction to the use of the cp command in Linux)

The cp command is used to copy files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing directory, it will copy all the previously specified files or directories to this directory. How to use the cp command of Linux? Let's take a look at the specific example.

1. Copy files from one directory to another

Copy a file by using "cp source file target file", and copy the source file to the target file by using this command.

[ root@sharplee creatfile]# cp hello.txt test1/test.txt

 566138-20220327134540753-773434542

If you want to copy the file name to the destination directory without changing, you can add the copy directory directly without adding the file name.

[ root@sharplee creatfile]# cp hello.txt test1/

 566138-20220327134848807-1558898007

2. Copy multiple files to another directory

The cp command can copy multiple files to another directory through the following command. The first is the source file, and the last is the directory to copy to.

[ root@sharplee creatfile]# cp tt1.log tt3.log tt4.log test3/

 566138-20220327135331078-1077826493

If there are multiple file names to be copied and the file extensions are the same, you can use the following methods to copy all files to another directory.

[ root@sharplee creatfile]# cp *.log test3/

 566138-20220327135657213-517457400

3. Back up files with the same name when copying

To copy a file through the cp command, some will interactively prompt whether to rename the file with the same name, and some will not prompt whether to rename the file with the same name. At this time, it is recommended to add the following options to backup the file with the same name when copying the file. The backup operation will prompt whether to overwrite it. Select Yes, and then a backup file will be generated.

[ root@sharplee creatfile]# cp –backup hello.txt test2/
or
[ root@sharplee sharplee]# cp -b hello.txt test2/

 566138-20220327140845374-772974831

 566138-20220327141016275-1274975716

4. Copy the file and keep the link

When executing the cp command, if the source file is a linked file, the actual file will be copied instead of the linked file. If you only want to copy the linked file as is, specify the option - d, as shown below:

[ root@sharplee creatfile]# cp -d softlinkfile.txt test1/

 566138-20220327142027637-185295837

5. Copying does not overwrite an existing file

If you want to copy only when the target file does not exist, use the - n option, as shown below. This will not overwrite the existing file, and the cp command will return success.

[ root@sharplee creatfile]# cp -n hello.txt test1/

 566138-20220327142401442-594278698

6. When copying a file with the same name, you will be prompted whether to overwrite it

When the - i option is used to copy a file, it will ask for confirmation before overwriting the file, as shown below.

[ root@sharplee creatfile]# cp hello.txt test1/

 566138-20220327142647689-802386780

7. Create a hard link to the file (instead of copying)

Use the cp command to create a hard link to a file instead of copying it. Use the following command.

[ root@sharplee sharplee]# cp -l hello.txt test4/

 566138-20220327143212946-1523921414

8. Create a hard link to the file (instead of copying)

Use the cp command to create a hard link to a file instead of copying it. Use the following command, and finally test5/hello.txt is the same file as hello.txt in the previous directory.

[ root@sharplee sharplee]# cp -l hello.txt test5/

 566138-20220327143212946-1523921414

8. Create a soft link to the file (instead of copying)

Use the cp command to create a soft link to a file instead of copying it. Creating a soft link file can only be operated in the same directory, and cannot be copied to other directories. The following is an example of creating a soft connection.

[ root@sharplee sharplee]# cp -s hello.txt hello1.txt

 566138-20220327143859717-538948350

9. Copy the file and retain the file attributes

Use the cp command to copy the file, retain the file mode, the ownership of the file, and the time stamp of the file.

[ root@sharplee sharplee]# cp -p hello.txt test6/

 566138-20220327144251675-125189860

Copy the file through the cp command. The mode of retaining the file is through the following command. However, the timestamp has been changed. You can also use the timestamp change mode to change the timestamp. Use the option – preserve=timestamp.

[ root@sharplee sharplee]# cp –preserve=mode hello.txt test6/

 566138-20220327144718004-1210862291

10. Copy the latest file, not the old file

Use the cp command to copy the file. Only the latest file is copied. The old file is not copied. Check whether it is the latest file according to the timestamp.

[ root@sharplee sharplee]# cp -u hello.txt test6/

 566138-20220327150725671-1600714215

11. Copy directory

Use the cp - r command to copy a directory to another directory.

[ root@sharplee sharplee]# cp -r test7 test8/

 566138-20220327151048972-1143189795

12. Copy multiple directories

Use the cp command to copy multiple directories to a single directory.

[ root@sharplee sharplee]# cp -r test6 test7 test8 test9 test10

 566138-20220327151932251-1564288748

Cp is mainly used to copy a file or directory to another file or directory. This article mainly explains how to use the copy command. You should be very careful about the use of the copy command, otherwise improper use will result in the file not being backed up and overwriting the file.

Do not reprint without permission: Cheap VPS evaluation » How to use the cp command in Linux (introduction to the use of the cp command in Linux)