Linuxstar
Published on

cp command - Copy files or directories

Jonas

Jonas

2 min read
The cp command can be understood as the abbreviation of the English word copy, and its function is to copy files or directories. The cp command can copy multiple files to a specific file name or an existing directory, or copy multiple files to a specified directory at the same time. Syntax: cp [parameter] [file]
Command parameters
-f If the target file already exists, the original file will be overwritten directly
-i If the target file already exists, it will ask whether to overwrite
-p Keep all attributes of the source file or directory
-r Copy files and directories recursively
-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 connected to the source file or directory
-l Establish a hard link to the source file instead of copying the file
-s Create a symbolic link to the source file, not copy the file
-b Back up the target file before overwriting the existing file target
-v Detailed display of the operation process performed by the cp command
-a Equivalent to the "dpr" option
Example
Copy directory:
[root@linuxstar ~]# cp -R dir1 dir2/
Rename the file test1 to test2:
[root@linuxstar ~]# cp -f test1 test2
Copy multiple files:
[root@linuxstar ~]# cp -r file1 file2 file3 dir
Interactively copy all .c files in the directory /usr/linuxstar to the directory dir:
[root@linuxstar ~]# cp -r /usr/linuxstar/*.c dir