Linux tutorial: Detailed explanation of cp command

cp

Copy a source file or directory to a destination file or directory

Additional information

The cp command is used to copy one or more source files or directories to the specified destination file or directory. It can copy a single source file into a specific file with a specified file name or an existing directory. The cp command also supports copying multiple files at the same time. When copying multiple files at one time, the target file parameter must be an existing directory, otherwise an error will occur.

grammar

cp(选项)(参数)

Options

-a:此参数的效果和同时指定"-dpR"参数相同;
-d:当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录;
-f:强行复制文件或目录,不论目标文件或目录是否已存在;
-i:覆盖既有文件之前先询问用户;
-l:对源文件建立硬连接,而非复制文件;
-p:保留源文件或目录的属性;
-R/r:递归处理,将指定目录下的所有文件与子目录一并处理;
-s:对源文件建立符号连接,而非复制文件;
-u:使用这项参数后只会在源文件的更改时间较目标文件更新时或是名称相互对应的目标文件并不存在时,才复制文件;
-S:在备份文件时,用指定的后缀“SUFFIX”代替文件的默认后缀;
-b:覆盖已存在的文件目标前将目标文件备份;
-v:详细显示命令执行的操作。

parameter

  • Source files: Make a list of source files. By default, the cp command cannot copy directories. If you want to copy directories, you must use -Roptions;
  • Target file: Specify the target file. When the "source file" is multiple files, the "target file" is required to be the specified directory.

Example

The first line below is the cp command and the specific parameters (-r is "recursive", -u is "update", -v is "verbose"). The next three lines display information about the copied files, and the last line displays the command line prompt. This way, to copy only new files to my storage device, I use cp's "update" and "verbose" options.

In general, parameters -rcan also be used in a more verbose style --recursive. But in a short form, it can also be used together like this -ruv.

cp -r -u -v /usr/men/tmp ~/men/tmp

The version backup --backup=numberedparameter means "I want to make a backup, and it is a numbered continuous backup." So one backup is number 1, the second is number 2, and so on.

$ cp --force --backup=numbered test1.py test1.py
$ ls
test1.py test1.py.~1~ test1.py.~2~

If you copy a file to a destination file and the destination file already exists, the contents of the destination file will be destroyed. All parameters in this command can be either absolute path names or relative path names. .Usually dots or dots are used ... For example, the following command copies the specified file to the current directory:

cp ../mary/homework/assign .

The directories specified by all target files must already exist. The cp command cannot create directories. If there is no permission to copy the file, the system will display an error message.

Copy the file file to the directory /usr/men/tmpand rename it to file1

cp file /usr/men/tmp/file1

/usr/menCopy all files in the directory and its subdirectories /usr/zhinto the directory

cp -r /usr/men /usr/zh

Interactively /usr/mencopy all .c files starting with m in the directory to the /usr/zhdirectory

cp -i /usr/men m*.c /usr/zh

When we use the cp command to copy files under Linux, we sometimes need to overwrite some files with the same name. When overwriting files, there will be a prompt: You need to press Y repeatedly to confirm the execution of the overwrite. It’s fine that there aren’t many files, but if there were hundreds of them, I’d probably vomit blood if I pressed Y, so I spent a long time summarizing a method:

cp aaa/* /bbb
# 复制目录aaa下所有到/bbb目录下,这时如果/bbb目录下有和aaa同名的文件,需要按Y来确认并且会略过aaa目录下的子目录。

cp -r aaa/* /bbb
# 这次依然需要按Y来确认操作,但是没有忽略子目录。

cp -r -a aaa/* /bbb
# 依然需要按Y来确认操作,并且把aaa目录以及子目录和文件属性也传递到了/bbb。

cp -r -a aaa/* /bbb
# 成功,没有提示按Y、传递了目录属性、没有略过目录。

Recursively force a directory copy to the specified directory, overwriting existing files.

cp -rfb ./* ../backup
# 将当前目录下所有文件,复制到当前目录的兄弟目录 backup 文件夹中

Copy hidden files in the directory such as.babelrc

cp -r aaa/.* ./bbb
# 将 aaa 目录下的,所有`.`开头的文件,复制到 bbb 目录中。

cp -a aaa ./bbb/ 
# 记住后面目录最好的'/' 带上 `-a` 参数

Copy to current directory

cp aaa.conf ./
# 将 aaa.conf 复制到当前目录

links:

https://wangchujiang.com/linux-command/c/cp.html

Guess you like

Origin blog.csdn.net/a772304419/article/details/132981646