cp -- copy command

Example:

Copy the aaa file on the desktop to the mnt directory

cp /root/desktop/aaa  /mnt/

If a file is copied to a target file, and the target file already exists, the contents of the target file will be destroyed. All parameters in this command can be either absolute path names or relative path names. Usually the dot .or dot ..form is used. For example, the following command will copy the specified file to the current directory:

cp ../mary/homework/assign .

The directory specified by all target files must already exist, and the cp command cannot create a directory. If you do not have permission to copy the file, the system displays an error message.

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

cp file /usr/men/tmp/file1

Copy /usr/menall files under a directory and its subdirectories into a /usr/zhdirectory

cp -r /usr/men /usr/zh

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

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 files, there will be a prompt: You need to keep pressing Y to determine the execution of the overwrite. The number of files is not much, but if there are hundreds of them, it is estimated that you will vomit blood by pressing Y, so I have tossed for a long time and summed up a method:

cp aaa/* /bbb
Copy all the files in the aaa directory 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 subdirectories are not ignored.
cp -r -a aaa/* /bbb
You still need to press Y to confirm the operation, and also pass the aaa directory and subdirectory and file attributes to /bbb.
\cp -r -a aaa/* /bbb
Success, no prompt to press Y, directory attributes passed, no directory skipped.
Detailed explanation

The cp command is used to copy one or more source files or directories to a 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 a time, the target file parameter must be an existing directory, otherwise an error will occur.

grammar

cp(options)(parameters)

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, the target file or directory is also established as a symbolic link, and points to the original file or directory linked 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 the existing file;
 - l: establish a hard link to the source file instead of copying the file;
 - p: keep Attributes of source files or directories;
 -R/ r: Recursive processing, processing all files in the specified directory together with subdirectories;
 - s: Establish a symbolic link to the source file instead of copying the file;
 - u: Use this After the parameter, the file will be copied only when the change time of the source file is newer than that of the target file or when the target file with the corresponding name does not exist;
 - S: When backing up the file, replace the file with the specified suffix "SUFFIX" Default suffix;
 - b: Backup the target file before overwriting the existing file target;
 -v: Display the operation performed by the command in detail.

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 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.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326216167&siteId=291194637