Copy the entire directory under Linux

Source: Internet
Author: User

The function of this command is to copy the given file or directory to another file or directory, just like the Copy command under DOS, which is very powerful.

syntax : CP [option] source file or directory destination file or directory

Note : This command copies the specified source files to the destination file or copies multiple source files to the destination directory.

each option means :

    • -A This option is typically used when copying a directory. It retains links, file attributes, and recursively copies the directory, which is equal to the combination of the DPR option.
    • -Keep links when copying D.
    • -F Delete a target file that already exists without prompting.
    • -I and F options, in contrast, will prompt the user for confirmation before overwriting the target file. Answer y when the target file will be overwritten, is an interactive copy.
    • -P At this point, the CP will also copy the modified time and access rights to the new file in addition to the contents of the source file.
    • -R If the source file is a directory file, the CP will recursively replicate all subdirectories and files in that directory. The destination file must be a directory name at this time.
    • -L do not make copies, just link files.

need to explain is : in order to prevent the user inadvertently with the CP command to destroy another file, such as the user specified target file name is an existing file name, the CP command after copying the file, the file will be the new copy of the source file is overwritten, therefore, It is recommended that users use the I option when copying files using the CP command.

$ cp-i exam1.c/usr/wang/shiyan1.c

This command copies the file exam1.c to the/usr/wang directory and renames it to shiyan1.c

If you do not want to rename it, you can use the following command:

$ CP exam1.c/usr/wang/

Copy all the files and their subdirectories in the/usr/xu directory to the directory/usr/liu, with the following command:

$ cp-r/usr/xu//usr/liu/

Features: Copying Files or directories
Note: The CP instruction is used to copy files or directories, such as specifying more than two files or directories at the same time, and the final destination is an existing directory, it will copy all the previously specified files or directories into this directory. If you specify multiple files or directories at the same time, and the final destination is not an existing directory, an error message appears
Parameters:
-A or--archive the effect of this parameter with the same as specifying the "-DPR" parameter
-B or--backup Delete, overwrite destination files are backed up first, backed up files or directories are also created as symbolic links and point to source files or directories linked to source files or directories. If this parameter is not added, the source file or directory will be copied directly if a symbolic link is encountered during the copy process.
-F or--force forcibly copy a file or directory, regardless of whether the destination file or directory already exists
-I or--interactive overwrite the file before asking the user
-L or--link to create a hard link to the source file instead of copying the file
-P or--preserve preserves the properties of the source file or directory, including the owner, the owning group, the permissions and the time
-P or--parents retains the path to the source file or directory, which can be an absolute or relative path, and the destination directory must already be in
-R recursive processing, the files under the specified directory and subdirectories are processed together. If the form of the source file or directory is not a directory or symbolic link, it will be treated as normal file
-R or--recursive recursive processing, the files and subdirectories under the specified directory are processed together
-S or--symbolic-link to create symbolic links to source files instead of copying files
-S < back-end string > or--suffix=< back-up string > After backing up the destination file with the "-B" parameter, a backup string is added to the end of the backup file. The default backup character string is the symbol "~"
If you or--update use this parameter, the file will only be copied when the source file has been modified (modification time) when the destination file is updated, or the destination file for the name is not present.
-V or--verbose shows the execution process
-V < backup method > or--version-control=< backup method > Specifies that when backing up files, the backup file name is named in the following 3 ways:
1.numbered or T, the backup number will be used, and the ~1~ string will be added at the end of the word, with the number incremented sequentially
2.simple or never will use a simple backup, the default back-end string is ~, can also be specified by-s
3.existing or nil will use the current mode, the program will first check if there is a backup number, if there is a backup number, if not the use of simple backup
The file system that the file or directory is copied by-X or--one-file-system must be the same as the file system where the CP instruction is executed, otherwise it will not be copied or processed in other partitions
--help Show online Help
--sparse=< Timing > Setting time to save sparse files
--version Display version

Example:
Copy files only if the source files are newer than the modified time of the destination file
Cp-u-V file1 file2

. Copy the file file1 to a file file2
CP file1 File2

The file file1 is copied into a file interactively by file2
Cp-i file1 file2

. Copy the file file1 to File2 because the destination file already exists, so specify a mode that uses forced replication
Cp-f file1 file2

. copy directory Dir1 to directory Dir2
cp-r file1 file2

. copy files File1, file2, File3 and directory Dir1 to Dir2 at the same time
Cp-r file1 file2 file3 dir1 dir2

. preserve file properties while copying
Cp-p a.txt tmp/

. Preserve the directory structure of files while copying
Cp-p/var/tmp/a.txt./temp/

. Creating a backup file when copying
Cp-b a.txt tmp/

Backup file generated when copying, tail ~1~ format
Cp-b-V T a.txt/tmp

. Specify the backup file footer
Cp-b-S _bak a.txt/tmp

Copy the entire directory under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.