Linux replicates all files in the specified directory to another directory
Copy all files in the specified directory to another directory
Replication of files and directories is often used. The command for replication under Linux is CP.
Suppose the replication source directory is dir1 and the target directory is dir2. How to copy all the files under Dir1 to Dir2.
If the Dir2 directory does not exist, you can directly use the
Cp-r Dir1 Dir2
Can.
If the Dir2 directory already exists, you need to use the
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.
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 will be a file prompts you to confirm, using CP-RF is also the same hint
--------------------------------------
Linux under the CP entire folder file to another folder
Cp-ri a/b/* a1/b1/Carriage return
If you are asked whether to overwrite the copy process, enter Y press ENTER, if you do not want to see the hint directly cover the use of-RF
In addition if A A1 is not in the same directory, it is best to fill the absolute path, 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 the 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
Description: 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 usually used when copying directories. It retains the link, file attributes, and recursively copies the directory, which is equal to the combination of the DPR option.
-D Keep links when copying.
-F Deletes an existing destination file without prompting.
-I and F options, in contrast, will be prompted to confirm the user before overwriting the target file. When you answer y, the target file is overwritten and is an interactive copy.
-P In addition to copying the contents of the source file, the CP will also copy the modification time and access rights to the new file.
-R If the source file given is a directory file, the CP will recursively replicate all subdirectories and files in the directory. The destination file must be a directory name at this time.
-L do not copy, just link file.
It is to be explained that to prevent users from inadvertently using the CP command to destroy another file, such as a user-specified target file name already exists, with the CP command to copy files, 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
Replication of files and directories is often used. The command for replication under Linux is CP.
Suppose the replication source directory is dir1 and the target directory is dir2. How to copy all the files under Dir1 to Dir2.
If the Dir2 directory does not exist, you can directly use the
Cp-r Dir1 Dir2
Can.
If the Dir2 directory already exists, you need to use the
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
Copy the specified file to the specified folder
First set up a directory for testing, using the ' tree ' command to view
It can be seen that the catalogue contains mainly *.txt files for testing and the * used to serve as cannon fodder. TES file
The goal is to keep the current directory structure and just copy the TXT file
Method One: When a file type that is not required is relatively single, it can be completed by completely copying and then deleting the file of the specified type
STEP1 uses the command cp-r test/test2 to completely copy all content under test directory tests to Test2
STEP2 combination uses find and xargs to remove *.tes files
Xargs is a filter that passes arguments to a command, which produces the output of the previous command as an argument to the latter command
Command find test2/-name ' *.tes ' |xargs rm-rf, the output produced by the find (all test2 files in the TES directory), as the parameters of RM, thereby completely removing
Examples of applicable scenarios: to back up the project files, to remove the. svn file, you can use this method
Method Two: The required file is a single type, with directory structure replication
In this case, you can use the TAR command to package the files of the specified type and then unpack them, and you need to combine the find and Xargs
STEP1 Build Directory Test3
mkdir test3
STEP2 package with directory structure for the specified type of file
Find test/-name ' *.txt ' |xargs tar czf test3.tgz
Step3 Unpack to Directory Test3
Tar zxvf test3.tgz-c test3
Scenario: More common, for example, to copy all html/jsp/php files for a Web project, or to copy a specific type of source file from another project