Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the cp command in linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How is the cp command used in linux? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

1. Command format:

Usage:

Cp [options]... [- T] Source destination

Or: cp [options]... Source. Catalogue

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

2. Command function:

Copy the source file to the destination file, or copy multiple source files to the destination directory.

3. Command parameters:

-a,-- archive equals-dR-- preserve=all

-- backup [= CONTROL creates a backup for each existing target file

-b is similar to-- backup but does not accept parameters

-- copy-contents copies the contents of special files in recursive processing

-d equals-- no-dereference-- preserve=links

-f,-- force if the target file cannot be opened, remove it and try again (when-n option

Do not need to select this option if it exists)

-I,-- ask before interactive override (invalidate the previous-n option)

-H follows the command line symbolic link in the source file

-l,-- link links the file without copying it

-L,-- dereference always follows symbolic links

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

-P,-- no-dereference does not follow symbolic links in the source file

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

-- preserve [= attribute list keeps the specified attributes (default: mode, ownership, timestamp), if

Additional attributes may be maintained: environment, links, xattr, etc.

-R,-r,-- recursive copies the directory and all items in the directory

4. Command example:

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:

Copy the code

The code is as follows:

[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

Description:

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

Example 2: when the target file exists, you will be asked whether to overwrite it.

Command:

Cp log.log test5

Output:

Copy the code

The code is as follows:

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

Description:

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

Example 3: copy the entire directory

Command:

Output:

When the destination directory exists:

Copy the code

The code is as follows:

[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

The destination directory does not exist is:

Copy the code

The code is as follows:

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

Description:

Note that the results are different if the target directory exists or not. When the destination directory exists, the entire source directory is copied to the destination directory.

Example 4: create a link file log_link.log for the copied log.log

Command:

Cp-s log.log log_link.log

Output:

Copy the code

The code is as follows:

[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

After reading the above, have you mastered how the cp command is used in linux? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report