Linux's cp command and examples are explained in detail

  • 2020-05-13 04:21:06
  • OfStack

Recently learned the linux command, now put the cp command sorted out for future query.

Function: copy files or directories

Note: the cp directive is used to copy files or directories. If more than two files or directories are specified at the same time, and the last destination is an existing directory, it copies all the previously specified files or directories to this directory. If you specify multiple files or directories at the same time and the last destination is not an existing directory, an error message will appear

Parameters:

-a or --archive this parameter has the same effect as specifying the "-dpR" parameter at the same time

-b or --backup delete, overwrite the destination file first backup, the backup file or directory is also established as a symbolic link, and points to the source file or directory of the source file or directory link. If this parameter is not added, the source file or directory will be copied directly if a symbolic link is encountered during the replication process

-f or --force forcibly copies a file or directory, regardless of whether the destination file or directory already exists

Ask the user before overwriting the file -i or -interactive

-l or --link establishes hard links to source files instead of copying files

-p or --preserve retains the properties of the source file or directory, including owner, belonging group, permissions, and time

-P or --parents retains the path to the source file or directory, which can be either an absolute path or a relative path, and the destination directory must already be abundant

-r recursively processes files in the specified directory with subdirectory 1. If the form of the source file or directory does not belong to the directory or symbolic link, then law 1 is treated as a normal file

-R or --recursive recursively processes files and subdirectories 1 in the specified directory

-s or -- symbolic-link establishes symbolic links to source files, rather than copying files

-S < Backup the trailing character string > Or - suffix = < Backup the trailing character string > After backing up the destination file with the parameter "-b", a backup string is appended to the end of the backup file. The default backup suffix string is the symbol "~"

When -u or --update use this parameter, the file will only be copied if the source file's modification time (Modification Time) is newer than the destination file, or if the destination file with the corresponding name does not exist

-v or --verbose shows the execution process

-V < backups > Or - version - control = < backups > When specifying the backup file name, there are three ways to name the backup file name:

1.numbered or t, which will use the backup number, will add the string ~1~ at the end of the word, and the number number will be increments

2.simple or never will use a simple backup. The default backup trailing string is ~, which can also be specified by -S

3.existing or nil will use the current method, and the program will first check whether there is a backup number. If there is, the backup number will be used, and if there is no, the simple backup will be used

-x or -- one-file-system copied files or directories must be stored on the same file system as the one on which the cp directive is executed, or files located on other partitions will not be copied or processed

--help shows online help

--sparse= < Use time > Sets the time to save the Greek file

--version display version

Example:

Copy the file, only if the source file is newer than the modification time of the destination file


cp -u -v file1 file2

Copy file file1 into file file2


cp file1 file2

. Interactively copy the file file1 into the file file2


cp -i file1 file2

Copy file file1 to file2, specify the mode to use forced copy because the destination file already exists


cp -f file1 file2

Copy directory dir1 into directory dir2


 cp -R file1 file2

Copy file1, file2, file3 and dir1 to dir2 at the same time


cp -R file1 file2 file3 dir1 dir2

.preserve file properties when copying


 cp -p a.txt tmp/

Preserve the directory structure of the file when copying


cp -P /var/tmp/a.txt ./temp/

.backup files are generated when copying


cp -b a.txt tmp/

. Backup file is generated when copying, with tail tag ~1~ format


cp -b -V t  a.txt /tmp  

Specifies the backup file tail


cp file1 file2
0

Related articles: