河南网站建设推广公司,怎么建设网站啊,网络工程师证书难考吗,wordpress导航框架cp命令用来复制文件或者目录#xff0c;是Linux系统中最常用的命令之一。一般情况下#xff0c;shell会设置一个别名#xff0c;在命令行下复制文件时#xff0c;如果目标文件已经存在#xff0c;就会询问是否覆盖#xff0c;不管你是否使用-i参数。但是如果是在shell脚本…cp命令用来复制文件或者目录是Linux系统中最常用的命令之一。一般情况下shell会设置一个别名在命令行下复制文件时如果目标文件已经存在就会询问是否覆盖不管你是否使用-i参数。但是如果是在shell脚本中执行cp时没有-i参数时不会询问是否覆盖。这说明命令行和shell脚本的执行方式有些不同。 1命令格式用法 cp [选项]... [-T] 源 目的 或cp [选项]... 源... 目录 或cp [选项]... -t 目录 源...2命令功能将源文件复制至目标文件或将多个源文件复制至目标目录。3命令参数-a, --archive 等于-dR --preserveall --backup[CONTROL 为每个已存在的目标文件创建备份-b 类似--backup 但不接受参数 --copy-contents 在递归处理是复制特殊文件内容-d 等于--no-dereference --preservelinks-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命令实例实例一复制单个文件到目标目录文件在目标文件中不存在命令cp log.log test5输出[rootlocalhost test]# cp log.log test5[rootlocalhost test]# ll-rw-r--r-- 1 root root 0 10-28 14:48 log.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 10-28 14:47 test3drwxr-xr-x 2 root root 4096 10-28 14:53 test5[rootlocalhost test]# cd test5[rootlocalhost 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说明在没有带-a参数时两个文件的时间是不一样的。在带了-a参数时两个文件的时间是一致的。 实例二目标文件存在时会询问是否覆盖命令cp log.log test5输出[rootlocalhost test]# cp log.log test5cp是否覆盖“test5/log.log”? n[rootlocalhost test]# cp -a log.log test5cp是否覆盖“test5/log.log”? y[rootlocalhost test]# cd test5/[rootlocalhost 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说明目标文件存在时会询问是否覆盖。这是因为cp是cp -i的别名。目标文件存在时即使加了-f标志也还会询问是否覆盖。实例三复制整个目录命令输出目标目录存在时[rootlocalhost test]# cp -a test3 test5 [rootlocalhost test]# ll-rw-r--r-- 1 root root 0 10-28 14:48 log.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 10-28 14:47 test3drwxr-xr-x 3 root root 4096 10-28 15:11 test5[rootlocalhost test]# cd test5/[rootlocalhost 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.logdrwxrwxrwx 2 root root 4096 10-28 14:47 test3目标目录不存在是[rootlocalhost test]# cp -a test3 test4[rootlocalhost test]# ll-rw-r--r-- 1 root root 0 10-28 14:48 log.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 10-28 14:47 test3drwxrwxrwx 2 root root 4096 10-28 14:47 test4drwxr-xr-x 3 root root 4096 10-28 15:11 test5[rootlocalhost test]#说明注意目标目录存在与否结果是不一样的。目标目录存在时整个源目录被复制到目标目录里面。 实例四复制的 log.log 建立一个连结档 log_link.log命令cp -s log.log log_link.log输出[rootlocalhost test]# cp -s log.log log_link.log[rootlocalhost test]# lllrwxrwxrwx 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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 10-28 14:47 test3drwxrwxrwx 2 root root 4096 10-28 14:47 test4drwxr-xr-x 3 root root 4096 10-28 15:11 test5说明那个 log_link.log 是由 -s 的参数造成的建立的是一个『快捷方式』所以您会看到在文件的最右边会显示这个文件是『连结』到哪里去的