The full text of the most detailed series of Linux command -cp command

cp command to copy a file or directory is one of the most commonly used Linux system commands. Under normal circumstances, shell will set up an alias when copying files, if the target file already exists, it will ask whether to overwrite the command line, regardless of whether you use the -i parameter. But if cp is executed in shell scripts, not asking to overwrite the absence -i parameter. This shows some differences execute the command line and shell scripts.

  1. Format:
    Usage:
    cp [option] ... [-T] source and destination
    or: cp [options] source ... directory ...
    or: cp [option] ... -t directory source ...

  2. Function:
    to copy the source file to the target file, or copy multiple source files to the target directory.

  3. Command parameters:

-a, --archive        等于-dR --preserve=all
--backup[=CONTROL    为每个已存在的目标文件创建备份
-b                   类似--backup 但不接受参数
--copy-contents      在递归处理是复制特殊文件内容
-d                   等于--no-dereference --preserve=links
-f, --force        如果目标文件无法打开则将其移除并重试(当 -n 选项存在时则不需再选此项)
-i, --interactive        覆盖前询问(使前面的 -n 选项失效)
-H                       跟随源文件中的命令行符号链接
-l, --link               链接文件而不复制
-L, --dereference        总是跟随符号链接
-n, --no-clobber         不要覆盖已存在的文件(使前面的 -i 选项失效)
-P, --no-dereference     不跟随源文件中的符号链接
-p                       等于--preserve=模式,所有权,时间戳
--preserve[=属性列表   保持指定的属性(默认:模式,所有权,时间戳),如果可能保持附加属性:环境、链接、xattr 等。
-R, -r, --recursive  复制目录及目录内的所有项目
  1. Command examples:

Example a: copy a single file to the target directory, the file does not exist in the target file
command:

cp log.log test5

Output:

[root@localhost test]# cp log.log test5
[root@localhost test]# ll
-rw-r--r-- 1 root root    0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-xr-x 2 root root 4096 10-28 14:53 test5
[root@localhost test]# cd test5
[root@localhost test5]# ll
-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root 0 10-28 14:53 log.log

Description:
In the absence of the -a parameter, time the two files is not the same. When with the -a parameter, time the two documents are consistent.
Example 2: When the target file exists, it will be asked whether to overwrite

command:

cp log.log test5

Output:

[root@localhost test]# cp log.log test5
cp:是否覆盖“test5/log.log”? n
[root@localhost test]# cp -a log.log test5
cp:是否覆盖“test5/log.log”? y
[root@localhost test]# cd test5/
[root@localhost test5]# ll
-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log

Explanation: The
target file exists, it will be asked whether to overwrite. This is because the cp is cp -i alias. When the target file exists, even if the addition of the -f flag, also will be asked whether to overwrite.

Three examples: copy the entire directory

Command:
Output:
target directory exists when:

[root@localhost test]# cp -a test3 test5 
[root@localhost test]# ll
-rw-r--r-- 1 root root    0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]# cd test5/
[root@localhost test5]# ll
-rw-r--r-- 1 root root    0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root    0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root    0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root    0 10-28 14:48 log.log
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
目标目录不存在是:
[root@localhost test]# cp -a test3 test4
[root@localhost test]# ll
-rw-r--r-- 1 root root    0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]#

Note:
Note that the target directory exists or not, the result is not the same. When the target directory exists, the entire source directory is copied to the destination directory.

Four examples: Copy of log.log establish a link file log_link.log

command:

cp -s log.log log_link.log

Output:

[root@localhost test]# cp -s log.log log_link.log
[root@localhost test]# ll
lrwxrwxrwx 1 root root    7 10-28 15:18 log_link.log -> log.log
-rw-r--r-- 1 root root    0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5

Description:
That log_link.log -s parameter is caused by the establishment of a "shortcut", so you'll see in the far right of the file, the file is displayed "Link" where they go!

Guess you like

Origin www.cnblogs.com/passzhang/p/12132120.html