共计 2486 个字符,预计需要花费 7 分钟才能阅读完成。
提醒:本文最后更新于2019-08-03 15:00,文中所关联的信息可能已发生改变,请知悉!
[v_act]格式:cp [OPTION]… SOURCE… DIRECTORY
-R:递归拷贝目录(-r同样效果)
-a(-dR):拷贝目录文件时保留源目录文件的原有属性及其它特性;一般用于归档(备份)
-f:强制拷贝;当目标位置有同名文件直接覆盖操作;默认cp命令是有别名(alias cp=’cp -i’)的,即使用参数(-f)也无法强制覆盖文件;解决方法就是无需此参数(-f)直接在命令cp前加反斜杠(\cp)即可。
-v:详细显示命令执行的操作
[/v_act]
[root@localhost tmp]# ll -h /tmp/1/2/
total 0
[root@localhost tmp]# cp /tmp/1/test.txt /tmp/1/2/
[root@localhost tmp]# ll -h /tmp/1/2/
total 0
-rw-r--r-- 1 root root 0 Feb 12 20:48 test.txt
[root@localhost tmp]#
将目录/tmp/1下的某文件拷贝到目录/tmp/1/2下并重命名
[root@localhost tmp]# ll -h /tmp/1/2/
total 0
[root@localhost tmp]# cp /tmp/1/test.txt /tmp/1/2/test1.txt
[root@localhost tmp]# ll -h /tmp/1/2/
total 0
-rw-r--r-- 1 root root 0 Feb 12 21:00 test1.txt
[root@localhost tmp]#
[root@localhost tmp]# ll /tmp/qwe/
total 0
[root@localhost tmp]# cp -R /tmp/1/ /tmp/qwe/
[root@localhost tmp]# tree -d /tmp/
/tmp/
├── 1
│ └── 2
│ └── 3
│ └── 4
│ └── 5
├── qwe
│ └── 1
│ └── 2
│ └── 3
│ └── 4
│ └── 5
├── rty
├── systemd-private-67f03791c3f34110b44cc9fe6c96389d-vmtoolsd.service-Nfell2
│ └── tmp
└── z
└── x
└── c
└── v
└── b
19 directories
[root@localhost tmp]#
将tmp下的目录1拷贝到目录/tmp/z下并重命名
[root@localhost tmp]# ll /tmp/z
total 0
drwxr-xr-x 3 root root 14 Nov 28 2017 x
[root@localhost tmp]# cp -R /tmp/1/ /tmp/z/poi
[root@localhost tmp]# tree -d /tmp/
/tmp/
├── 1
│ └── 2
│ └── 3
│ └── 4
│ └── 5
├── qwe
│ └── 1
│ └── 2
│ └── 3
│ └── 4
│ └── 5
├── rty
├── systemd-private-67f03791c3f34110b44cc9fe6c96389d-vmtoolsd.service-Nfell2
│ └── tmp
└── z
├── poi
│ └── 2
│ └── 3
│ └── 4
│ └── 5
└── x
└── c
└── v
└── b
24 directories
[root@localhost tmp]#
[root@localhost tmp]# cp -f test*.txt testdir/
cp: overwrite 'testdir/test10.txt'? y
cp: overwrite 'testdir/test1.txt'? ^C
[root@localhost tmp]# \cp test*.txt testdir/
[root@localhost tmp]# \cp -v test*.txt testdir/
'test10.txt' -> 'testdir/test10.txt'
'test1.txt' -> 'testdir/test1.txt'
'test2.txt' -> 'testdir/test2.txt'
'test3.txt' -> 'testdir/test3.txt'
'test4.txt' -> 'testdir/test4.txt'
'test5.txt' -> 'testdir/test5.txt'
'test6.txt' -> 'testdir/test6.txt'
'test7.txt' -> 'testdir/test7.txt'
'test8.txt' -> 'testdir/test8.txt'
'test9.txt' -> 'testdir/test9.txt'
[v_blue]SRC DEST
SRC是文件:
目标不存在:新建目标文件并将源文件内容填充至目标文件中
目标存在:
目标是文件:将源文件内容覆盖至目标文件中
目标是目录:单源文件(在目标目录中新建与原文件同名的文件并将源文件内容填充至新文件中);多源文件(分别复制每个文件至目标目录中并保持原名)
SRC是目录:拷贝目录中的内容(DirName/*) 拷贝整个目录(DirName/)
目标不存在:新建目标目录并将源目录内容拷贝至目标目录中
目标存在:
目标是文件:报错
目标是目录:将源目录内容拷贝至目标目录中[/v_blue]
[v_error]Ubuntu系统请配合sudo使用(sudo cp)[/v_error]