zoukankan      html  css  js  c++  java
  • 06-Linux命令【cp】

    1、命令简介

    cp(Copy file):将源文件复制至目标文件,或将多个源文件复制至目标目录。

    2、用法

         cp [选项]... [-T] 源文件 目标文件
     或:cp [选项]... 源文件... 目录
     或:cp [选项]... -t 目录 源文件...

    3、选项

      -a, --archive       等于-dR --preserve=all,与同时指定 -dpR 这三个选项效果一样,用于复制整个目录,包括目录中的子目录等都递归的复制,而且还要保持文件的访问模式,所有者,时间戳等属性与原文件一样。
          --backup[=CONTROL        为每个已存在的目标文件创建备份
      -b                类似--backup 但不接受参数
          --copy-contents        在递归处理是复制特殊文件内容
      -d                等于--no-dereference --preserve=links
      -f, --force            如果目标文件无法打开则将其移除并重试(当 -n 选项存在时则不需再选此项)
      -i, --interactive        覆盖前询问(使前面的 -n 选项失效),默认cp命令覆盖目标文件时是不会提示的,很多Linux发行版里的cp都被设置别名`cp -i`,其实作用就是给用户一个提醒。如果你不想被提示,那么请这样输入:cp source target,或者使用cp命令的绝对路径/bin/cp
      -H                跟随源文件中的命令行符号链接
      -l, --link            对源文件建立硬链接,而非复制文件
      -L, --dereference        总是跟随符号链接
      -n, --no-clobber        不要覆盖已存在的文件(使前面的 -i 选项失效)
      -P, --no-dereference        不跟随源文件中的符号链接
     -p                等于--preserve=模式,所有权,时间戳
          --preserve[=属性列表    保持指定的属性(默认:模式,所有权,时间戳),如果可能保持附加属性:环境、链接、xattr 等
      -c                           same as --preserve=context
          --sno-preserve=属性列表    不保留指定的文件属性
          --parents            复制前在目标目录创建来源文件路径中的所有目录
      -R, -r, --recursive        递归复制目录及其子目录内的所有内容
          --reflink[=WHEN]        控制克隆/CoW 副本。请查看下面的内如。
          --remove-destination    尝试打开目标文件前先删除已存在的目的地文件 (相对于 --force 选项)
          --sparse=WHEN        控制创建稀疏文件的方式
          --strip-trailing-slashes    删除参数中所有源文件/目录末端的斜杠
      -s, --symbolic-link        只创建符号链接而不复制文件
      -S, --suffix=后缀        自行指定备份文件的后缀
      -t,  --target-directory=目录    将所有参数指定的源文件/目录 复制至目标目录
      -T, --no-target-directory    将目标目录视作普通文件
     -u, --update                 使用这项参数后只会在源文件的更改时间较目标文件更新时或是名称相互对应的目标文件并不存在时,才复制文件;-v, --verbose                详细显示命令执行的操作。
      -x, --one-file-system        复制的文件或目录存放的文件系统,必须与cp指令执行时所处的文件系统相同,否则不复制,亦不处理位于其他分区的文件
      -Z, --context=CONTEXT        set security context of copy to CONTEXT

    4、实例

    实例1:将文件a.txt复制成文件b.txt
    [root@cent6 directory]# cp a.txt  b.txt
    实例2:将文件a.txt复制成文件b.txt,显示详细信息 
    [root@cent6 directory]# cp -v a.txt  b.txt
    `a.txt' -> `b.txt'
    实例3:复制文件,只有源文件较目的文件的修改时间新时,才复制文件
    [root@cent6 directory]# cp -uv  a.txt  c.txt
    实例4:采用交互方式将文件a.txt复制成文件d.txt
    [root@cent6 directory]# cp -iv a.txt  d.txt
    cp: overwrite `d.txt'? y
    `a.txt' -> `d.txt'
    实例5:将文件a.txt复制成d.txt,因为目的文件已经存在,所以指定使用强制复制的模式
    [root@cent6 directory]# cp -fv a.txt  d.txt
    `a.txt' -> `d.txt'
    实例6:递归将目录dir1复制到目录dir2下面(此时dir2不存在
    [root@cent6 directory]# cp -rv dir1 dir2
    `dir1' -> `dir2'
    `dir1/c.txt' -> `dir2/c.txt'
    `dir1/a.txt' -> `dir2/a.txt'
    `dir1/b.txt' -> `dir2/b.txt'
    `dir1/d.txt' -> `dir2/d.txt'
    实例7:递归将目录dir1复制到目录dir2下面(此时dir2已经存在
    [root@cent6 directory]# cp -rv  dir1  dir2
    `dir1/c.txt' -> `dir2/dir1/c.txt'
    `dir1/a.txt' -> `dir2/dir1/a.txt'
    `dir1/b.txt' -> `dir2/dir1/b.txt'
    `dir1/d.txt' -> `dir2/dir1/d.txt'
    实例8:复制时保留文件属性
    [root@cent6 directory]# ll
    total 0
    -rwxrwxrwx 1 root root 0 Apr 16 16:54 a.txt
    [root@cent6 directory]# cp a.txt  /tmp/a1.txt
    [root@cent6 directory]# cp -p a.txt  /tmp/a2.txt
    [root@cent6 directory]# ll /tmp
    total 12
    -rwxr-xr-x  1 root  root     0 Apr 16 16:56 a1.txt
    -rwxrwxrwx  1 root  root     0 Apr 16 16:54 a2.txt
    实例9:复制时产生备份文件
    [root@cent6 directory]# cp -bv a.txt  /tmp/
    `a.txt' -> `/tmp/a.txt'
    [root@cent6 directory]# cp -bv a.txt  /tmp/
    `a.txt' -> `/tmp/a.txt' (backup: `/tmp/a.txt~')
    [root@cent6 directory]# ll /tmp
    total 0
    -rwxr-xr-x 1 root root 0 Apr 16 17:02 a.txt
    -rwxr-xr-x 1 root root 0 Apr 16 17:02 a.txt~
    实例10:创建文件的硬链接(有同样的inode),而不是拷贝它们
    [root@oracledb dir1]# cp -l a.txt  b.txt
    [root@oracledb dir1]# cp a.txt  c.txt
    [root@oracledb dir1]# ls -li
    总用量 0
    4718769 -rw-r--r-- 2 root root 0 4月  16 17:18 a.txt
    4718769 -rw-r--r-- 2 root root 0 4月  16 17:18 b.txt
    4718772 -rw-r--r-- 1 root root 0 4月  16 17:28 c.txt
    实例11:复制的 a.txt 建立一个连结档 a_link.txt
    [root@cent6 directory]# cp -sv a.txt  a_link.txt
    `a.txt' -> `a_link.txt'
    [root@cent6 directory]# ll
    total 0
    lrwxrwxrwx 1 root root 5 Apr 16 17:06 a_link.txt -> a.txt
    -rwxrwxrwx 1 root root 0 Apr 16 16:54 a.txt
    12:不随符号链接拷贝原文件
    [root@oracledb dir1]# ll
    总用量 0
    lrwxrwxrwx 1 root root 5 4月  16 17:30 a_link.txt -> a.txt
    -rw-r--r-- 1 root root 0 4月  16 17:18 a.txt
    [root@oracledb dir1]# cp -P a_link.txt  c.txt
    [root@oracledb dir1]# ll
    总用量 0
    lrwxrwxrwx 1 root root 5 4月  16 17:30 a_link.txt -> a.txt
    -rw-r--r-- 1 root root 0 4月  16 17:18 a.txt
    lrwxrwxrwx 1 root root 5 4月  16 17:31 c.txt -> a.txt
    实例13:指定备份文件尾标 
    [root@oracledb dir1]# cp -v -S _bak a.txt   /tmp/
    "a.txt" -> "/tmp/a.txt"
    
    [root@oracledb dir1]# cp -v -S _bak a.txt /tmp/ cp:是否覆盖"/tmp/a.txt"? y "a.txt" -> "/tmp/a.txt" (备份:"/tmp/a.txt_bak")
  • 相关阅读:
    端口查看netstat -tunpl |grep 25
    解释一下查找出文件并删除find /var/log -type f -mtime +7 -ok rm {} ;
    2021.6.2
    2021.6.1
    2021.5.31
    2021.5.30(每周总结)
    2021.5.28
    2021.5.27
    2021.5.26
    2021.5.25
  • 原文地址:https://www.cnblogs.com/wang1001/p/14417144.html
Copyright © 2011-2022 走看看