linux命令:cp命令学习与使用,linux命令cp学习


cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一。一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数。但是如果是在shell脚本中执行cp时,没有-i参数时不会询问是否覆盖。这说明命令行和shell脚本的执行方式有些不同。

1.命令格式:

用法:

cp[选项]…[-T]源目的

或:cp[选项]…源…目录

或:cp[选项]…-t目录源…

2.命令功能:

将源文件复制至目标文件,或将多个源文件复制至目标目录。

3.命令参数:

-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复制目录及目录内的所有项目

4.命令实例:

实例一:复制单个文件到目标目录,文件在目标文件中不存在

命令:

cplog.logtest5

输出:

[root@localhosttest]#cplog.logtest5

[root@localhosttest]#ll

-rw-r–r–1rootroot010-2814:48log.log

drwxr-xr-x6rootroot409610-2701:58scf

drwxrwxrwx2rootroot409610-2814:47test3

drwxr-xr-x2rootroot409610-2814:53test5

[root@localhosttest]#cdtest5

[root@localhosttest5]#ll

-rw-r–r–1rootroot010-2814:46log5-1.log

-rw-r–r–1rootroot010-2814:46log5-2.log

-rw-r–r–1rootroot010-2814:46log5-3.log

-rw-r–r–1rootroot010-2814:53log.log

说明:

在没有带-a参数时,两个文件的时间是不一样的。在带了-a参数时,两个文件的时间是一致的。

实例二:目标文件存在时,会询问是否覆盖

命令:

cplog.logtest5

输出:

[root@localhosttest]#cplog.logtest5

cp:是否覆盖“test5/log.log”?n

[root@localhosttest]#cp-alog.logtest5

cp:是否覆盖“test5/log.log”?y

[root@localhosttest]#cdtest5/

[root@localhosttest5]#ll

-rw-r–r–1rootroot010-2814:46log5-1.log

-rw-r–r–1rootroot010-2814:46log5-2.log

-rw-r–r–1rootroot010-2814:46log5-3.log

-rw-r–r–1rootroot010-2814:48log.log

说明:

目标文件存在时,会询问是否覆盖。这是因为cp是cp-i的别名。目标文件存在时,即使加了-f标志,也还会询问是否覆盖。

实例三:复制整个目录

命令:

输出:

目标目录存在时:

[root@localhosttest]#cp-atest3test5

[root@localhosttest]#ll

-rw-r–r–1rootroot010-2814:48log.log

drwxr-xr-x6rootroot409610-2701:58scf

drwxrwxrwx2rootroot409610-2814:47test3

drwxr-xr-x3rootroot409610-2815:11test5

[root@localhosttest]#cdtest5/

[root@localhosttest5]#ll

-rw-r–r–1rootroot010-2814:46log5-1.log

-rw-r–r–1rootroot010-2814:46log5-2.log

-rw-r–r–1rootroot010-2814:46log5-3.log

-rw-r–r–1rootroot010-2814:48log.log

drwxrwxrwx2rootroot409610-2814:47test3

目标目录不存在是:

[root@localhosttest]#cp-atest3test4

[root@localhosttest]#ll

-rw-r–r–1rootroot010-2814:48log.log

drwxr-xr-x6rootroot409610-2701:58scf

drwxrwxrwx2rootroot409610-2814:47test3

drwxrwxrwx2rootroot409610-2814:47test4

drwxr-xr-x3rootroot409610-2815:11test5

[root@localhosttest]#

说明:

注意目标目录存在与否结果是不一样的。目标目录存在时,整个源目录被复制到目标目录里面。

实例四:复制的log.log建立一个连结档log_link.log

命令:

cp-slog.loglog_link.log

输出:

[root@localhosttest]#cp-slog.loglog_link.log

[root@localhosttest]#ll

lrwxrwxrwx1rootroot710-2815:18log_link.log->log.log

-rw-r–r–1rootroot010-2814:48log.log

drwxr-xr-x6rootroot409610-2701:58scf

drwxrwxrwx2rootroot409610-2814:47test3

drwxrwxrwx2rootroot409610-2814:47test4

drwxr-xr-x3rootroot409610-2815:11test5

说明:

那个log_link.log是由-s的参数造成的,建立的是一个『快捷方式』,所以您会看到在文件的最右边,会显示这个文件是『连结』到哪里去的!

相关内容