Linux CP Directory

Source: Internet
Author: User

Linux copies all files in the specified directory to another directory
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 the Dir1 to Dir2?
If the Dir2 directory does not exist, you can use it directly
Cp-r Dir1 Dir2
Can.
If the Dir2 directory already exists, you need to use the
Cp-r dir1/. Dir2
If the Cp-r dir1 Dir2 is used at this point, the Dir1 directory will also be copied to Dir2, which is clearly not in line with the requirements.
Ps:dir1, Dir2 change to the corresponding directory path.

Cp-r/home/www/xxx/statics/. /home/www/statics
If there are files that need to be deleted first
rm-rf/home/www/statics/*
Otherwise you will be prompted to confirm the file, using CP-RF also prompt

--------------------------------------

Linux under CP entire folder file to another folder
Cp-ri a/b/* a1/b1/Enter
If the copy process asks whether to overwrite, enter y press ENTER, if you do not want to see the prompt to overwrite using-RF
In addition, if A A1 is not in the same directory, it is best to fill the absolute path, that is/xxx/xxx/a/b/*/xxx/a1/b1/

Instance:

cp-ri/home/server/tomcat/*/home/server/test/

Cp:target '/home/server/test/' is not a directory
You need to create a destination folder first
Mkdir/home/server/test

--------------------------------------
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 the Dir1 to Dir2?
If the Dir2 directory does not exist, you can use it directly
Cp-r Dir1 Dir2
Can.
If the Dir2 directory already exists, you need to use the
Cp-r dir1/. Dir2
If the Cp-r dir1 Dir2 is used at this point, the Dir1 directory will also be copied to Dir2, which is clearly not in line with the requirements.
Ps:dir1, Dir2 change to the corresponding directory path.
Example 2
Copy the specified file to the specified folder
First create a directory for testing, and use the ' tree ' command to view
It can be seen that the catalog contains *.txt files for testing and * for serving as cannon fodder. TES file
The goal is to keep the current directory structure, just copy the TXT file.

Method One: When you do not need a single file type, you can complete the file by completely copying and then deleting the specified type
Step1 Use the command cp-r test/test2 to completely copy all the contents of the test catalog to Test2

STEP2 combination use Find and xargs to delete the *.tes file
Xargs is a filter that passes parameters to the command, which can be the output from the previous command as a parameter of the latter command
Command find test2/-name ' *.tes ' |xargs rm-rf, the output that will be generated by the find (all TES files under the Test2 directory), as an RM parameter, thus completely removed
Example of a scenario: Backing up a project file, removing the. svn file from it, can be used 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 and unpack files of the specified type, together with the Find and Xargs

Step1 Creating a directory Test3
mkdir test3

STEP2 packaging The specified type file with a directory structure
Find test/-name ' *.txt ' |xargs tar czf test3.tgz

STEP3 Unpacking to Directory Test3
Tar zxvf test3.tgz-c test3

Scenario: 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


This article is from the "Dream to Reality" blog, please be sure to keep this source http://lookingdream.blog.51cto.com/5177800/1941187

Linux CP Directory

Related Article

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.