A detailed explanation of CP Copy File command application in Linux VPS

Source: Internet
Author: User
Tags vps file permissions

The CP command is more commonly used in Linux VPS operations and applications, and can be used to copy files or folders, rename a new file, and copy files to other paths for transfer.

Example usage:

1. Copy the itbulu.com.jpg file from the root directory to the/wwwroot/web directory

cd/root/
Cp-v itbulu.com.jpg/wwwroot/web/

2, copy itbulu.com.jpg file into itbulu-2.com.jpg file

Cp-v itbulu.com.jpg itbulu.com-2.jpg

Here, the old Shunshun band shares a MV command that is used to transfer (cut) and rename files for a file.

1. File name change

MV Itbulu.log Itbulu.txt

Here is to change Itbulu.log to Itbulu.txt file name

2. File transfer

MV Itbulu.log File

Move the Itbulu.log to the file folder.

-B: Overwrite the file before overwriting
-f:force the meaning of coercion, if the target file already exists, do not ask and directly overwrite
-I: If the target file already exists, it asks if the overwrite
-U: If the target file already exists and the file is newer, it will update
MV commonly used parameters.

Introduction to CP command

Features: Copying Files or directories
Description: The CP directive is used to copy files or directories, such as specifying more than two files or directories, and the last destination is an existing directory, which copies all the files or directories previously specified to this directory. If you specify multiple files or directories at the same time, and the last destination is not an existing directory, an error message appears
Parameters:
-A or--archive the effect of this parameter is the same as specifying the '-DPR ' parameter
-B or--backup Delete, overwrite the destination file first, back up the file or directory is also established as a symbolic link, and point to the source file or directory link source files or directories. If you do not add this parameter, if you encounter a symbolic link during the copy process, the source file or directory will be copied directly
-F or--force forcibly copies a file or directory, regardless of whether the destination file or directory already exists
-I or--interactive to ask the user before overwriting the file
-L or--link create hard links to source files, not copy files
-P or--preserve preserves the properties of the source file or directory, including owner, owning group, permissions, and time
-P or--parents preserves the path to the source file or directory, which can be an absolute or relative path, and the destination directory must already be abundant in the
-R recursively processes the files in the specified directory with the subdirectories. If the source file or directory form, does not belong to the directory or symbolic link, it is considered as normal file processing
-R or--recursive recursion to handle files and subdirectories in the specified directory
-S or--symbolic-link create symbolic links to source files instead of copying files
-S < backup tail string > or--suffix=< backup Word tail string > The end of the backup file is backed up with a backup string after the "-B" parameter is used to back up the destination file. The default backup Word tail string is the symbol "~"
-U or--update Use this parameter to copy files only when the source file is modified (modification time) is newer than the destination file, or if the destination file name corresponds to the other
-V or--verbose display the execution process
-V < backup mode > or--version-control=< backup method > Specifies the following 3 ways to name a backup file name when backing up a file:
1.numbered or T, the backup number is used, and the ~1~ string is added to the end of the word, and its number is incremented sequentially
2.simple or never will use a simple backup, the default backup tail string is ~, or it can be specified by-s
3.existing or nil will use the current mode, the program will first check whether there is a backup number, if there is a backup number, if not the use of simple backup
File systems that are stored in files or directories that are copied by-X or--one-file-system must be the same as the file system in which the CP directive is executed, otherwise they will not be replicated and files located in other partitions are not processed
--help Show online Help
--sparse=< Use time > Set the time to save the Greek files
--version Display version


Example One: copy the. BASHRC from the home directory to/TMP and rename it to BASHRC

[Root@linux ~]# Cd/tmp
[Root@linux tmp]# CP ~/.BASHRC BASHRC
[Root@linux tmp]# cp-i ~/.BASHRC BASHRC
Cp:overwrite ' BASRHC '? N
# Repeat two times, because there is already BASHRC in/tmp, plus-i parameters,
# before overwriting, ask the user if they are sure! You can press N or y!
# But, conversely, if you don't want to ask, add the-f argument to force direct coverage!

Example two: copy/var/log/wtmp to/tmp below

[Root@linux tmp]# cp/var/log/wtmp. <== want to copy to the current directory, the last. Don't forget
[Root@linux tmp]# ls-l/var/log/wtmp wtmp
-rw-rw-r–1 Root utmp 71808 June 12:46/var/log/wtmp
-rw-r–r–1 root root 71808 21:58 wtmp
# Have you noticed?! In the case of no parameters, the owner of the file will change, even the permissions have been changed ~
# This is a very important feature! Be careful! Also, even the time of file establishment is different!
# If you want to copy all of the features of the file together, you can add-a Oh!
[Root@linux tmp]# cp-a/var/log/wtmp wtmp_2
[Root@linux tmp]# ls-l/var/log/wtmp wtmp_2
-rw-rw-r–1 Root utmp 71808 June 12:46/var/log/wtmp
-rw-rw-r–1 root utmp 71808 12:46 wtmp_2
# It's gone! The entire data feature is exactly the same sunglass! That's not bad. This is the feature of-a!

example Three: copy/etc/All content under this directory to/TMP

[Root@linux tmp]# cp/etc//tmp
cp:omitting directory '/etc ' <== if it is a directory, you cannot copy it directly, add-R parameters
[Root@linux tmp]# cp-r/etc//tmp
# still need to emphasize again! -R can replicate directories, but file and directory permissions will be changed
# So, can also use cp-a/etc/tmp to give instructions Oh!

Example four: Create a link file (symbolic link) with the sample one copy of BASHRC

[Root@linux tmp]# ls-l BASHRC
-rw-r–r–1 root root 395 22:08 BASHRC
[Root@linux tmp]# cp-s BASHRC bashrc_slink
[Root@linux tmp]# cp-l BASHRC bashrc_hlink
[Root@linux tmp]# ls-l bashrc*
-rw-r–r–2 root root 395 22:08 BASHRC
-rw-r–r–2 root root 395 22:08 Bashrc_hlink
lrwxrwxrwx 1 root 6 22:31 bashrc_slink-> BASHRC
# that Bashrc_slink is caused by the parameters of-s, and is built as a "shortcut",
# so you'll see at the far right of the file, it will show where the file is ' connected '!
# As for that Bashrc_hlink fun! After setting up this file, BASHRC and Bashrc_hlink
# All parameters are the same, just, the second column of the link number changed into 2 ~ instead of the original 1 Oh!
# The similarities and differences between the two ways of connecting, we will introduce in the next chapter!

Example Five: if ~/.BASHRC is newer than/TMP/BASHRC, copy it.

[Root@linux tmp]# cp-u ~/.BASHRC/TMP/BASHRC
# The character of this-U is copied when the target file differs from the source file.
# So, the comparison is often used in the "backup" of the work of Oh! ^_^

example Six : replication of the bashrc_slink resulting from paradigm four into bashrc_slink_2

[Root@linux tmp]# CP Bashrc_slink bashrc_slink_2
[Root@linux tmp]# ls-l bashrc_slink*
lrwxrwxrwx 1 root 6 22:31 bashrc_slink-> BASHRC
-rw-r–r–1 root root 395 22:48 bashrc_slink_2
# This example is also very interesting oh! The link file was originally copied, but the actual file of the link file was copied.
# that is, if you don't add any arguments, you copy the source file, not the properties of the linked file!
# to copy the properties of a linked file, you have to use the-D or-a argument!

example Seven: copy the. BASHRC and. Bash_history of the home directory to/tmp

[Root@linux tmp]# CP ~/.BASHRC ~/.bash_history/tmp
# You can copy multiple data to the same directory at once!
This CP has a lot of functionality, and because we are often replicating some data, we often use this instruction. In general, if we are going to copy someone else's data (of course, you must have Read permission to do that). ^_^), always want to copy the data to the end of our own, so, in the preset conditions, the CP source file and destination file permissions are different, the owner of the target file is usually the operator itself. For example, in example two above, because I am the root identity, the copied file owners and groups are changed to root! Say so, can you understand?! ^_^

With this feature, therefore, when we make a backup, some special permission files that require special attention, such as password files (/etc/shadow) and some profiles, cannot be copied directly to the CP, but must be combined with a-a or a-p and so on to fully copy the file permission parameters! In addition, if you want to copy files to other users, you must also pay attention to the file's permissions (including reading, writing, execution, and file owners, etc.), otherwise, others will not be able to make changes to the file you have given the action Oh! Pay attention!

As for the above example, the fourth example is the most interesting, using-l and-s will create the so-called link file (link file), but the two link files do not show the same situation. What the hell is this all about? That-l is the so-called hard link, as for-S is symbolic link, bird brother here first do not introduce, because this involves i-node knowledge, we have not introduced to, the next chapter to discuss this link question Oh! In short, because the CP has a variety of file attributes and permissions of the characteristics, so, in the replication, you must have a clear understanding:
• Do you need complete information about the retention source files?
• Is the source file a link file (symbolic link file)?
• Is the source file a special file, such as FIFO, socket, etc.?
• Is the source file a directory?

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.

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.