Home  >  Article  >  Operation and Maintenance  >  What is the command to copy files in linux?

What is the command to copy files in linux?

Christopher Nolan
Christopher NolanOriginal
2020-10-20 15:04:4477857browse

The command to copy files in Linux is: "cp" command, the full English name is "copy file", which can be used to copy one or more source files or directories to a specified destination file or directory; it can copy a single source file or directory to a specified destination file or directory. The file is copied to a specific file with the specified file name or to an existing directory.

What is the command to copy files in linux?

The Linux cp (full English spelling: copy file) command is mainly used to copy files or directories.

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.

Syntax

cp(选项)(参数)

Options

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

Parameters

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

Example

If you copy a file to a target file and the target file already exists, then the content of the target file will be destroyed. All parameters in this command can be either absolute path names or relative path names. Usually the form dot . or dot .. is used. For example, the following command will copy 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/tmp, and rename it to file1

cp file /usr/men/tmp/file1

Copy the directory /usr Copy all files and subdirectories under /men to the directory /usr/zh

cp -r /usr/men /usr/zh

interactively copy the directory /usr/men Copy all .c files starting with m to the directory /usr/zh

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

When we use the cp command to copy files under Linux, sometimes we need to overwrite some files with the same name. When overwriting a file, 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 you press Y for hundreds of them, you’ll probably vomit blood. So after a long time of tossing, I came up with a method:

cp aaa/* /bbb

Copy all the files in the directory aaa to the /bbb directory. At this time, if There is a file with the same name as aaa in the /bbb directory. You need to press Y to confirm and the subdirectory under the aaa directory will be skipped.

cp -r aaa/* /bbb

This time you still need to press Y to confirm the operation, but the subdirectory is not ignored.

cp -r -a aaa/* /bbb

You still need to press Y to confirm the operation, and the aaa directory, subdirectories and file attributes are also passed to /bbb.

\cp -r -a aaa/* /bbb

Success, no prompt to press Y, directory attributes passed, directory not skipped.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What is the command to copy files in linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn