Linux命令集(Linux文件管理命令–CP指令篇)

Linux文件管理命令集(CP指令篇)


如下为笔者总结出在linux中最常用的CP指令集
🌟希望能够帮助正在Linux路上奋斗的你🌟


1. cp(copy)

复制文件或目录

cp filename.py /workspace/test/
#复制filename.py文件到/workspace/test/路径文件夹下
cp -r [directory] /workspace/test/
#复制[directory]目录及其内容到/workspace/test/路径文件夹下
短选项 长选项 描述
-a --archive 复制文件或目录,保留链接、文件属性,并复制目录下的所有内容;等同于-dpR参数组合
-b --backup=CONTROL 创建备份文件,若目标文件已经存在,添加一个备份拓展名;备份文件的类型由选项CONTROL指定,默认为none
-f --force 强制复制文件或目录,不询问
-i --interactive 复制文件或目录前先询问是否覆盖
-n --no-clobber 不覆盖已经存在的文件,不进行复制操作
-p --preserve 保留文件属性,包括所有权、访问时间、修改时间和权限
-R --recursive 递归复制整个目录及子目录;若要拷贝目录下所有内容,必须使用此选项
-s --symbolic-link 复制符号链接本身而非链接的目标文件
-S --suffix=SUFFIX 为备份拓展名设置指定的拓展名
-t --target-directory=DIRECTORY 将文件或目录复制到目标目录中
-T --no-target-directory 复制多个源文件时,将目标视为一个普通文件而不是目录
-u --update 仅复制源文件更新且文件大小不同或者目标文件不存在的文件
-v --verbose 显示详细信息
-- --help 显示帮助信息
-- --version 显示版本信息

下述为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                           set SELinux security context of destination
                                 file to default type
      --context[=CTX]          like -Z, or if CTX is specified then set the
                                 SELinux or SMACK security context to CTX
      --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.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/cp>
or available locally via: info '(coreutils) cp invocation'

 ***********************************************************
 ***********************************************************
 
用法: cp [选项]... [-T] 源文件 目标文件
或者: cp [选项]... 源文件... 目录
或者: cp [选项]... -t 目录 源文件...
将源文件复制到目标文件,或将多个源文件复制到目录。

长选项的强制参数对于短选项也是强制的。
-a, --archive                等同于 -dR --preserve=all
--attributes-only        只复制文件属性,不复制文件数据
--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=mode,ownership,timestamps
--preserve[=ATTR_LIST]   尽可能保留指定的文件属性(默认为 mode、ownership、timestamps),其他属性: context, links, xattr, all
--no-preserve=ATTR_LIST  不保留指定的文件属性
--parents                在目录树结构下使用完整的文件名
-R, -r, --recursive          递归复制目录及其子目录中的内容
--reflink[=WHEN]         控制克隆/写时复制的操作。参见下方
--remove-destination     在每次尝试打开已存在的目标文件之前先删除它(与 --force 相反)
--sparse=WHEN            控制稀疏文件的创建。参见下方
--strip-trailing-slashes  移除每个源文件参数的尾部斜杠
-s, --symbolic-link          创建符号链接而不是复制文件
-S, --suffix=SUFFIX          覆盖默认的备份后缀(~)
-t, --target-directory=DIRECTORY  将所有源文件参数复制到目录中
-T, --no-target-directory    将 DEST 视为普通文件
-u, --update                 当源文件更新时间比目标文件新,或者目标文件不存在时才进行复制
-v, --verbose                显示正在进行的操作
-x, --one-file-system        仅保留在同一文件系统上的文件
-Z                           将目标文件的 SELinux 安全上下文设置为默认类型
--context[=CTX]          类似于 -Z,或者如果指定 CTX 则将 SELinux 或 SMACK 的安全上下文设置为 CTX
--help     显示此帮助信息并退出
--version  输出版本信息并退出

默认情况下,cp 会使用粗略的启发式方法检测源文件是否为稀疏文件,并在目标文件也是稀疏文件时使其成为稀疏文件。这是通过使用 --sparse=auto 选项选择的行为。指定 --sparse=always 以创建稀疏的目标文件,当源文件包含足够长的连续空字节时。使用 --sparse=never 来禁止创建稀疏文件。

当指定 --reflink[=always] 时,执行轻量级复制,只有数据块在被修改时才会被复制。如果不可能进行该操作,则复制失败。如果指定了 --reflink=auto,则回退到标准复制。

备份后缀为“~”,除非使用 --suffix 或 SIMPLE_BACKUP_SUFFIX 来设置。版本控制方法可以通过 --backup 选项或 VERSION_CONTROL 环境变量进行选择。以下是可选值:

none, off       不进行备份(即使给出了 --backup)
numbered, t     进行编号备份
existing, nil   如果存在编号备份,则进行编号备份,否则进行简单备份
simple, never   总是进行简单备份

作为特殊情况,当给出 -f 和 --backup 选项,并且源文件与目标文件参数相同且为已存在的常规文件时,cp 从源文件创建一个备份。

GNU Coreutils 在线帮助文档:http://www.gnu.org/software/coreutils/
完整文档请参见:http://www.gnu.org/software/coreutils/cp,或在本地使用 info '(coreutils) cp invocation' 命令查看。

1. 强制复制文件,若目标文件已经存在,会直接覆盖而不提示

cp -f src_file.txt target_directory/

2. 递归复制目录,将源目录及其子目录的文件或目录全部复制到新目录

cp -r src_directory/ target_directory/

3. 保留文件属性,包括权限、时间等信息

cp -a src_file.txt target_directory/

4. 显示复制进度

cp -v src_file.txt target_directory/

5. 试探性复制文件,若目标文件已存在,取消复制文件

cp -n src_file.txt target_directory/

6. 复制指定目录下的所有内容,包括子目录和文件

cp -r /workspace/src/directory/. /workspace/target/directory/



本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>