The cp
command is used to copy files or directories and is one of the most commonly used commands on Linux systems. Normally, the shell sets an alias and when copying a file from the command line, if the target file already exists, it will ask whether to overwrite it or not, regardless of whether you use the -i argument or not. However, if you execute cp
in a shell script, you will not be asked whether to overwrite or not without the -i argument. This means that command line and shell scripts are executed a bit differently.
1. Command format.
Usage.
cp [option ]… [-T] source destination
or: cp [option ]… Source… Directory
or: cp [option ]… -t directory Source…
2. Command function.
Copy source file to target file, or copy multiple source files to target directory.
3. Command parameters.
-a, –archive equal to -dR –preserve=all
-backup[=CONTROL Create a backup for each existing target file
-b is similar to –backup but does not accept arguments
-–copy-contents in recursive processing is to copy the contents of special files
-d is equivalent to –no-dereference –preserve=links
-f, –force removes the target file if it cannot be opened and retries (when the -n option
is present, you do not need to select this option again)
-i, –interactive override pre-interrogation (disable the previous -n option)
-H follow command-line symbolic links in the source file
-l, –link link files without copying
-L, –dereference always follow symbolic links
-n, –no-clobber Don’t overwrite existing files (disables the previous -i option)
-P, –no-dereference do not follow symbolic links in source files
-p equals –preserve=pattern, ownership, timestamp
-p-preserve[=list of attributes Keep the specified attributes (default: mode, ownership, timestamp), if
may keep additional attributes: environment, link, xattr, etc.
-R, -r, –recursive Copy the directory and all items within it
4. Examples of commands.
Example 1: Copy a single file to the target directory, the file does not exist in the target file
Command:
cp log.log test5
output:
[root@localhost test]# cp log.log test5
[root@localhost test]# 11
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-xr-x 2 root root 4096 10-28 14:53 test5
[root@localhost test]# cd test5
[root@localhost test5]# 11
-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root 0 10-28 14:53 log.log
Description:
Without the -a argument, the times of the two files are not the same. With the -a parameter, the times of the two files are the same.
Example 2: When the target file exists, it will ask whether to overwrite
Command:
cp log.log test5
Output:
[root@localhost test]# cp log.log test5
cp: does it overwrite "test5/log.log"? n
[root@localhost test]# cp -a log.log test5
cp: does it overwrite "test5/log.log"? y
[root@localhost test]# cd test5/
[root@localhost test5]# 11
-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
Description:
When the target file exists, it asks whether to overwrite it. This is because cp is an alias for cp -i. When the target file exists, it will ask if it is overwritten even if the -f flag is added.
Example 3: Copying an entire directory
Command:
output:
When the target directory exists:
[root@localhost test]# cp -a test3 test5
[root@localhost test]# 11
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]# cd test5/
[root@localhost test5]# 11
-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
Target directory does not exist is:
[root@localhost test]# cp -a test3 test4
[root@localhost test]# 11
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]#
Description:
Note that the result is different whether the target directory exists or not. When the target directory exists, the entire source directory is copied inside the target directory.
Example 4: copied log.log Create a link file log_link.log
Command:
cp -s log.log log_link.log
output:
[root@localhost test]# cp -s log.log log_link.log
[root@localhost test]# 11
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
Description:
The log_link.log
is created by the -s
parameter, and is a “shortcut”, so you will see on the far right side of the file, where the file is “linked” to!
Reference: