Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux cp Command

Linux cp Command

Linux Command Reference Linux Command Reference

The Linux cp (short for "copy file") command is mainly used to copy files or directories.

With the cp command, users can copy files or directories from one location to another, and can choose to preserve the original file's attributes (such as permissions and timestamps).

Syntax

cp [options] source dest

或

cp [选项] 源文件 目标文件

Here, source (source file) indicates the path of the file or directory to be copied, and dest (destination file) indicates the path of the copied file or directory.

Option descriptions:

Example

1. Copy a file to the destination directory



cp file.txt /path/to/destination/

Copy file.txt into the /path/to/destination/ directory.

2. Copy a file and rename it



cp file.txt /path/to/destination/newfile.txt

Copy file.txt into the /path/to/destination/ directory and rename it to newfile.txt.

3. Copy a directory recursively



cp -r /path/to/source_dir /path/to/destination/

Recursively copy the source_dir directory and its contents into the destination directory.

4. Copy in interactive mode



cp -i file.txt /path/to/destination/

If a file with the same name already exists at the destination, the user is prompted to confirm whether to overwrite it.

5. Preserve file attributes



cp -p file.txt /path/to/destination/

Copy a file and preserve its original attributes (such as permissions and timestamps).

6. Copy only updated files



cp -u file.txt /path/to/destination/

Copy file.txt only when it is newer than the destination file.

7. Display the copying process



cp -v file.txt /path/to/destination/

Display detailed information about the copying.

** 8. Create a hard link or a symbolic link**



cp -l file.txt /path/to/destination/  # 创建硬链接

cp -s file.txt /path/to/destination/  # 创建符号链接

9. Copy multiple files to a directory



cp file1.txt file2.txt /path/to/destination/

Copy multiple files to the destination directory.

10. Copy using wildcards



cp *.txt /path/to/destination/

Copy all .txt files to the destination directory.

** 11. Combine with the find command to copy specific files**



find /path/to/source -name "*.log" -exec cp {} /path/to/destination/ \;

Find and copy all .log files to the destination directory.

The above are just some common uses of the cp command; you can see more options and uses by running the man cp command.

Notes

  1. If the destination path is a directory,cp it copies the source file or directory into that directory.

  2. If the destination path is a filename,cp it copies the source file and renames it to the destination filename.

  3. When copying a directory, you must use the -r or -R option, otherwise an error will be reported.

  4. If the destination file already exists, by default cp it is overwritten (unless the -i option is used).

Linux Command Reference Linux Command Reference