Linux copies all files in the specified directory to another directory
Copy all files in the specified directory to another directory
Copying files and directories is often used. The command to copy under Linux is cp.
Assume that the replication source directory is dir1 and the target directory is dir2. How to copy all files under dir1 to dir2
If dir2 directory does not exist, you can directly use
cp -r dir1 dir2
OK.
If dir2 directory already exists, you need to use
cp -r dir1/. dir2
If cp - r dir1 dir2 is used at this time, the dir1 directory will also be copied to dir2, which obviously does not meet the requirements.
Ps: dir1 and dir2 can be changed to the corresponding directory path.
cp -r /home/www/xxx/statics/. / home/www/statics
If there are files, delete them first
rm -rf /home/www/statics/*
Otherwise, you will be prompted to confirm one file after another, and the same prompt will be given when using cp - rf
--------------------------------------
The function of the copy command is to copy a given file or directory to another file or directory. Like the copy command under MSDOS, it is very powerful.
Syntax: cp [options] source file or directory target file or directory
Note: This command copies the specified source file to the target file or copies multiple source files to the target directory.
The meanings of the options of this command are as follows:
-A This option is usually used when copying directories. It retains link and file attributes, and copies directories recursively. Its role is equal to the combination of dpR options.
-D Keep the link when copying.
-F Delete the existing target file without prompting.
-On the contrary, the i and f options will prompt the user for confirmation before overwriting the target file. When you answer y, the target file will be overwritten, which is an interactive copy.
-PAt this time, in addition to copying the contents of the source file, cp will also copy its modification time and access permissions to the new file.
-R If the given source file is a directory file, then cp will recursively copy all subdirectories and files under the directory. In this case, the target file must be a directory name.
-L Do not copy, just link files.
It should be noted that to prevent the user from accidentally using the cp command to destroy another file, if the target file name specified by the user already exists, the file will be overwritten by the new source file after copying the file with the cp command. Therefore, it is recommended that the user use the i option when copying files with the cp command.
Example 1
Copy all files in the specified directory to another directory
Copying files and directories is often used. The command to copy under Linux is cp.
Assume that the replication source directory is dir1 and the target directory is dir2. How to copy all files under dir1 to dir2
If dir2 directory does not exist, you can directly use
cp -r dir1 dir2
OK.
If dir2 directory already exists, you need to use
cp -r dir1/. dir2
If cp - r dir1 dir2 is used at this time, the dir1 directory will also be copied to dir2, which obviously does not meet the requirements.
Ps: dir1 and dir2 can be changed 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 directory mainly contains *. txt files for testing and * files for cannon fodder. Tes file
The goal is to keep the current directory structure and only copy the txt file
Method 1: When the unnecessary file type is relatively single, it can be completed by completely copying and then deleting the specified type of file
Step 1 Use the command cp - r test/test2 to completely copy all the contents under the test directory test to test2
Step 2 Use find and xargs together to delete *. tes files
Xargs is a filter for passing parameters to commands. You can take the output from the previous command as the parameter of the next command
The command find test2/- name '*. tes' | xargs rm - rf, that is, the output generated by find (all test files in the test2 directory), as a parameter of rm, is deleted completely
Example of applicable scenarios: Back up the project file and remove the. svn file
Method 2: The required files are of a single type and copied with directory structure
In this case, you can use the tar command to package and unpack files of the specified type. You also need to use find and xargs together
Step1 Create directory test3
mkdir test3
Step 2 Package the specified type file with directory structure
find test/ -name '*.txt' |xargs tar czf test3.tgz
Step3 Unpack to directory test3
tar zxvf test3.tgz -C test3
Applicable scenario: It is common, for example, you can copy all html/jsp/php files of a Web project; Or copy a specific type of source file in another project
Statement: All articles on this site, unless otherwise specified or marked, are originally published on this site. No individual or organization is allowed to copy, embezzle, collect and publish the content of this website to any website, book and other media platforms without the consent of this website. If the content of this website infringes upon the legitimate rights and interests of the original author, please contact us for handling.