Linux cp Command

Linux 命令大全Linux Command Reference

The Linux cp (full English name: 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 optionally preserve the attributes of the original files (such as permissions, timestamps, etc.).

Syntax

cp [options] source dest
或
cp [选项] 源文件 目标文件

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

Option Description:

  • -ror-R: Recursively copy the directory and its contents (used for copying directories).

  • -i: Interactive mode; prompts the user for confirmation before overwriting.

  • -f: Force copy; overwrite the destination file without prompting.

  • -v: Display the detailed copy process (verbose).

  • -p: Preserve the original attributes of the file (such as permissions, timestamps, etc.).

  • -a: Archive mode, equivalent to-dpR, which preserves all file attributes and recursively copies directories.

  • -u: Copy only when the source file is newer than the destination file (update mode).

  • -l: Create hard links instead of copying files.

  • -s: Create symbolic links (soft links) instead of copying files.

Examples

1. Copy a file to a destination directory

cp file.txt /path/to/destination/

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

2. Copy a file and rename it

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

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

3. Recursively copy a directory

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

Recursively copy the source_dir directory and its contents to 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 will be prompted to confirm whether to overwrite it.

5. Preserve file attributes

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

Copy the file and preserve its original attributes (such as permissions, timestamps, etc.).

6. Copy only newer files

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

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

7. Display the copy process

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

Display detailed information about the copy.

8. Create hard links or symbolic links

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 run theman cpcommand to view more options and usage.

Notes

  1. If the destination path is a directory,cpthe source file or directory will be copied into that directory.

  2. If the destination path is a file name,cpthe source file will be copied and renamed to the destination file name.

  3. When copying a directory, you must use the-ror-Roption, otherwise an error will be reported.

  4. If the destination file already exists, by defaultcpit will be overwritten (unless the-ioption is used).

Linux 命令大全Linux Command Reference

Other Extensions