每天一命令(7)cp (copy files and directories)

cp (copy files and directories)

man cp 查看更多命令

cp  [-aifprud] source target
        -a : 相当于 -pdr
        -d : 源文件为链接文件,则复制过去也是链接文件,而不是源文档
        -i : 复制的文件的,覆盖文件的时候,提示
        -f : 强制,如果目标文件已经存在且无法开启,则移除后在尝试
        -p : 连同档案的属性一起复制过去,而非使用默认的属性
        -r : 递归复制,用于复制目录
        -u : 如果目标文件已近存在,则目标文件要比源文件要旧的时候在复制。
cp 文件 目标目录 只是复制文件
root@guofeng  ~
# ll
total 84
-rw-------. 1 root root  2404 Nov  8  2014 anaconda-ks.cfg
-rw-r--r--  1 root root     0 Jun  6 18:47 b-ba-c.txt
-rw-r--r--  1 root root     0 Jun 11 20:48 b.txt
drwxr-xr-x  2 root root  4096 Jun 11 23:19 css
-rw-r--r--  1 root root   868 Jun  9 21:06 dump.rdb
-rw-r--r--  1 root root     0 May 17 07:00 EOF
-rw-r--r--  1 root root     0 Jun 11 23:20 Hello.java
drwxr-xr-x  2 root root  4096 Jun 11 23:20 html
-rw-r--r--  1 root root     0 Jun 11 23:20 index.css
-rw-r--r--. 1 root root 39004 Nov  8  2014 install.log
-rw-r--r--. 1 root root  9154 Nov  8  2014 install.log.syslog
drwxr-xr-x  2 root root  4096 Jun 11 23:19 java
-rw-r--r--  1 root root     0 Jun 12  2016 mm.txt
drwxr-xr-x  2 root root  4096 Jun 11 23:19 test
-rw-r--r--  1 root root     0 Jun 11 20:06 test.txt
drwxr-xr-x  2 root root  4096 Jun 11 23:20 web

root@guofeng  ~
# cp b.txt test

root@guofeng  ~
# ll
total 84
-rw-------. 1 root root  2404 Nov  8  2014 anaconda-ks.cfg
-rw-r--r--  1 root root     0 Jun  6 18:47 b-ba-c.txt
-rw-r--r--  1 root root     0 Jun 11 20:48 b.txt
drwxr-xr-x  2 root root  4096 Jun 11 23:19 css
-rw-r--r--  1 root root   868 Jun  9 21:06 dump.rdb
-rw-r--r--  1 root root     0 May 17 07:00 EOF
-rw-r--r--  1 root root     0 Jun 11 23:20 Hello.java
drwxr-xr-x  2 root root  4096 Jun 11 23:20 html
-rw-r--r--  1 root root     0 Jun 11 23:20 index.css
-rw-r--r--. 1 root root 39004 Nov  8  2014 install.log
-rw-r--r--. 1 root root  9154 Nov  8  2014 install.log.syslog
drwxr-xr-x  2 root root  4096 Jun 11 23:19 java
-rw-r--r--  1 root root     0 Jun 12  2016 mm.txt
drwxr-xr-x  2 root root  4096 Jun 11 23:21 test
-rw-r--r--  1 root root     0 Jun 11 20:06 test.txt
drwxr-xr-x  2 root root  4096 Jun 11 23:20 web

root@guofeng  ~
# cd test

root@guofeng  ~/test
# ll
total 0
-rw-r--r-- 1 root root 0 Jun 11 23:21 b.txt

cp -r 源码目录 目标目录 复制目录 cp -a

root@guofeng  ~
# ll
total 84
-rw-------. 1 root root  2404 Nov  8  2014 anaconda-ks.cfg
-rw-r--r--  1 root root     0 Jun  6 18:47 b-ba-c.txt
-rw-r--r--  1 root root     0 Jun 11 20:48 b.txt
drwxr-xr-x  2 root root  4096 Jun 11 23:19 css
-rw-r--r--  1 root root   868 Jun  9 21:06 dump.rdb
-rw-r--r--  1 root root     0 May 17 07:00 EOF
-rw-r--r--  1 root root     0 Jun 11 23:20 Hello.java
drwxr-xr-x  3 root root  4096 Jun 11 23:32 html
-rw-r--r--  1 root root     0 Jun 11 23:20 index.css
-rw-r--r--. 1 root root 39004 Nov  8  2014 install.log
-rw-r--r--. 1 root root  9154 Nov  8  2014 install.log.syslog
drwxr-xr-x  2 root root  4096 Jun 11 23:19 java
-rw-r--r--  1 root root     0 Jun 12  2016 mm.txt
drwxr-xr-x  2 root root  4096 Jun 11 23:21 test
-rw-r--r--  1 root root     0 Jun 11 20:06 test.txt
drwxr-xr-x  2 root root  4096 Jun 11 23:20 web

root@guofeng  ~
# cp -r css html/

root@guofeng  ~
# ls -l html
total 4
drwxr-xr-x 2 root root 4096 Jun 11 23:32 css

root@guofeng  ~
# cp -a test html/

root@guofeng  ~
# ls -l html
total 8
drwxr-xr-x 2 root root 4096 Jun 11 23:32 css
drwxr-xr-x 2 root root 4096 Jun 11 23:21 test

复制文件,如果有文件,提示是否覆盖 cp -i

root@guofeng  ~
# cp -i Hello.java web/

root@guofeng  ~
# cp -i Hello.java web/
cp: overwrite `web/Hello.java'? y
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是 `cp --help` 命令的输出: ``` Usage: cp [OPTION]... [-T] SOURCE DEST or: cp [OPTION]... SOURCE... DIRECTORY or: cp [OPTION]... -t DIRECTORY SOURCE... Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY. Mandatory arguments to long options are mandatory for short options too. -a, --archive same as -dR --preserve=all --attributes-only don't copy the file data, just the attributes --backup[=CONTROL] make a backup of each existing destination file -b like --backup but does not accept an argument --copy-contents copy contents of special files when recursive -d same as --no-dereference --preserve=links -f, --force if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used) -i, --interactive prompt before overwrite (overrides a previous -n option) -H follow command-line symbolic links in SOURCE -l, --link hard link files instead of copying -L, --dereference always follow symbolic links in SOURCE -n, --no-clobber do not overwrite an existing file (overrides a previous -i option) -P, --no-dereference never follow symbolic links in SOURCE -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all --no-preserve=ATTR_LIST don't preserve the specified attributes --parents use full source file name under DIRECTORY -R, -r, --recursive copy directories recursively --reflink[=WHEN] control clone/CoW copies. See below --remove-destination remove each existing destination file before attempting to open it (contrast with --force) --sparse=WHEN control creation of sparse files. See below --strip-trailing-slashes remove any trailing slashes from each SOURCE argument -s, --symbolic-link make symbolic links instead of copying -S, --suffix=SUFFIX override the usual backup suffix -t, --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY -T, --no-target-directory treat DEST as a normal file -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -v, --verbose explain what is being done -x, --one-file-system stay on this file system -Z, --context set SELinux security context of destination file to default type --help display this help and exit --version output version information and exit By default, sparse SOURCE files are detected by a crude heuristic and the corresponding DEST file is made sparse as well. That is the behavior selected by --sparse=auto. Specify --sparse=always to create a sparse DEST file whenever the SOURCE file contains a long enough sequence of zero bytes. Use --sparse=never to inhibit creation of sparse files. When --reflink[=always] is specified, perform a lightweight copy, where the data blocks are copied only when modified. If this is not possible the copy fails, or if --reflink=auto is specified, fall back to a standard copy. The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the --backup option or through the VERSION_CONTROL environment variable. Here are the values: none, off never make backups (even if --backup is given) numbered, t make numbered backups existing, nil numbered if numbered backups exist, simple otherwise simple, never always make simple backups As a special case, cp makes a backup of SOURCE when the force and backup options are given and SOURCE and DEST are the same name for an existing, regular file. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值