• Linux cp 命令使用介绍



    Linux中cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一。之前有rm 删除,有mv移动。一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管是否使用-i参数。但是如果是在shell脚本中执行cp时,没有-i参数时不会询问是否覆盖。这说明命令行和shell脚本的执行方式有不同之处。

    1.命令格式及用法


    cp [选项]... [-T] 源 目标目录
    或:
    cp [选项]... 源... 目标目录
    或:
    cp [选项]... -t 目标目录 源...
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.命令功能


    将源文件复制至目标文件,或将多个源文件复制至目标目录。

    3.命令参数


    • -a, --archive - 等于-dR --preserve=all,
    • –backup[=CONTROL为每个已存在的目标文件创建备份
    • -b - 类似 --backup 但不接受参数 --copy-contents 在递归处理是复制特殊文件内容
    • -d - 等于–no-dereference --preserve=links
    • -f, --force - 如果目标文件无法打开则将其移除并重试(当 -n 选项 存在时则不需再选此项)
    • -i, --interactive - 覆盖前询问(使前面的 -n 选项失效)
    • -H - 跟随源文件中的命令行符号链接
    • -l, --link - 链接文件而不复制
    • -L, --dereference - 总是跟随符号链接
    • -n, --no-clobber - 不要覆盖已存在的文件(使前面的 -i 选项失效)
    • -P, --no-dereference - 不跟随源文件中的符号链接
    • -p - 等于–preserve=模式,所有权,时间戳 --preserve[=属性列表 保持指定的属性(默认:模式,所有权,时间戳),如果 可能保持附加属性:环境、链接、xattr 等
    • -R, -r, --recursive 复制目录及目录内的所有项目

    4.示例


    一:复制单个文件到目标目录,文件在目标文件中不存在

    命令:

    cp test.txt testdir/
    
    • 1

    输出:

    ubuntu@VM-4-14-ubuntu:~/cp$ tree
    .
    ├── testdir
    └── test.txt
    
    1 directory, 1 file
    ubuntu@VM-4-14-ubuntu:~/cp$ cp test.txt testdir/
    ubuntu@VM-4-14-ubuntu:~/cp$ tree
    .
    ├── testdir
    │   └── test.txt
    └── test.txt
    
    1 directory, 2 files
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    说明:在没有带-a参数时,移动前后时间是不一样的。在带了-a参数时,两个文件的时间是一致的。

    二:目标文件存在时,会询问是否覆盖

    命令:

    cp -i test.txt testdir/
    
    • 1

    输出:

    ubuntu@VM-4-14-ubuntu:~/cp$ cp -i test.txt testdir/
    cp: overwrite 'testdir/test.txt'?
    
    
    • 1
    • 2
    • 3

    说明:目标文件存在时,cp -i会询问是否覆盖。目标文件存在时,即使加了-f标志,也还会询问是否覆盖。

    三:复制整个目录命令执行演示及输出:

    ubuntu@VM-4-14-ubuntu:~/cp$ ll
    total 16
    drwxrwxr-x 4 ubuntu ubuntu 4096 Jun 30 17:40 ./
    drwx------ 9 ubuntu ubuntu 4096 Jun 30 17:30 ../
    drwxrwxr-x 2 ubuntu ubuntu 4096 Jun 30 17:32 testdir/
    drwxrwxr-x 2 ubuntu ubuntu 4096 Jun 30 17:40 testdir1/
    -rw-rw-r-- 1 ubuntu ubuntu    0 Jun 30 17:35 test.txt
    ubuntu@VM-4-14-ubuntu:~/cp$ cp -a testdir1 testdir
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    说明:注意目标目录存在与否结果是不一样的。目标目录存在时,整个源目录被复制到目标目录里面。

    四:复制 test.txt 建立一个连结档 test-l.txt -> test.txt

    命令:

    cp -s test.txt test-l.txt
    
    • 1

    输出:

    cp: cannot create symbolic link 'test1.txt' to 'test2.txt': File exists
    ubuntu@VM-4-14-ubuntu:~/cp$ cp -s test2.txt test1.txt
    cp: cannot create symbolic link 'test1.txt' to 'test2.txt': File exists
    ubuntu@VM-4-14-ubuntu:~/cp$ cp -s test.txt test-l.txt
    ubuntu@VM-4-14-ubuntu:~/cp$ ll
    total 16
    drwxrwxr-x 4 ubuntu ubuntu 4096 Jun 30 17:48 ./
    drwx------ 9 ubuntu ubuntu 4096 Jun 30 17:30 ../
    -rw-rw-r-- 1 ubuntu ubuntu    0 Jun 30 17:43 test1.txt
    -rw-rw-r-- 1 ubuntu ubuntu    0 Jun 30 17:43 test2.txt
    -rw-rw-r-- 1 ubuntu ubuntu    0 Jun 30 17:47 test4.txt
    drwxrwxr-x 3 ubuntu ubuntu 4096 Jun 30 17:41 testdir/
    drwxrwxr-x 2 ubuntu ubuntu 4096 Jun 30 17:40 testdir1/
    lrwxrwxrwx 1 ubuntu ubuntu    8 Jun 30 17:48 test-l.txt -> test.txt
    -rw-rw-r-- 1 ubuntu ubuntu    0 Jun 30 17:35 test.txt
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    注意:这种连接档,是复制一个去建立,因此被建立(目标文件)一定是不存在才能执行成功。

  • 相关阅读:
    Mac 使用 scp 上传或下载文件/文件夹
    设计模式-中介者模式
    PDAC复盘法是什么?怎么用?
    varchar与char区别,以及最大长度
    【axios】的浅度分析
    深入解析序列模型:全面阐释 RNN、LSTM 与 Seq2Seq 的秘密
    Android 5.1 open data flow 数据开启流程
    LeetCode - 611.有效三角形个数
    qrcodejs二维码
    使用wxpython开发跨平台桌面应用,对wxpython控件实现类似C#扩展函数处理的探究
  • 原文地址:https://blog.csdn.net/lanlangaogao/article/details/125545517