Home >System Tutorial >LINUX >You will definitely regret missing this detailed CP command description!

You will definitely regret missing this detailed CP command description!

WBOY
WBOYforward
2023-12-30 15:23:39556browse
introduce

You will definitely regret missing this detailed CP command description!
Name:cp
Function: Copy files or directories
Description: The cp command is used to copy files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing directory, it will copy all the previously specified files or directories to this directory. . If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.

grammar
cp(选项)(参数)
Options
  • -a or --archive The effect of this parameter is the same as specifying the "-dpR" parameter at the same time
  • -b or --backup deletes or overwrites the destination file and backs it up first. The backed-up file or directory is also created as a symbolic link and points to the source file or directory of the source file or directory link. If this parameter is not added, if a symbolic link is encountered during the copy process, the source file or directory will be copied directly
  • -f or --force forcefully copies a file or directory, regardless of whether the destination file or directory already exists
  • -i or --interactive Ask user before overwriting file
  • -l or --link creates a hard link to the source file instead of copying the file
  • -p or --preserve preserves the attributes of the source file or directory, including owner, group, permissions and time
  • -P or --parents retains the path of the source file or directory. This path can be an absolute path or a relative path, and the destination directory must already be in
  • -r Recursive processing, processing files and subdirectories in the specified directory together. If the form of the source file or directory does not belong to a directory or symbolic link, it will be treated as an ordinary file.
  • -R or --recursive recursive processing, processing files and subdirectories in the specified directory together
  • -s or --symbolic-link creates a symbolic link to the source file instead of copying the file
  • -S or --suffix= After backing up the destination file with the "-b" parameter, a backup will be added to the suffix of the backup file. string.默认的备份字尾符串是符号"~"
  • -u 或 --update 使用这项参数之后,只会在源文件的修改时间(Modification Time)较目的文件更新时,或是名称相互对应的目的文件并不存在,才复制文件
  • -v 或 --verbose 显示执行过程
  • -V 或 --version-control= 指定当备份文件时,备份文件名的命名方式,有以下3种:
    1. numbered或t, 将使用备份编号,会在字尾加上~1~字符串,其数字编号依次递增
    2. simple或never 将使用简单备份,默认的备份字尾字符串是~, 也可通过-S来指定
    3. existing或nil将使用当前方式,程序会先检查是否存在着备份编号,若有则采用备份编号,若无则采用简单备份
  • -x 或 --one-file-system 复制的文件或目录存放的文件系统,必须与cp指令执行时所处的文件系统相同,否则不复制,亦不处理位于其他分区的文件
  • --help 显示在线帮助
  • --sparse= 设置保存希疏文件的时机
  • --version 显示版本
示例 复制文件,只有源文件较目的文件的修改时间新时,才复制文件
  cp -u -v file1 file2
将文件file1复制成文件file2
 cp file1 file2
采用交互方式将文件file1复制成文件file2
 cp -i file1 file2
.将文件file1复制成file2,因为目的文件已经存在,所以指定使用强制复制的模式
 cp -f file1 file2
将目录dir1复制成目录dir2
cp -R file1 file2
同时将文件file1、file2、file3与目录dir1复制到dir2
cp -R file1 file2 file3 dir1 dir2
复制时保留文件属性
 cp -p a.txt tmp/
复制时保留文件的目录结构
 cp -P  /var/tmp/a.txt  ./temp/
复制时产生备份文件
 cp -b a.txt tmp/
复制时产生备份文件,尾标 ~1~格式
cp -b -V t  a.txt /tmp
指定备份文件尾标
cp -b -S _bak a.txt /tmp

The above is the detailed content of You will definitely regret missing this detailed CP command description!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete