cp commandUsed to copy one or more source files or directories to a specified destination file or directory. It can copy a single source file into a specific file with a specified file name or an existing directory. The cp command also supports copying multiple files at the same time. When copying multiple files at once, the target file parameters must be an existing directory, otherwise an error will occur.
-a:The effect and simultaneous specification of this parameter"-dpR"The parameters are the same;-d: When copying symbolic connections, the target file or directory is also established as symbolic connections and points to the original file or directory connected to the source file or directory;-f: Force copying a file or directory, regardless of whether the target file or directory already exists;-i: Ask the user before overwriting existing files;-l: Establish a hard connection to the source file, rather than copying the file;-p: retain the properties of the source file or directory;-R/r:Recursive processing,Process all files in the specified directory together with subdirectories; -s: Establish a symbolic connection to the source file, rather than copying the file;-u: After using this parameter, the file will only be copied when the source file changes time is updated compared to the target file or the target file with the corresponding names does not exist;-S: When backing up the file, use the specified suffix "SUFFIX" instead of the default suffix of the file;-b: Back up the target file before overwriting the existing file target;-v:Detailed display of the command execution。
If a file is copied into a target file and the target file already exists, the contents of the target file will be corrupted. All parameters in this command can be either absolute path names or relative path names. Usually, point is used.
Or dots..
form. For example, the following command copies the specified file to the current directory:
cp ../mary/homework/assign .
The directory specified by all target files must exist, and the cp command cannot create a directory. If there is no permission to copy the file, an error message will be displayed.
Put the filefileCopy to directory/usr/men/tmp
Next, and rename it to file1
cp file /usr/men/tmp/file1
Put the directory/usr/men
Copy all files and their subdirectories under it to the directory/usr/zh
middle
cp -r /usr/men /usr/zh
Interactively list the directory/usr/men
Copy all .c files headed by m to the directory/usr/zh
middle
cp -i /usr/men m*.c /usr/zh
We're inLinuxWhen copying files using the cp command, sometimes you need to overwrite some files with the same name. When overwriting the files, there will be a prompt: you need to press Y continuously to determine the overwrite. It's okay if there are not many files, but if there are hundreds of them, it's probably going to vomit blood. So I've been trying for a long time and summarized a method:
cp aaa/*/bbb Copy all the directory aaa to the /bbb directory. At this time, if there is a file with the same name as aaa in the /bbb directory, you need to press Y to confirm and the subdirectories under the aaa directory will be skipped. cp -r aaa/* /bbb This time, you still need to press Y to confirm the operation, but the subdirectory is not ignored. cp -r -a aaa/* /bbb You still need to press Y to confirm the operation, and pass the aaa directory, subdirectory and file attributes to /bbb. \cp -r -a aaa/* /bbb Success, no prompt to press Y, passed the directory attribute, and did not skip the directory.
scpIt is the abbreviation of secure copy, and it is used to remotely copy files under Linux. The commands similar to it include CP, but CP is only copied on the local machine and cannot cross-server, and the SCP transmission is encrypted. It may affect the speed slightly. When your server hard disk becomes read-only, read only system, use scp to help you move the files out. In addition, scp is very resource-free and will not increase much system load. At this point, rsync is far inferior to it. Although rsync is a little faster than scp, rsync will cause very high hard disk I/O when there are many small files, and scp will basically not affect the normal use of the system.
1. Command format:
scp [parameters] [original path] [target path]
2. Command function:
scp is the abbreviation of secure copy, scp is a secure remote file copy command based on ssh login under Linux system. Linux's scp command can copy files and directories between linux servers.
3. Command parameters:
-1Force the scp command to use protocol ssh1-2Force the scp command to use protocol ssh2-4Force the scp command to use only IPv4 addressing-6Force the scp command to use only IPv6 addressing-B Use batch mode (no transfer password or phrase is asked during transmission)-C Allow compression。(Will-The C flag is passed to ssh, thereby opening the compression function)-p retains the modification time, access time and access permissions of the original file.-q The transfer progress bar does not display.-r Recursively copy the entire directory.-v Display output in detail。scpandssh(1) will display debugging information for the entire process. This information is used to debug connection, verification, and configuration issues.-c cipher encrypts data transmission with cipher, this option will be passed directly to ssh.-F ssh_config specifies an alternative ssh configuration file, and this parameter is passed directly to ssh.-i identity_file Read the key file used when transferring from the specified file, and this parameter is passed directly to ssh.-l limit Limited bandwidth available to users,byKbit/sFor units。 -o ssh_option If usedssh_config(5) parameter transfer method,-P port Note that it is capitalized by P, port is the port number used to specify the data transmission.-S program Specify the program used for encrypted transmission。This program must be able to understandssh(1)Options。
4. Examples of usage:
Overview of the practical application of scp command:
Copy from the local server to the remote server:
(1) Copy the file:
Command format:
scp local_file remote_username@remote_ip:remote_folder
or
scp local_file remote_username@remote_ip:remote_file
or
scp local_file remote_ip:remote_folder
or
scp local_file remote_ip:remote_file
The first and second ones specify the user name. After the command is executed, you need to enter the user password. The first one only specifies the remote directory, the file name remains unchanged, and the second one specifies the file name.
The 3rd and 4th ones do not specify a user name. After the command is executed, you need to enter a user name and password. The 3rd one only specifies a remote directory, the file name remains unchanged, and the 4th one specifies a file name.
(2) Copy the directory:
Command format:
scp -r local_folder remote_username@remote_ip:remote_folder
or
scp -r local_folder remote_ip:remote_folder
The first one specifies the user name, and you need to enter the user password after the command is executed;
The second one does not specify a user name, and you need to enter the user name and password after the command is executed;
Copy from a remote server to a local server:
The scp command copied from remote to local is the same as the above command. Just swap the following two parameters after the command copied from local to remote.
Example 1: Copy files from a distance to local directory
Order:
scp [email protected]:/opt/soft/nginx-0.5. /opt/soft/
illustrate:
Download nginx-0.5. file from the /opt/soft/ directory on the 192.168.120.204 machine to the local /opt/soft/ directory
Example 2: Copy from a distance to local
Order:
scp -r [email protected]:/opt/soft/mongodb /opt/soft/
illustrate:
Download the mongodb directory from /opt/soft/ on the 192.168.120.204 machine to the local /opt/soft/ directory.
Example 3: Upload local files to the specified directory of the remote machine
Order:
scp /opt/soft/nginx-0.5. [email protected]:/opt/soft/scptest
illustrate:
Copy the file nginx-0.5 in the local opt/soft/ directory to the opt/soft/scptest directory of the remote machine 192.168.120.204
Example 4: Upload the local directory to the specified directory of the remote machine
Order:
scp -r /opt/soft/mongodb [email protected]:/opt/soft/scptest
illustrate:
Upload the local directory /opt/soft/mongodb to the directory of /opt/soft/scptest on remote machine 192.168.120.204
Reference address: /peida/archive/2013/03/15/