网站建设开发哪家质量好百度sem推广具体做什么
cp命令
cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录。
它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下。
cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文件参数必须是一个已经存在的目录,否则将出现错误。
语法
cp(选项)(参数)
选项
-a:此参数的效果和同时指定"-dpR"参数相同;
-d:当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录;
-f:强行复制文件或目录,不论目标文件或目录是否已存在;
-i:覆盖既有文件之前先询问用户;
-l:对源文件建立硬连接,而非复制文件;
-p:保留源文件或目录的属性;
-R/r:递归处理,将指定目录下的所有文件与子目录一并处理;
-s:对源文件建立符号连接,而非复制文件;
-u:使用这项参数后只会在源文件的更改时间较目标文件更新时或是名称相互对应的目标文件并不存在时,才复制文件;
-S:在备份文件时,用指定的后缀“SUFFIX”代替文件的默认后缀;
-b:覆盖已存在的文件目标前将目标文件备份;
-v:详细显示命令执行的操作。
参数
- 源文件:制定源文件列表。默认情况下,cp命令不能复制目录,如果要复制目录,则必须使用
-R
选项; - 目标文件:指定目标文件。当“源文件”为多个文件时,要求“目标文件”为指定的目录。
实例
- 单个文件拷贝:
#将文件file复制到目录/mnt/e/tmp/shell/scripts下:
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# pwd
/mnt/e/tmp/shell
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# ll
total 0
drwxrwxrwx 0 root root 512 Sep 3 22:15 ./
drwxrwxrwx 0 root root 512 Sep 3 21:57 ../
-rwxrwxrwx 1 root root 0 Sep 3 22:15 file1*
drwxrwxrwx 0 root root 512 Sep 3 22:15 scripts/
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# cp file1 ./scripts/
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# ll
total 0
drwxrwxrwx 0 root root 512 Sep 3 22:15 ./
drwxrwxrwx 0 root root 512 Sep 3 21:57 ../
-rwxrwxrwx 1 root root 0 Sep 3 22:15 file1*
drwxrwxrwx 0 root root 512 Sep 3 22:16 scripts/
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# cd scripts/
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell/scripts# ll
total 0
drwxrwxrwx 0 root root 512 Sep 3 22:16 ./
drwxrwxrwx 0 root root 512 Sep 3 22
#将文件file复制到目录/mnt/e/tmp/shell/scripts下,并改名为file2:
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# cp file1 ./scripts/file2
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# cd scripts/
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell/scripts# ll
total 0
drwxrwxrwx 0 root root 512 Sep 3 22:20 ./
drwxrwxrwx 0 root root 512 Sep 3 22:15 ../
-rwxrwxrwx 1 root root 0 Sep 3 22:16 file1*
-rwxrwxrwx 1 root root 0 Sep 3 22:20 file2*
- 多个文件拷贝:
#新建file3,file4文件并拷贝到scripts目录下:
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# vi file3
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# vi file4
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# cp file3 file4 ./scripts/
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# cd scripts/
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell/scripts# ll
total 0
drwxrwxrwx 0 root root 512 Sep 3 22:24 ./
drwxrwxrwx 0 root root 512 Sep 3 22:24 ../
-rwxrwxrwx 1 root root 0 Sep 3 22:16 file1*
-rwxrwxrwx 1 root root 0 Sep 3 22:20 file2*
-rwxrwxrwx 1 root root 0 Sep 3 22:24 file3*
-rwxrwxrwx 1 root root 0 Sep 3 22:24 file4*
- 目录拷贝:
#将目录dir1下所有文件及子目录都拷贝到scripts目录下,
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# cp -r ./dir1 ./scripts/
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell# cd scripts/
root@DESKTOP-MVB6M1F:/mnt/e/tmp/shell/scripts# ll
total 0
drwxrwxrwx 0 root root 512 Sep 3 22:27 ./
drwxrwxrwx 0 root root 512 Sep 3 22:26 ../
drwxrwxrwx 0 root root 512 Sep 3 22:27 dir1/
-rwxrwxrwx 1 root root 0 Sep 3 22:16 file1*
-rwxrwxrwx 1 root root 0 Sep 3 22:20 file2*
-rwxrwxrwx 1 root root 0 Sep 3 22:24 file3*
-rwxrwxrwx 1 root root 0 Sep 3 22:24 file4*
注意事项:
- 如果使用cp命令把一个文件复制到一个目标文件中,而目标文件已经存在,那么,该目标文件的内容将被破坏。
- cp命令中所有参数既可以是绝对路径名,也可以是相对路径名。通常会用到点
.
或点点..
的形式。 - cp命令不能创建目录,所有目标文件指定的目录必须是己经存在的,如果没有文件复制的权限,则系统会显示出错信息。
参考:http://man.linuxde.net/cp