cp 命令

Linux命令:cp

  功能说明:复制文件或目录

  语法格式

cp [OPTION]... SOURCE... DIRECTORY
cp [选项] [源文件] [目标文件]

  参数说明

-p  复制文件时保持源文件的所有者,权限信息及时间属性
-d  若复制的源文件是符号链接,只复制符号链接本身,保留符号链接指向的目标文件或目录 
-r   递归复制目录
-a   归档参数,相当于pdr
-i   覆盖已有文件之前,提示用户进行确认
-t   默认情况下,cp 源文件 目标目录,使用-t参数可以颠倒顺序,即cp -t 目标目录 源文件

 

 示例

[root@template test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  9 18:15 001.txt
[root@template test]# cp 001.txt 002.txt  #<==== 不加任何参数,发现时间属性有所变化
[root@template test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  9 18:15 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:16 002.txt
[root@template test]# cp -p 001.txt 003.txt  #<==== 加-p参数,所有属性一致
[root@template test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  9 18:15 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:16 002.txt
-rw-r--r-- 1 root root 0 Nov  9 18:15 003.txt
[root@template test]# ln -s 001.txt 001_link.txt
[root@template test]# ll
total 0
lrwxrwxrwx 1 root root 7 Nov  9 18:18 001_link.txt -> 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:15 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:16 002.txt
-rw-r--r-- 1 root root 0 Nov  9 18:15 003.txt
[root@template test]# cp 001_link.txt 001_link1.txt   #<==== 不加任何参数,将软链接文件复制为普通文件
[root@template test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  9 18:18 001_link1.txt
lrwxrwxrwx 1 root root 7 Nov  9 18:18 001_link.txt -> 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:15 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:16 002.txt
-rw-r--r-- 1 root root 0 Nov  9 18:15 003.txt
[root@template test]# cp -d 001_link.txt 001_link2.txt  #<==== 加-d参数后,文件类型不变
[root@template test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  9 18:18 001_link1.txt
lrwxrwxrwx 1 root root 7 Nov  9 18:18 001_link2.txt -> 001.txt
lrwxrwxrwx 1 root root 7 Nov  9 18:18 001_link.txt -> 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:15 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:16 002.txt
-rw-r--r-- 1 root root 0 Nov  9 18:15 003.txt
[root@template test]# mkdir dir01/{a..c}/{1..3} -p
[root@template test]# touch dir01/file.txt
[root@template test]# tree dir01/
dir01/
├── a
│   ├── 1
│   ├── 2
│   └── 3
├── b
│   ├── 1
│   ├── 2
│   └── 3
├── c
│   ├── 1
│   ├── 2
│   └── 3
└── file.txt

12 directories, 1 file
[root@template test]# cp -r dir01/ dir02  #<==== 加-r递归复制
[root@template test]# tree dir02
dir02
├── a
│   ├── 1
│   ├── 2
│   └── 3
├── b
│   ├── 1
│   ├── 2
│   └── 3
├── c
│   ├── 1
│   ├── 2
│   └── 3
└── file.txt

12 directories, 1 file
[root@template test]# cp dir01 dir03  #<==== 不加参数
cp: omitting directory ‘dir01’
[root@template test]# cp -a dir01 dir03  #<==== 相当于-pdr参数的整合,推荐使用 
[root@template test]# tree dir03
dir03
├── a
│   ├── 1
│   ├── 2
│   └── 3
├── b
│   ├── 1
│   ├── 2
│   └── 3
├── c
│   ├── 1
│   ├── 2
│   └── 3
└── file.txt

12 directories, 1 file
[root@template test]# ll
total 0
-rw-r--r-- 1 root root  0 Nov  9 18:18 001_link1.txt
lrwxrwxrwx 1 root root  7 Nov  9 18:18 001_link2.txt -> 001.txt
lrwxrwxrwx 1 root root  7 Nov  9 18:18 001_link.txt -> 001.txt
-rw-r--r-- 1 root root  0 Nov  9 18:15 001.txt
-rw-r--r-- 1 root root  0 Nov  9 18:16 002.txt
-rw-r--r-- 1 root root  0 Nov  9 18:15 003.txt
drwxr-xr-x 5 root root 49 Nov  9 18:23 dir01
drwxr-xr-x 5 root root 49 Nov  9 18:23 dir02
drwxr-xr-x 5 root root 49 Nov  9 18:23 dir03
[root@template test]# cp 001.txt 002.txt   #<==== 由于别名设置了cp='cp -i',因此,覆盖之前会提示确认
cp: overwrite ‘002.txt’? no
[root@template test]# alias
alias cp='cp -i'
[root@template test]# cp -i 001.txt 002.txt
cp: overwrite ‘002.txt’?
[root@template test]# ll
total 0
-rw-r--r-- 1 root root  0 Nov  9 18:18 001_link1.txt
lrwxrwxrwx 1 root root  7 Nov  9 18:18 001_link2.txt -> 001.txt
lrwxrwxrwx 1 root root  7 Nov  9 18:18 001_link.txt -> 001.txt
-rw-r--r-- 1 root root  0 Nov  9 18:15 001.txt
-rw-r--r-- 1 root root  0 Nov  9 18:16 002.txt
-rw-r--r-- 1 root root  0 Nov  9 18:15 003.txt
drwxr-xr-x 5 root root 49 Nov  9 18:23 dir01
drwxr-xr-x 5 root root 49 Nov  9 18:23 dir02
drwxr-xr-x 5 root root 49 Nov  9 18:23 dir03
[root@template test]# cp -t dir01 001.txt 
[root@template test]# ll dir01
total 0
-rw-r--r-- 1 root root  0 Nov  9 18:33 001.txt
drwxr-xr-x 5 root root 33 Nov  9 18:20 a
drwxr-xr-x 5 root root 33 Nov  9 18:20 b
drwxr-xr-x 5 root root 33 Nov  9 18:20 c
-rw-r--r-- 1 root root  0 Nov  9 18:23 file.txt

  扩展内容:使用cp命令,如何使得覆盖已有文件时,不对用户进行提示?

  方法一:使用命令全路径的方式

[root@template test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  9 18:45 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:45 002.txt
[root@template test]# cp 001.txt 002.txt 
cp: overwrite ‘002.txt’? no
[root@template test]# which cp
alias cp='cp -i'
    /usr/bin/cp
[root@template test]# /usr/bin/cp 001.txt 002.txt
[root@template test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  9 18:45 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:46 002.txt

  方法二:命令开头使用反斜线(\)

[root@template test]# \cp 001.txt 002.txt 
[root@template test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  9 18:45 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:47 002.txt

  方法三:取消别名,临时生效,重启服务器后依然存在

[root@template test]# unalias cp
[root@template test]# alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@template test]# cp 001.txt 002.txt 
[root@template test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  9 18:45 001.txt
-rw-r--r-- 1 root root 0 Nov  9 18:49 002.txt

  方法四:修改~/.bashrc 文件内容,保存退出,使用source命令使其生效。不建议采取此种方法

[root@template test]# cat ~/.bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'  #<==== 删除此项
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi
[root@template test]# source ~/.bashrc 

 

posted @ 2020-11-09 18:55  小屁孩云轩  阅读(190)  评论(0编辑  收藏  举报