Make a little progress every day-linux-cp
1 Introduction
The cp command is used to copy files or directories. It is one of the most common commands in Linux. In general, shell sets an alias. When copying a file under the command line, if the target file already exists, it will ask whether to overwrite the file, whether or not you use the-I parameter. However, if the cp is executed in a shell script, if the-I parameter is not displayed, the system does not ask whether to overwrite it.
[Root @ Nagios ~] # Alias
Alias cp = 'cp-I'
2. Command Format
Cp [Option]... [-T] source objective
Or: cp [Option]... source... directory
Or: cp [Option]...-t directory source...
Three Common Parameters
-A, -- archive equals to-dR -- preserve = all
-- Backup [= CONTROL creates a backup for each existing target file
-I, -- interactive overwrite the previous query (invalidate the previous-n option)
-L, -- link file without copying
-L, -- dereference always follows the Symbolic Link
-N, -- no-clobber do not overwrite existing files (invalidate the previous-I option)
-R,-r, -- recursive Recursively copies all contents in the directory and Its subdirectories.
Iv. Uncommon Parameters
-A, -- archive equals to-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: In recursive processing, copying special file content
-D equals -- no-dereference -- preserve = links
-F, -- force: if the target file cannot be opened, remove it and try again (when-n Option
This option does not need to be selected if it exists)
-I, -- interactive overwrite the previous query (invalidate the previous-n option)
-H follows the command line symbolic link in the source file
-L, -- link file without copying
-L, -- dereference always follows the Symbolic Link
-N, -- no-clobber do not overwrite existing files (invalidate the previous-I option)
-P, -- no-dereference does not follow the symbolic link in the source file
-P equals to -- preserve = mode, ownership, timestamp
-- Preserve [= the attribute list retains the specified attribute (default: mode, ownership, timestamp). If
Additional attributes may be maintained: environment, Link, xattr, etc.
-C sameas -- preserve = context
-- Sno-preserve = the attribute list does not retain the specified file attribute.
-- All directories in the source file path are created in the target directory before parents is copied.
-R,-r, -- recursive Recursively copies all contents in the directory and Its subdirectories.
-- Reflink [= WHEN] controls clone/CoW copies. See the following example.
-- Remove-destination delete an existing destination before trying to open the target file
File (relative to the -- force option)
-- Sparse = WHEN controls how to create sparse files
-- Strip-trailing-slashes Delete the slashes at the end of all source files/directories in the Parameter
-S, -- symbolic-link: Only Create symbolic Links without copying files
-S, -- suffix = suffix specifies the suffix of the backup file
-T, -- target-directory = directory: All the source files/Directories specified by Parameters
Copy to target directory
-T, -- no-target-directory treats the target directory as a common file
-U, -- update copy only whenthe SOURCE file is newer
Than thedestination file or when
Destination file is missing
-V, -- verbose explain what isbeing done
-X, -- one-file-system stay on thisfile system
-Z, -- context = CONTEXT set securitycontext of copy to CONTEXT
-- Help: displays the help information and exits.
-- Version: displays the version information and exits.
Example 5: copy a single file to the directory. The object does not exist.
[Bzfys @ Nagiost1] $ tree
.
── T2
── T3
│ ?? ── T2
── T4
[Bzfys @ Nagios t1] $ cp t4./t3/
[Bzfys @ Nagiost1] $ tree
.
── T2
── T3
│ ?? ── T2
│ ?? ── T4
── T4
5.1 copy a single file to the directory. The file target exists. Add-a and no-.
[Root @ Nagiost1] # ll
Total usage 8
Drwxrwxr-x 2 bzfys 4096 June 18 09:16 t2
Drwxrwxr-x 3 bzfys 4096 June 18 09:18 t3
-Rw-r -- 1 bzfys 0 June 18 09:17 t4
[Root @ Nagiost1] # cp t4 t3/
Cp: overwrite "t3/t4 "? Y
[Root @ Nagiost1] # ll t3/
Total usage 4
Drwxrwxr-x 2 bzfys 4096 June 18 09:16 t2
-Rw-r -- 1 bzfys 0 June 18 09:22 t4
[Root @ Nagiost1] # cp-p t4 t3/
Cp: overwrite "t3/t4 "? Y
[Root @ Nagiost1] # ll t3/
Total usage 4
Drwxrwxr-x 2 bzfys 4096 June 18 09:16 t2
-Rw-r -- 1 bzfys 0 June 18 09:17 t4
It is not hard to see that the attributes such as the file timestamp copied with the-p parameter will not change.
5.2 copy directory
[Root @ Nagiost1] # cp t3 t5
Cp: skipping the directory "t3"
[Root @ Nagiost1] # cp-a t3 t5
[Root @ Nagiost1] # tree
.
── T2
── T3
│ ?? ── T2
│ ?? ── T4
── T4
── T5
── T2
── T4
5 directories, 3 files
It can be seen that if you want to copy a directory, you need the-a or-r parameter (the-r parameter is demonstrated later)
5.3 copy one directory to another
[Root @ Nagiost1] # cp t3 t6/
Cp: skipped directory "t3
[Root @ Nagiost1] # cp-r t3 t6
[Root @ Nagiost1] # tree
.
── T2
── T3
│ ?? ── T2
│ ?? ── T4
── T4
── T5
│ ?? ── T2
│ ?? ── T4
── T6
── T3
── T2
── T4
8 directories, 4 files
[Root @ Nagiost1] # cp-r t3/t6/
Cp: does it cover "t6/t3/t4 "? Y
[Root @ Nagiost1] # tree
.
── T2
── T3
│ ?? ── T2
│ ?? ── T4
── T4
── T5
│ ?? ── T2
│ ?? ── T4
── T6
── T3
── T2
── T
We can see that the final result of the addition/unaddition is the same.