TrumanWong

cp

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

Supplementary instructions

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(option)(parameter)

Options

-a: The effect of this parameter is the same as specifying the "-dpR" parameter at the same time;
-d: When copying a symbolic link, create the target file or directory as a symbolic link and point to the original file or directory connected to the source file or directory;
-f: Forcibly copy the file or directory, regardless of whether the target file or directory already exists;
-i: Ask the user before overwriting existing files;
-l: Create a hard link to the source file instead of copying the file;
-p: Preserve the attributes of the source file or directory;
-R/r: Recursive processing, processing all files and subdirectories in the specified directory together;
-s: Create a symbolic link to the source file instead of copying the file;
-u: After using this parameter, the file will only be copied when the change time of the source file is newer than the target file or when the target file with the corresponding name does not exist;
-S: When backing up files, replace the default suffix of the file with the specified suffix "SUFFIX";
-b: Back up the target file before overwriting the existing file target;
-v: Display the operations performed by the command in detail.

Parameters

  • 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 the -R option;
  • 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 files being copied, and the last line displays the command line prompt. This way, to copy only the new files to my storage device, I use cp's "update" and "verbose" options.

In general, the argument -r can also be used in the more verbose style --recursive. But in a shorthand way, -ruv can also be used in this way.

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

Version backup --backup=numbered parameter 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 the form of dot . or dot .. is 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/tmp and rename it to file1

cp file /usr/men/tmp/file1

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

cp -r /usr/men /usr/zh

Interactively copy all .c files starting with m in the directory /usr/men to the directory /usr/zh

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 okay that there aren’t many files, but if there were hundreds of files, I would probably vomit blood if I pressed Y, so I spent a long time summarizing a method:

cp aaa/* /bbb
# Copy everything in the directory aaa to the /bbb directory. If there is a file with the same name as aaa in the /bbb directory, you need to press Y to confirm and the subdirectories in 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.

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

cp -rfb ./* ../backup
# Copy all files in the current directory to the backup folder, a sibling directory of the current directory.

Copy hidden files in the directory such as .babelrc

cp -r aaa/.* ./bbb
# Copy all files starting with `.` in the aaa directory to the bbb directory.

cp -a aaa ./bbb/
# Remember the best '/' in the following directory with the `-a` parameter

Copy to current directory

cp aaa.conf ./
# Copy aaa.conf to the current directory