Linux copy files to the specified folder

Source: Internet
Author: User
Tags create directory unpack

The function of the Copy command is to copy the given file or directory to another file or directory, which is as powerful as the copy command under MSDOS.

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.

The options for this command have the following meanings:

-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.

It should be explained that in order to prevent the user inadvertently using the CP command to destroy another file, such as the user specified target file name already exists, the CP command after copying the file, the file will be overwritten by the Xinyuan file, it is recommended that users use the CP command to copy files, it is best to use the I option.

Example 1

Copy all files in the specified directory to another directory

The copying of files and directories is often used. The commands for copying under Linux are CP. Assume that the replication source directory is dir1 and the destination directory is DIR2. How can I copy all the files under Dir1 to Dir2? If the Dir2 directory does not exist, you can use Cp-r dir1 dir2 directly. If the Dir2 directory already exists, you need to use Cp-r dir1/. Dir2 If you use Cp-r dir1 dir2 at this point, the Dir1 directory will also be copied to Dir2, which clearly does not meet the requirements.

Ps:dir1, Dir2 change to the corresponding directory path.

Example 2 copying a specified file to a specified folder

First create a directory for testing, and use the ' tree ' command to view its structure as follows: It can be seen that the directory contains *.txt files for testing and * for serving as cannon fodder. TES file The target is to keep the current directory structure, only the TXT file is copied out method one: When the file type is not required to be a single, can be completely copied and then delete the specified type of file completion Step1 use command Cp-r test/test2 Copy all the contents of the test catalog to Test2, when the tree structure, such as the STEP2 combination, uses Find and Xargs, and the *.tes file is deleted Xargs is a filter that passes parameters to the command. You can use the output from the previous command as the parameter command for the last command find test2/-name ' *.tes ' |xargs rm-rf, the output that will be generated by find (all TES files in the Test2 directory), as an RM parameter, thus completely removing the actual effect Examples of scenarios: The project file backup, to remove the. svn file, you can use this way method two: Required files for a single type, with directory structure replication in this case, you can use the TAR command to package a file of the specified type, and then unpack it, using the same combination of find and Xargs STEP1 Create directory Test3mkdir test3 Step2 specify type file with directory structure package find test/-name ' *.txt ' |xargs tar czf test3.tgz Step3 unpack to directory Test3tar ZXVF Test3.tgz-c test3 Run Result: Applicable scenario: More common, for example, you can copy all html/jsp/php files for a Web project, or copy a specific type of source file from another project

Linux copy files to the specified folder

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.