cp命令--文件复制
目录和文件的复制命令
cp命令语法和参数:
cp [OPTION]...SOURCE...DIRECTORY
-a | ALL | 等价于dpR |
-d | 复制符号链接源文件时,目标文件也将创建符号链接执行源文件的原始文件 | |
-p | preserve | 保留文件属性,属主,属组,权限和时间 |
-r | recursive | 递归目录下的子文件 |
示例:
[root@localhost ~]# ll /etc/services
-rw-r--r-- 1 root root 362031 2006-02-23 /etc/services
[root@localhost ~]# stat /etc/services
File: “/etc/services”
Size: 362031 Blocks: 728 IO Block: 4096 一般文件
Device: fd00h/64768d Inode: 2282597 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-11-11 11:20:33.000000000 +0800
Modify: 2006-02-23 21:09:23.000000000 +0800
Change: 2014-10-21 15:21:26.000000000 +0800
[root@localhost ~]# cp -a /etc/services /tmp/services
[root@localhost ~]# ll /tmp/services
-rw-r--r-- 1 root root 362031 2006-02-23 /tmp/services
[root@localhost ~]# stat /tmp/services
File: “/tmp/services”
Size: 362031 Blocks: 728 IO Block: 4096 一般文件
Device: fd00h/64768d Inode: 1434761 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-11-11 11:20:33.000000000 +0800
Modify: 2006-02-23 21:09:23.000000000 +0800
Change: 2014-11-11 11:44:39.000000000 +0800
说明:文件的所有属性和权限都拷贝过来了,并且ctime发生了改变
扩展:源文件复制到目标目录中提示文件已经存在,如何直接覆盖掉目标文件?而让系统不提示覆盖呢?
[root@localhost ~]# cp -a /etc/services /tmp/
cp:是否覆盖“/tmp/services”?
在脚本中是不能人工参与交互,解决方法:
[root@localhost ~]# \cp -a /etc/services /tmp/services
[root@localhost ~]# unalias cp
[root@localhost ~]# cp -a /etc/services /tmp/
[root@localhost ~]# /bin/cp -a /etc/services /tmp/services
说明:默认情况下,cp= cp -i 然而-i参数是提示覆盖的意思
本文出自 “linux运维分享” 博客,请务必保留此出处http://liangey.blog.51cto.com/9097868/1575258
原文地址:http://liangey.blog.51cto.com/9097868/1575258