In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use the cp command to copy files and directories under Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The use of the cp command under Linux is mainly used to copy files, plus parameters can also copy directories, the following with the editor to understand the specific usage of the cp command under Linux.
In general, shell sets an alias, and when copying a file from the command line, it asks if the target file already exists, regardless of whether you use the-I parameter or not. However, if you are executing cp in a shell script, you will not be asked if you want to overwrite without the-I parameter. This shows that the command line and shell scripts are executed differently.
1. Command format:
Usage:
Cp [options]. [- T] Source destination
Or: cp [options] The 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:
[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:
[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:
[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:
[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:
[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
Description:
That log_link.log is caused by the-s parameter, which creates a "shortcut", so you will see that on the far right of the file, it will show where the file is "linked"!
This is the end of the introduction of "how to copy files and directories using cp commands under Linux". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.