To copy files, use the CP (copy) command. However, the CP command has many uses. In addition to simple replication, you can also create a connection file (that is, a shortcut), compare the new and old two files and update them, copy the entire directory, and so on. For moving directories and files, use MV (move), which can also be used to rename (rename) directly. Remove the use RM (remove) command. Let's take a look at it below.
CP (copy files or directories)
[[email protected] ~]# CP [-ADFILPRSU] source file (source) destination file (destination) [[email protected] ~]# CP [ Options] Source1 source2 source3 .... Directory Parameter: -A: equivalent to the meaning of-pdr. -D: If the source file is a property of a connection file, the connection file property is copied instead of the file itself. -F: Force means that if there are duplicates or other questions, the user is not queried and the copy is forced. -I: If the destination file (destination) already exists, the overwrite will first ask if the action is true. -L: Create a hard link connection file instead of copying the file itself. -P: is copied along with the properties of the file, not the default properties. -R: Recursive continuous replication for the replication behavior of the directory. -S: Copy becomes a symbolic connection file (symbolic link), which is the "shortcut" file. -U: Updates the target file only if the destination file is older than the source file. Finally, it is important to note that if there are more than two source files, the last destination file must be "directory". Example: Example one: Copy the. BASHRC from the home directory to/TMP and rename it to BASHRC. [[email protected] ~]# cd/tmp [[email protected] tmp]# cp ~/.BASHRC BASHRC [[email Protected] tmp]# cp-i ~/.BASHRC bashrc cp:overwrite ' basrhc '? n # Repeated two actions, since/TMP already exists BASHRC, plus the-i parameter, # before overwriting will Let the user confirm. You can press N or y. # However, conversely, if you do not want to ask, add the-f argument to force a direct overwrite. Example two: Copy the/var/log/wtmp to/tmp. [Email protected] tmp]# cp/var/log/wtmp. <== want to copy to the current directory, the last. Don't forget [Email protected] tmp]# ls-l/var/log/wtmp wtmp -rw-rw-r--1 root utmp 71808 Jul 12:46/var/log/wtmp -rw-r--r--1 root root 71808 Jul 21:58 wtmp # Did you notice it? Without any parameters, the owner of the file changes, and even the permissions change. # This is a very important feature. Be careful. Also, the file setup time is not the same. # If you want to copy all the properties of a file together, you can add-a. [Email protected] tmp]# cp-a/var/log/wtmp wtmp_2 [Email protected] tmp]# ls-l/var/log/wtmp wtmp_2 -rw-rw-r--1 root utmp 71808 Jul 12:46/var/log/wtmp -rw-rw-r--1 root utmp 71808 Jul 12:46 wtmp_2 # The entire property is exactly identical. This is the role of-a. Example three: Copy all the contents of the/etc/directory to/tmp. [Email protected] tmp]# cp/etc//tmp Cp:omitting directory '/etc ' <== if it is a directory and cannot be copied directly, add the-R parameter [Email protected] tmp]# cp-r/etc//tmp # re-emphasize. -R can copy directories, but the permissions of files and directories are changed. # So, you can also use Cp-a/etc/tmp to execute commands. Example four: Create a symbolic Connection file (symbolic link) For example one copy of BASHRC [Email protected] tmp]# ls-l BASHRC -rw-r--r--1 root root 395 Jul 22:08 BASHRC [Email protected] tmp]# cp-s BASHRC bashrc_slink [Email protected] tmp]# cp-l BASHRC bashrc_hlink [Email protected] tmp]# ls-l bashrc* -rw-r--r--2 root root 395 Jul 22:08 BASHRC -rw-r--r--2 root root 395 Jul 22:08 Bashrc_hlink lrwxrwxrwx 1 root root 6 Jul 22:31 Bashrc_slink-BASHRC # Bashrc_slink is caused by the-s parameter, which is created by a "shortcut", # so you'll see at the far right of the file, and it will show where this file is "connected" to. # As for Bashrc_hlink. After establishing this file, BASHRC and Bashrc_hlink # All parameters are the same, except that the link number in the second column is changed to be 2. Rather than the original 1. # The similarities and differences between the two types of connections are described in the next chapter. Example five: If ~/.BASHRC is newer than/TMP/BASHRC, copy. [Email protected] tmp]# cp-u ~/.BASHRC/TMP/BASHRC #-U's role is to replicate when the destination file differs from the source file. # Therefore, it is more commonly used for "backup". Example 6: Copy the Bashrc_slink generated by example four into bashrc_slink_2. [email protected] tmp]# CP Bashrc_slink bashrc_slink_2 [Email protected] tmp]# ls-l bashrc_slink* lrwxrwxrwx 1 root root 6 Jul 22:31 Bashrc_slink-BASHRC -rw-r--r--1 root root 395 Jul 22:48 bashrc_slink_2 # This example is also very interesting. The original copy is the connection file, but the connection file of the actual file copied over. In other words, if you do not add any parameters, the copy is the source file, not the properties of the connection file. # to copy the properties of a connection file, use the-D or-a parameter. Example 7: Copy the. BASHRC and. Bash_history of the home directory to/tmp below. [email protected] tmp]# CP ~/.BASHRC ~/.bash_history/tmp # Copy multiple data to the same directory at a time. |
The
CP has a lot of functions, and we often use this command because we often replicate some of the data. In general, if you want to copy other people's data (of course, the file must have Read permission), always want to copy to the final data is our own, so, in the default condition, the CP source file and the target file permissions are different, the target file owner is usually the command operator itself. For example, in example two, because I am root, the copied file owners and user groups change to be root.
Because of this feature, in the time of backup, some special permissions files that require special attention, such as password file (/etc/shadow) and some settings files, can not be copied directly with CP, but must be added-a or-p, which fully copy the file permissions parameters. In addition, if you want to copy files to other users, you must also pay attention to the permissions of the file (including read, write, execute, and file owner), otherwise, the file you are not able to modify. Pay special attention to it.
The 4th example above is the most interesting, using-l and-s will create so-called connection files (link file), but the two connection files have a different display. What's the matter? -L is the so-called hard link,-s is a symbolic connection (symbolic link), I do not introduce here, because this involves i-node knowledge, we have not introduced the next chapter to discuss the issue of the connection. In summary, since CP has various file attributes and permissions, it must be clearly understood when copying: does the
need to keep the source file information intact?
Source files are symbolic connection files (symbolic link file)?
Source file is a special file, such as FIFO, socket, etc.?
Source file is a directory?
RM (delete file or directory)
[[email protected] ~]# rm [-fir] File or directory parameter: -F: Force means, forced delete. -I: interactive mode, which asks the user whether to move -r: Recursive deletion before deleting. Frequently used in the removal of directories Example: Example one: Delete a file after you have created it [[email protected] ~]# cd/tmp [[email protected] tmp]# CP ~/. BASHRC BASHRC [[email protected] tmp]# rm-i BASHRC rm:remove regular file ' bashrc '? y # If you add the-i parameter, you will be asked. Add-f parameter if you do not need to ask. Example two: Delete a directory that is not empty. [[email protected] tmp]# mkdir test [[email protected] tmp]# cp ~/.bashrc test/<== copy files to this directory, It's not an empty directory. [[email protected] tmp]# rmdir test rmdir: ' Test ': Directory not empty <== deleted. Because this is not an empty directory. [[email protected] tmp]# RM-RF test |
The
Delete command (remove) is equivalent to the DEL command under DOS. It is important to note that, usually, in the Linux system, in order to fear that the file is mistakenly deleted, many versions default to the-I parameter,-I refers to each file is deleted before the user will be confirmed once to prevent accidental deletion of files. If you want to delete the contents of the directory together, such as subdirectories and subdirectories, you will use the-RF parameter. However, before using the "RM-RF" command, please be aware that the directory or file "Yes" will be deleted by root. Because the system does not ask again if you want to delete. So this is a very important command that requires special attention. However, if it is determined that the directory is not available, it is a good idea to use RM-RF to recycle the deletion.
MV (move files and directories, or rename)
[[email protected] ~]# mv [-FIV] source destination [[email Protected] ~]# mv [options] Source1 source2 source3 .... Directory Parameters: -f:force, forcing the meaning to force a direct move without asking. -I: If the destination file (destination) already exists, you will be asked to overwrite it. -U: If the destination file already exists and the source file is newer, update Example: Example one: Copy a file, create a directory, and move the file to the directory. [[email protected] ~]# cd/tmp [[email protected] tmp]# cp ~/.BASHRC BASHRC [[email Protected] tmp]# mkdir mvtest [[email protected] tmp]# mv BASHRC mvtest # Move a file to a directory to do so. Example Two: Rename the name of the directory you just named to Mvtest2. [[email protected] tmp]# mv Mvtest Mvtest2 <== this is renamed. # In fact, there is an interesting command under Linux, called Rename, # This command specifically renames the file. Many uses. You can refer to man rename. Example three: Create two more files, and then move them all to/tmp/mvtest2. [[email protected] tmp]# cp ~/.BASHRC BASHRC1 [[email protected] tmp]# cp ~/.BASHRC BASHRC2 [[ Email protected] tmp]# mv Bashrc1 bashrc2 Mvtest2 # Note that if you have more than one source file or directory, the last destination file must be "directory." # means that all data is moved to that directory. |
This is the meaning of move. To move a file or directory, this command is important. Similarly, you can use-u (update) to test the old and new files to see if they need to be moved. Another use is "change the file name." We can easily change the file name by using the MV. However, the Linux Rename command can be used to change the file name of a large number of files, and you can take a look at the man rename.
Copy, move, and delete: CP, RM, MV