cp和mv、scp的问题 分享
转至:http://ctchina.blog.sohu.com/110617060.html
?
一些个别测试和常规测试:
测试平台:Red Hat Enterprise Linux Server release 5.2 (Tikanga)
一、cp的测试
#############
?(1) /home/test/t1 下的内容
==========================
在t1目录下有(a.txt、.b、c、d. )4个文件和目录t_t1及其下的文件,如下所示:
[root@rhdb1 home]# ls -laR /home/test/t1
/home/test/t1:
total 28
drwxr-xr-x 3 root root 4096 Feb 17 21:05 .
drwxr-xr-x 3 root root 4096 Feb 17 21:23 ..
-rw-r--r-- 1 root root? 557 Feb 17 20:37 a.txt
-rw-r--r-- 1 root root?? 29 Feb 17 20:37 .b
-rw-r--r-- 1 root root?? 29 Feb 17 20:58 c
-rw-r--r-- 1 root root?? 29 Feb 17 20:58 d.
drwxr-xr-x 2 root root 4096 Feb 17 21:05 t_t1
/home/test/t1/t_t1:
total 24
drwxr-xr-x 2 root root 4096 Feb 17 21:05 .
drwxr-xr-x 3 root root 4096 Feb 17 21:05 ..
-rw-r--r-- 1 root root? 557 Feb 17 20:38 aa.txt
-rw-r--r-- 1 root root?? 29 Feb 17 20:38 .bb
-rw-r--r-- 1 root root?? 29 Feb 17 20:59 cc
-rw-r--r-- 1 root root?? 29 Feb 17 20:59 dd.
[root@rhdb1 home]# ls -aR /home/test/t1
/home/test/t1:
.? ..? a.txt? .b? c? d.? t_t1
/home/test/t1/t_t1:
.? ..? aa.txt? .bb? cc? dd.
[root@rhdb1 home]#
(1) 拷贝 cp -ar /home/test/t1/.? /home/test/t2/
===============================================
[root@rhdb1 home]# mkdir /home/test/t2
[root@rhdb1 home]# cp -ar /home/test/t1/.? /home/test/t2/
[root@rhdb1 home]# ls -aR /home/test/t2/
/home/test/t2/:
.? ..? a.txt? .b? c? d.? t_t1
/home/test/t2/t_t1:
.? ..? aa.txt? .bb? cc? dd.
[root@rhdb1 home]#
Don't forget the dot (.), it makes sure to copy hidden files.
全部文件都拷贝过去了
注意:当使用.时,必须加参数,否则将报如下错误:
cp: omitting directory `/home/test/t1/.'
(3) 拷贝 cp -ar /home/test/t1/*? /home/test/t3/
===============================================
[root@rhdb1 home]# mkdir /home/test/t3
[root@rhdb1 home]# cp -ar /home/test/t1/*? /home/test/t3/
[root@rhdb1 home]# ls -aR /home/test/t3/
/home/test/t3/:
.? ..? a.txt? c? d.? t_t1
/home/test/t3/t_t1:
.? ..? aa.txt? .bb? cc? dd.
[root@rhdb1 home]#
/home/test/t1 下的隐藏文件没有拷过去,而子目录t_t1下的内容全部拷过去了。
(4) 拷贝 cp -ar /home/test/t1/*.*? /home/test/t4/
================================================
[root@rhdb1 home]# mkdir /home/test/t4
[root@rhdb1 home]# cp -ar /home/test/t1/*.*? /home/test/t4/
[root@rhdb1 home]# ls -aR /home/test/t4/
/home/test/t4/:
.? ..? a.txt? d.
[root@rhdb1 home]#
只拷过去了2个文件a.txt和d.
(5) 拷贝 cp -ar /home/test/t1/.*?? /home/test/t5/
================================================
注意:这条命令不宜使用,以下是相关说明。
[root@rhdb1 home]# mkdir /home/test/t5
[root@rhdb1 home]# cp -ar /home/test/t1/.*?? /home/test/t5/
cp: will not create hard link `/home/test/t5/t1' to directory `/home/test/t5/.'
cp: cannot copy a directory, `/home/test/t1/..', into itself, `/home/test/t5/'
cp: cannot copy a directory, `/home/test/t1/..', into itself, `/home/test/t5/'
cp: cannot copy a directory, `/home/test/t1/..', into itself, `/home/test/t5/'
cp: cannot copy a directory, `/home/test/t1/..', into itself, `/home/test/t5/'
cp: cannot copy a directory, `/home/test/t1/..', into itself, `/home/test/t5/'
cp: overwrite `/home/test/t5/.b'? n
[root@rhdb1 home]# ls -aR /home/test/t5
/home/test/t5:
.? ..? a.txt? .b? c? d.? t2? t3? t4? t5? t_t1
/home/test/t5/t2:
.? ..? a.txt? .b? c? d.? t_t1
/home/test/t5/t2/t_t1:
.? ..? aa.txt? .bb? cc? dd.
/home/test/t5/t3:
.? ..? a.txt? c? d.? t_t1
/home/test/t5/t3/t_t1:
.? ..? aa.txt? .bb? cc? dd.
/home/test/t5/t4:
.? ..? a.txt? d.
/home/test/t5/t5:
.? ..? a.txt? .b? c? d.
/home/test/t5/t_t1:
.? ..? aa.txt? .bb? cc? dd.
[root@rhdb1 home]#
相当于如下的命令:
cp -i -ar /home/test/t1/. /home/test/t1/.. /home/test/t1/.b /home/test/t5/
1)
[root@rhdb1 home]# cp -ar /home/test/t1/.? /usr/local/nagios/.?? /home/test/ttt/
[root@rhdb1 home]# ls /home/test/ttt
a.txt? bin? c? d.? etc? libexec? sbin? share? t_t1? var
[root@rhdb1 home]#
意思:将/home/test/t1和/usr/local/nagios/下的内容拷贝到/home/test/ttt/
2)
[root@rhdb1 home]# cp -ar /home/test/t1?? /usr/local/nagios?? /home/test/yzhq
[root@rhdb1 home]# ls /home/test/yzhq
nagios? t1
意思:将/home/test/t1和/usr/local/nagios/两个目录及其下内容拷贝到/home/test/ttt/
(6) 拷贝cp -ar /home/test/t1? /home/test/t6/
============================================
[root@rhdb1 home]# mkdir /home/test/t6
[root@rhdb1 home]# cp -ar /home/test/t1? /home/test/t6/
[root@rhdb1 home]# ls -aR /home/test/t6/
/home/test/t6/:
.? ..? t1
/home/test/t6/t1:
.? ..? a.txt? .b? c? d.? t_t1
/home/test/t6/t1/t_t1:
.? ..? aa.txt? .bb? cc? dd.
[root@rhdb1 home]# ls /home/test/t6
t1
[root@rhdb1 home]#
把目录/home/test/t1和其下内容拷贝到了/home/test/t6/下。
(7) 拷贝 cp -ar /home/test/t1? /home/test/t1.bak
=================================================
注意:目标目录t1.bak不存在
[root@rhdb1 home]# cp -ar /home/test/t1? /home/test/t1.bak
[root@rhdb1 home]# ls -aR /home/test/t1.bak/
/home/test/t1.bak/:
.? ..? a.txt? .b? c? d.? t_t1
/home/test/t1.bak/t_t1:
.? ..? aa.txt? .bb? cc? dd.
[root@rhdb1 home]#
内容全部拷贝过去了!
二、mv的测试
############
[root@db106 ~]# mkdir bbb2
(1) mv ttt1/. bbb2
[root@db106 ~]# mv ttt1/. bbb2
mv: cannot move `ttt1/.' to `bbb2/.': Device or resource busy
错误
(2) mv ttt1/*? bbb2/
[root@db106 ~]# mv ttt1/*? bbb2/
[root@db106 ~]# ls -a bbb2
.? ..? a.txt
[root@db106 ~]# ls -a ttt1
.? ..? .c.txt
隐藏文件没有移动过去
(3) mv ttt1/*.* bbb2/
[root@db106 ~]# mv ttt1/*.* bbb2/
mv: cannot stat `ttt1/*.*': No such file or directory
错误
(4) mv ttt1/.* bbb2/
[root@db106 ~]# mv ttt1/.* bbb2/
mv: cannot move `ttt1/.' to `bbb2/.': Device or resource busy
mv: overwrite `bbb2/..'? y
mv: cannot remove `ttt1/..': Is a directory
[root@db106 ~]#
错误
(5) mv ttt1/*. bbb2
[root@db106 ~]# mv ttt1/*. bbb2
mv: cannot stat `ttt1/*.': No such file or directory
错误
在ttt1目录内存在m.文件的情况下,不会报错,但只会把m.文件移动过去。
[root@db106 ~]# mv ttt1/*. bbb2
[root@db106 ~]# ls -a bbb2
.? ..? m.
[root@db106 ~]#
三、scp的测试
#############
(1) scp 不支持.?
[root@test local]# scp 192.168.0.31:/home/zhyhl/software/.?? software
root@211.88.12.28's password:
scp: /home/zhyhl/software/.: not a regular file
(2) scp 不支持目标目录名不存在的现象
[root@test local]# scp 192.168.0.31:/home/zhyhl/software/*?? software
root@211.88.12.28's password:
jdk-6u10-linux-x64.bin??????????????????????????????????????????????????????????????????????????? 100%?? 69MB?
69.4MB/s?? 00:01???
mysql-5.0.67.tar.gz?????????????????????????????????????????????????????????????????????????????? 100%?? 27MB?
27.1MB/s?? 00:01???
resin-3.1.7a.tar.gz?????????????????????????????????????????????????????????????????????????????? 100%?? 10MB??
9.8MB/s?? 00:00???
[root@web206 local]#
[root@test local]# md5sum software
ef1626811a40a15438dfa4dcebf6e2fa? software
[root@gerenadc software]# md5sum resin-3.1.7a.tar.gz
ef1626811a40a15438dfa4dcebf6e2fa? resin-3.1.7a.tar.gz
要拷贝到的目录必须为已存在的,否则只会变成一个software,从上面的输出即可得知
(3) 正确的拷贝方法,此时squid为不存在
[root@test local]# scp -r? 192.168.0.31:/usr/local/squid?? squid