Linux common commands: cp command

  The cp command is used to copy files or directories and is one of the most commonly used commands in Linux systems. Under normal circumstances, the shell will set an alias, and when copying a file on the command line, if the target file already exists, it will ask whether to overwrite, regardless of whether you use the -i parameter. But if cp is executed in a shell script, it will not ask whether to overwrite if there is no -i parameter. This shows that the command line and shell scripts are executed somewhat differently. 

1. Command format:

usage:

cp [options]... [-T] source destination

or: cp [options]...source...directory

or: cp [options]... -t directory source...

2. Command function:

  Copy a source file to a target file, or copy multiple source files to a target directory.

3. Command parameters:

-a, --archive is equal to -dR --preserve=all

--backup[=CONTROL create a backup of each existing target file

-b like --backup but takes no arguments

--copy-contents Copy special file contents recursively

-d is equivalent to --no-dereference --preserve=links

-f, --force remove target file if it cannot be opened and try again (not required when -n option is present)

-i, --interactive Ask before overwriting (overrides previous -n options)

-H follow command line symlinks in source files

-l, --link link files without copying

-L, --dereference always follow symlinks

-n, --no-clobber do not overwrite existing files (invalidate the previous -i option)

-P, --no-dereference do not follow symbolic links in source files

-p equals --preserve=mode,ownership,timestamp

--preserve[=attribute-list Preserve the specified attributes (default: mode, ownership, timestamp), if possible preserve additional attributes: environment, link, xattr, etc.

-R, -r, --recursive copy directory and all items in it

4. Command example:

Example 1: Copy a single file to the target directory, the file does not exist in the target file

Order:

cp log.log test5

output:

[root@localhost test]# cp log.log test5

[root@localhost test]# ll

-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]# ll

-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

 

illustrate:

  Without the -a parameter, the time of the two files is different. When the -a parameter is used, the time of the two files is the same.  

Example 2: When the target file exists, it will ask whether to overwrite

Order:

cp log.log test5

output:

[root@localhost test]# cp log.log test5

cp : Overwrite "test5/log.log"? n

[root@localhost test]# cp -a log.log test5

cp : Overwrite "test5/log.log"? y

[root@localhost test]# cd test5/

[root@localhost test5]# ll

-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

 

illustrate:

  When the target file exists, it will ask whether to overwrite. This is because cp is an alias for cp -i. When the target file exists, even if the -f flag is added, it will ask whether to overwrite it.

Example 3: Copy the entire directory

Order:

output:

When the target directory exists:

[root@localhost test]# cp -a test3 test5 

[root@localhost test]# ll

-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]# ll

-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

When the target directory does not exist:

[root@localhost test]# cp -a test3 test4

[root@localhost test]# ll

-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]#

illustrate:

  Note that the result is different whether the target directory exists or not. When the target directory exists, the entire source directory is copied into the target directory.

 

Example 4: The copied log.log creates a link file log_link.log

Order:

cp -s log.log log_link.log

output:

[root@localhost test]# cp -s log.log log_link.log

[root@localhost test]# ll

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

illustrate:

  The log_link.log is caused by the -s parameter, and it creates a "shortcut", so you will see that on the far right of the file, it will show where this file is "linked"!

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325203624&siteId=291194637