Linux cp Command
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:
-
-ror-R: recursively copy a directory and its contents (used when copying directories). -
-i: interactive mode, prompting the user for confirmation before overwriting. -
-f: force copying, overwriting the destination file without prompting. -
-v: display the detailed copying process (verbose). -
-p: preserve the original attributes of the file (such as permissions and timestamps). -
-a: archive mode, equivalent to-dpR, preserving all file attributes and copying directories recursively. -
-u: copy only when the source file is newer than the destination file (update mode). -
-l: create a hard link instead of copying the file. -
-s: create a symbolic link (soft link) instead of copying the file.
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
-
If the destination path is a directory,
cpit copies the source file or directory into that directory. -
If the destination path is a filename,
cpit copies the source file and renames it to the destination filename. -
When copying a directory, you must use the
-ror-Roption, otherwise an error will be reported. -
If the destination file already exists, by default
cpit is overwritten (unless the-ioption is used).
Linux Command Reference