您的位置:首页 > 其它

文件的复制和移动命令:cp和mv

2016-10-17 15:04 316 查看
cp:copy复制

命令作用:

复制和移动文件;

2.命令格式:
cp [options] source...source destination //源可以有多个,目标只能有一个;

3.参数说明:
-f:force强行,强行复制不论目的文件或目录是否存在;

-i:interactive交互,覆盖之前先询问用户;

-P:preserve,保留源文件或目录的属性,包括所有者、所属组、权限与时间戳;

-r:recursive递归,递归复制,连同指定目录下的子目录和文件;

-R:recursive同-r;

-v:verbose详细,显示命令执行过程;

-a:archive档案,归档复制,保留所有属性,常用于备份,等同于-dpR;

4.范例:

[root@localhost ~]# cp /etc/inittab ./
[root@localhost ~]# ll
total 108
-rw-------. 1 root root 1645 Mar 16 2016 anaconda-ks.cfg
-rw-r--r--. 1 root root 884 Oct 16 21:13 inittab
-rw-r--r--. 1 root root 50101 Mar 16 2016 install.log
-rw-r--r--. 1 root root 10608 Mar 16 2016 install.log.syslog

[root@localhost ~]# cp -i /etc/inittab ./
cp: overwrite `./inittab'? y
[root@localhost ~]# ll
total 108
-rw-------. 1 root root 1645 Mar 16 2016 anaconda-ks.cfg
-rw-r--r--. 1 root root 884 Oct 16 21:14 inittab
-rw-r--r--. 1 root root 50101 Mar 16 2016 install.log
-rw-r--r--. 1 root root 10608 Mar 16 2016 install.log.syslog

[root@localhost ~]# cp -r /etc/ ./
[root@localhost ~]# ll
total 120
-rw-------. 1 root root 1645 Mar 16 2016 anaconda-ks.cfg
drwxr-xr-x. 120 root root 12288 Oct 16 21:18 etc
-rw-r--r--. 1 root root 884 Oct 16 21:15 inittab
-rw-r--r--. 1 root root 50101 Mar 16 2016 install.log
-rw-r--r--. 1 root root 10608 Mar 16 2016 install.log.syslog

[root@localhost ~]# cp -v /etc/passwd .
`/etc/passwd' -> `./passwd'
[root@localhost ~]# ll
total 124
-rw-------. 1 root root 1645 Mar 16 2016 anaconda-ks.cfg
drwxr-xr-x. 120 root root 12288 Oct 16 21:18 etc
-rw-r--r--. 1 root root 884 Oct 16 21:15 inittab
-rw-r--r--. 1 root root 50101 Mar 16 2016 install.log
-rw-r--r--. 1 root root 10608 Mar 16 2016 install.log.syslog
-rw-r--r--. 1 root root 1676 Oct 16 21:19 passwd

[root@localhost ~]# cp -a /etc/sudo.conf ./
cp: overwrite `./sudo.conf'? y
[root@localhost ~]# ll ./sudo.conf /etc/sudo.conf
-rw-r-----. 1 root root 1786 Sep 25 2012 ./sudo.conf
-rw-r-----. 1 root root 1786 Sep 25 2012 /etc/sudo.conf

mv:move移动

命令作用:

用来移动文件或重命名文件,经常用来备份文件或目录;

2.命令格式:
mv [options] source...source destination

3.参数说明:
-f:fource强行,如果目标已经存在则不会询问直接覆盖;

-i:interactive交互,覆盖前进行询问;

-u:update更新,若目标文件已经存在且源文件比较新才会更新;

-t:target目标,指定mv的目标目录,该选项适用于移动多个源文件到一个目标中,此时目标目录在前,源文件在后;

4.范例:

[root@localhost ~]# mv -v ./inittab /tmp
`./inittab' -> `/tmp/inittab'
[root@localhost ~]# ll /tmp
total 40
-rw-r--r--. 1 root root 884 Oct 16 21:15 inittab
drwx------. 2 root root 4096 Oct 16 20:46 keyring-U00hv0
drwx------. 2 root root 4096 Oct 15 17:18 keyring-UTq8Qb
drwx------. 2 root root 4096 Oct 16 20:47 orbit-root
drwx------. 2 cgy cgy 4096 Mar 16 2016 pulse-FsSpAseXSLkn
drwx------. 2 root root 4096 Oct 16 20:46 pulse-TuT0VNMAElV9
drwx------. 2 root root 4096 Oct 16 20:46 ssh-AhhoWr1925
drwx------. 2 root root 4096 Oct 15 19:15 virtual-root.2bgEqz
drwx------. 2 root root 4096 Oct 16 20:46 virtual-root.NWQZFc
drwx------. 2 root root 4096 Oct 15 17:18 virtual-root.wG1kVw

[root@localhost ~]# mv -i inittab /tmp
mv: overwrite `/tmp/inittab'? y

[root@localhost ~]# cp /etc/inittab .
[root@localhost ~]# mv -f inittab /tmp
[root@localhost ~]# ll /tmp
total 40
-rw-r--r--. 1 root root 884 Oct 16 21:40 inittab
drwx------. 2 root root 4096 Oct 16 20:46 keyring-U00hv0
drwx------. 2 root root 4096 Oct 15 17:18 keyring-UTq8Qb
drwx------. 2 root root 4096 Oct 16 20:47 orbit-root
drwx------. 2 cgy cgy 4096 Mar 16 2016 pulse-FsSpAseXSLkn
drwx------. 2 root root 4096 Oct 16 20:46 pulse-TuT0VNMAElV9
drwx------. 2 root root 4096 Oct 16 20:46 ssh-AhhoWr1925
drwx------. 2 root root 4096 Oct 15 19:15 virtual-root.2bgEqz
drwx------. 2 root root 4096 Oct 16 20:46 virtual-root.NWQZFc
drwx------. 2 root root 4096 Oct 15 17:18 virtual-root.wG1kVw

[root@localhost ~]# mv -t /tmp ./sudo.conf
[root@localhost ~]# ll /tmp
total 48
-rw-r--r--. 1 root root 1676 Oct 16 21:19 a
-rw-r--r--. 1 root root 884 Oct 16 21:40 inittab
drwx------. 2 root root 4096 Oct 16 20:46 keyring-U00hv0
drwx------. 2 root root 4096 Oct 15 17:18 keyring-UTq8Qb
drwx------. 2 root root 4096 Oct 16 20:47 orbit-root
drwx------. 2 cgy cgy 4096 Mar 16 2016 pulse-FsSpAseXSLkn
drwx------. 2 root root 4096 Oct 16 20:46 pulse-TuT0VNMAElV9
drwx------. 2 root root 4096 Oct 16 20:46 ssh-AhhoWr1925
-rw-r-----. 1 root root 1786 Sep 25 2012 sudo.conf
drwx------. 2 root root 4096 Oct 15 19:15 virtual-root.2bgEqz
drwx------. 2 root root 4096 Oct 16 20:46 virtual-root.NWQZFc
drwx------. 2 root root 4096 Oct 15 17:18 virtual-root.wG1kVw
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mv 复制和移动