• Linux命令之cp


    cp [选项] [–T] 源文件 目标文件

    cp [选项] 源文件 目录

    cp [选项] –t 目录 源文件

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

    (1).常用选项

    补充:符号链接又叫软链接,是一类特殊的文件,这个文件包含了另一个文件的路径名(绝对路径或相对路径)

    -a    等于 –dR –preserve=all
    --backup 为每个已存在的目标文件创建备份
    -b    类似于 --backup 但不接受参数
    -d    等于 --no-dereference –preserve=links
    -f     如果目标文件无法打开则将其移除并重试(当-n存在时则不需要再选此参数)
    -i     覆盖前询问(使前面的-n参数失效)
    -n    不要覆盖已存在的文件(使前面的-i参数失效)
    -P,--no-dereference    不跟随源文件中的符号链接
    --preserve     保持指定的属性(默认:模式,所有权,时间戳),如果可能保持附加属性:环境、链接、xattr(文件系统的扩展属性)等
    -R,-r,--recursive   递归复制目录及其子目录内的所有内容
    -s    只创建符号链接而不复制文件
    -t     将所有参数指定的源文件/目录复制至目标目录
    -T    将目标目录视作普通文件
    -v    说明正在做什么(详细显示命令执行操作)
    

    (2).实例

    补充:mkdir用来创建目录;ll约等于ls -l

    将文档拷贝到另一个文件夹下,该文件夹下没有同名文档

    [ root@CentOS6 桌面]# mkdir cpDir
    [ root@CentOS6 桌面]# ll
    总用量  4
    drwxr - xr - x .  2  root  root  4096 4月    3  12:34  cpDir
    [ root@CentOS6 桌面]# cat >mytext <<EOF
    > this is mytext!
    > EOF
    [ root@CentOS6 桌面]# ll
    总用量  8
    drwxr - xr - x.  2  root  root  4096 4月    3  12:34  cpDir
    - rw- r - - r- - .  1  root  root    16 4月    3  13:03  mytext
    [ root@CentOS6 桌面]# cp mytext cpDir
    [ root@CentOS6 桌面]# ls –l ./cpDir
    总用量  4
    - rw- r - - r- -.  1  root  root  16  4月    3  13:05  mytext
    [ root@CentOS6 桌面]# cat mytext
    this is mytext!
    [ root@CentOS6 桌面]# ll
    总用量  8
    drwxr - xr - x.  2  root  root  4096  4月    3  13:05  cpDir
    - rw- r - - r- - .  1  root  root    16  4月    3  13:03  mytext
    

    将文档拷贝到另一个文件夹下,该文件夹下有同名文档,因此会提醒是否覆盖。

    [ root@CentOS6 桌面]# ll
    总用量  8
    drwxr - xr - x.  2  root  root  4096  4月    3  13:05  cpDir
    - rw- r - - r- - .  1  root  root    16  4月    3  13:03  mytext
    [ root@CentOS6 桌面]# cat >mytext <<EOF
    > Modify the text !
    > EOF
    [ root@CentOS6 桌面]# cp mytext cpDir
    cp:是否覆盖“cpDir/mytext”? y
    [ root@CentOS6 桌面]# cat ./cpDir/mytext
    Modify the text !
    

    将newDir拷贝一份到Dir1目录下(当Dir1文件夹存在时);将newDir下的所有东西拷贝一份到新建的Dir2目录(Dir2不存在)

    [ root@CentOS6 桌面]# ll
    总用量  0
    [ root@CentOS6 桌面]# mkdir newDir
    [ root@CentOS6 桌面]# touch ./newDir/{text1.txt,text2.txt}
    [ root@CentOS6 桌面]# ll
    总用量  4
    drwxr - xr - x.  2  root  root  4096  4月    3  15:28  newDir
    [ root@CentOS6 桌面]# mkdir Dir1
    [ root@CentOS6 桌面]# cp newDir Dir1
    cp:略过目录“newDir”
    [ root@CentOS6 桌面]# cp -a newDir Dir1  //存在Dir1
    [ root@CentOS6 桌面]# cp -a newDir Dir2  //不存在Dir2
    [ root@CentOS6 桌面]# ll
    总用量  12
    drwxr - xr - x.  3  root  root  4096  4月    3  15:29  Dir1
    drwxr - xr - x.  2  root  root  4096  4月    3  15:28  Dir2
    drwxr - xr - x.  2  root  root  4096  4月    3  15:28  newDir
    [ root@CentOS6 桌面]# ls ./Dir1  //存在的效果
    newDir
    [ root@CentOS6 桌面]# ls ./Dir2  //不存在的效果
    text1.txt  text2.txt
    

    建立一个指向text1.txt的快捷方式 :t1_link

    [ root@CentOS6 桌面]# cp –s newDir newDir_link  //只有-s无法创建文件夹的快捷方式
    cp:略过目录“newDir”
    [ root@CentOS6 桌面]# ll
    总用量  12
    drwxr - xr - x.  3  root  root  4096  4月    3  15:29  Dir1
    drwxr - xr - x.  2  root  root  4096  4月    3  15:28  Dir2
    drwxr - xr - x.  2  root  root  4096  4月    3  15:28  newDir
    [ root@CentOS6 桌面]# cd newDir
    [ root@CentOS6 newDir]# ll
    总用量  0
    - rw- r- - r- -.  1  root  root  0  4月    3  15:28  text1.txt
    - rw- r- - r- -.  1  root  root  0  4月    3  15:28  text2.txt
    [ root@CentOS6 newDir]# cp –s text1.txt t1_link
    [ root@CentOS6 newDir]# ll
    总用量  0
    lrwxrwxrwx.  1  root  root  9  4月    4  08:33  t1_link - > text1.txt
    -rw- r- - r- -.  1  root  root  0  4月    3  15:28  text1.txt
    -rw- r- - r- -.  1  root  root  0  4月    3  15:28  text2.txt

    创建文件夹的快捷方式

    [ root@CentOS6 桌面]# cp –as newDir newDir_link
    cp:“newDir_link/t1_link”:只能用于当前目录中创建相对的符号链接
    cp:“newDir_link/text1.txt”:只能用于当前目录中创建相对的符号链接
    cp:“newDir_link/text2.txt”:只能用于当前目录中创建相对的符号链接
    [ root@CentOS6 桌面]# ll
    总用量  16
    drwxr - xr - x.  3  root  root  4096  4月    3  15:29  Dir1
    drwxr - xr - x.  2  root  root  4096  4月    3  15:28  Dir2
    drwxr - xr - x.  2  root  root  4096  4月    3  15:28  newDir
    drwxr - xr - x.  2  root  root  4096  4月    3  15:28  newDir_link

    复制一份加上后缀存起来

    [ root@CentOS6 桌面]# touch mytext
    [ root@CentOS6 桌面]# cp mytext{,.txt}
    [ root@CentOS6 桌面]# ll
    总用量  16
    drwxr - xr - x.  3  root  root  4096  4月    3  15:29  Dir1
    drwxr - xr - x.  2  root  root  4096  4月    3  15:28  Dir2
    -rw- r - - r - - .  1  root  root     0  4月    4  10:42  mytext
    -rw- r - - r - - .  1  root  root     0  4月    4  10:42  mytext.txt
    drwxr - xr - x.  2  root  root  4096  4月    4  15:28  newDir
    drwxr - xr - x.  2  root  root  4096  4月    4  15:28  newDir_link

    参考:http://www.cnblogs.com/MenAngel/p/5468058.html

  • 相关阅读:
    HDU 4611 Balls Rearrangement 数学
    Educational Codeforces Round 11 D. Number of Parallelograms 暴力
    Knockout.Js官网学习(简介)
    Entity Framework 关系约束配置
    Entity Framework Fluent API
    Entity Framework DataAnnotations
    Entity Framework 系统约定配置
    Entity Framework 自动生成CodeFirst代码
    Entity Framework CodeFirst数据迁移
    Entity Framework CodeFirst尝试
  • 原文地址:https://www.cnblogs.com/diantong/p/8622901.html
Copyright © 2020-2023  润新知