[Collection] Linux commands: CP, RM, and tar

Source: Internet
Author: User

CP (copy an archive or directory)
[Root @ Linux ~] # Cp [-adfilprsu] Destination)
[Root @ Linux ~] # Cp [Options] source1 source2 sourc4 .... Directory
Parameters:
-A: equivalent to-PDR;
-D: if the source file is a link file, copy the link file property instead of the file itself;
-F: force. If there are repeated or other questions, the user is not asked, but the user is forced to copy;
-I: If the destination effect (destination) already exists, the system will first ask if it is a real action during overwriting!
-L: Create a hard link instead of copying the file;
-P: copy the file together with its attributes, instead of using the predefined attributes;
-R: recursive continuous replication for directory replication;
-S: copy it into a symbolic link, that is, a "shortcut" file;
-U: If destination is earlier than source, it will update destination!
Note that if there are more than two source files, the last target file must be a "directory!

Example:
Example 1: Copy. bashrc under the Home Directory to/tmp and rename it bashrc
[Root @ Linux ~] # Cd/tmp
[Root @ Linux TMP] # CP ~ /. Bashrc
[Root @ Linux TMP] # cp-I ~ /. Bashrc
CP: overwrite 'basrhc '? N
# Repeat the two operations. Because bashrc already exists under/tmp, add the-I parameter,
# You will be asked if the user is sure before overwriting! You can press N or Y!
# However, if you do not want to ask, add the-F parameter to forcibly overwrite it!

Example 2: copy/var/log/wtmp to/tmp
[Root @ Linux TMP] # cp/var/log/wtmp. <= If you want to copy data to the current directory, do not forget the last one.
[Root @ Linux TMP] # ls-L/var/log/wtmp
-RW-r-1 root utmp 71808 Jul 18 12:46/var/log/wtmp
-RW-r-1 Root 71808 Jul 18 21:58 wtmp
# Have you noticed this ?! Without adding any parameters, the owner of the file will change, and the permissions will be changed ~
# This is a very important feature! Be careful! In addition, the time for establishing a file connection is not the same!
# If you want to copy all the features of an archive, add-!
[Root @ Linux TMP] # cp-A/var/log/wtmp wtmp_2
[Root @ Linux TMP] # ls-L/var/log/wtmp wtmp_2
-RW-r-1 root utmp 71808 Jul 18 12:46/var/log/wtmp
-RW-r-1 root utmp 71808 Jul 18 wtmp_2
# Now! The data features are exactly the same! Really nice ~ This is the feature of-!

Example 3: Copy all contents in the/etc/directory to the/tmp directory.
[Root @ Linux TMP] # cp/etc/tmp
CP: omitting directory '/etc' <= if it is a directory, you cannot copy it directly. Add the-R parameter.
[Root @ Linux TMP] # cp-r/etc/tmp
# I want to emphasize it again! -R indicates that directories can be copied. However, permissions on files and directories are changed ~
# Therefore, you can also use CP-A/etc/tmp to issue instructions!

Example 4: Create a symbolic link for bashrc copied in Example 1)
[Root @ Linux TMP] # ls-l bashrc
-RW-r-1 Root 395 Jul 18 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-2 root Root 395 Jul 18 22:08 bashrc
-RW-r-2 root Root 395 Jul 18 22:08 bashrc_hlink
Lrwxrwxrwx 1 Root 6 Jul 18 bashrc_slink-> bashrc
# The bashrc_slink is caused by the-S parameter and creates a "shortcut 』,
# So you will see at the far right of the file, where the file is connected!
# The bashrc_hlink is interesting! After this file is created, bashrc and bashrc_hlink
# All parameters are the same, but the number of links in the second column changes to 2 ~ Instead of the original 1 Oh!
# The similarities and differences between the two links will be introduced in the next chapter!

Example 5: If ~ /. Bashrc/tmp/bashrc is copied.
[Root @ Linux TMP] # cp-u ~ /. Bashrc/tmp/bashrc
# This-u feature is copied only when the target file is different from the source file.
# So, it is often used in "backup" work! Pai_^

Example 6: Copy bashrc_slink from example 4 to bashrc_slink_2.
[Root @ Linux TMP] # cp bashrc_slink bashrc_slink_2
[Root @ Linux TMP] # ls-l bashrc_slink *
Lrwxrwxrwx 1 Root 6 Jul 18 bashrc_slink-> bashrc
-RW-r-1 Root 395 Jul 18 22:48 bashrc_slink_2
# This example is also very interesting! The original link file was copied, but the actual file of the link file was copied.
# That is, if no parameter is added, the source file is copied, not the link file attribute!
# To copy the attributes of a link file, you must use the-D or-a parameter!

Example 7: Copy. bashrc and. bash_history in the home directory to/tmp
[Root @ Linux TMP] # CP ~ /. Bashrc ~ /. Bash_history/tmp
# You can copy multiple data to the same directory at a time!
This CP has many functions, and we often use this command because we are copying some data. In general, if we copy others' data (of course, you must have read permission for this file! When copying _ ^), we always want to copy the data to our own end. Therefore, in The Preset conditions, the CP source file and target file have different permissions, the owner of the target file is usually the instruction operator. For example, in the example 2 above, because I am a root user, the copied file owner and group are changed to the root user! Do you understand this ?! Pai_^

Because of this feature, some special permission files, such as password files (/etc/shadow) and some configuration files, need special attention during Backup, you cannot directly copy files using CP, but you must add parameters such as-A or-P that can completely copy files! In addition, if you want to copy an archive to other users, you must also note the permissions of the archive (including read, write, execute, and owner). Otherwise, other people still cannot revise your archives! Note!

As for the above example, the fourth example is the most interesting. Using-L and-s will create the so-called link file ), however, these two links do not show the same situation. What is this? That-L is the so-called hard link. As for-s, it is the symbolic link. This is not introduced here, because this involves the knowledge of I-node, which we have not introduced yet, the next chapter will discuss the link issue! In short, because CP has various archive attributes and permissions, you must be aware of the following during replication:
• Do I need to retain the full source file information?
• Is the source file symbolic link file )?
• Is the source file special, such as FIFO or socket?
• Is the source file a directory?

From: http://www.ategpu.com/2009/08/14/linux-cp%E5%91%BD%E4%BB%A4%E8%AF%A6%E8%A7%A3.html

 

 

Rm command

You can use the RM command to delete unnecessary files. This command is used to delete one or more files or directories in a directory. It can also delete a directory and all its files and subdirectories. For linked files, the link is closed, and the original file remains unchanged.

The common format of the RM command is:

Rm [Option] file...

If the-r option is not used, RM will not delete the directory.

The options of this command are as follows:

-F ignores non-existing files and never gives a prompt.

-R indicates that Rm recursively deletes all directories and subdirectories listed in the parameter.

-I.

Be careful when using the RM command. Because once a file is deleted, it cannot be recovered. To prevent this situation, you can use the I option to confirm the files to be deleted one by one. If you enter y, the file will be deleted. If you enter anything else, the file will not be deleted.

From: http://www.sb123.org/603

 

 

 

Tar command
Tar [-cxtzjvfppn] file and directory ....
Parameters:
-C: Create a parameter command for the compressed file );
-X: Unlock the parameter command of a compressed file!
-T: view the files in the tarfile!
Note that C/X/T can only exist under the parameter! Cannot exist at the same time!
Because it is impossible to simultaneously compress and decompress.
-Z: does it have the gzip attribute at the same time? That is, do I need to use gzip for compression?
-J: Does it have Bzip2 attributes at the same time? That is, do I need to use Bzip2 for compression?
-V: The file is displayed during compression! This is common, but it is not recommended to use it in the background execution process!
-F: use the file name. Please note that the file name should be followed immediately after F! Do not add parameters!
For example, using "Tar-zcvfp tfile sfile" is an incorrect method.
"Tar-zcvpf tfile sfile" is correct!
-P: use the original attributes of the original file (the attributes will not be changed based on the user)
-P: absolute paths can be used for compression!
-N: a new date (yyyy/mm/DD) will be packed into the new file!
-Exclude file: do not pack the file during compression!
Example:
Example 1: package all the files in the/etc directory into/tmp/etc.tar
Tar-CVF/tmp/etc.tar/etc <= package only, do not compress!
Tar-zcvf/tmp/etc.tar.gz/etc <= compressed with Gzip
Tar-jcvf/tmp/etc.tar.bz2/etc <= compressed with Bzip2
Note that the file name after parameter F is obtained by ourselves. We are used to using. tar for identification.
If the Z parameter is added, .tar.gz or. tgz is used to represent the tar file ~ compressed by gzip ~
If you add the J parameter, use .tar.bz2 as the file name ~
When the preceding command is executed, a warning message is displayed:
"Tar: removing leading '/" from Member names "is a special setting for absolute paths.

Example 2: Check the files in the above/tmp/etc.tar.gz file?
Tar-ztvf/tmp/etc.tar.gz
Since we use gzip compression, when you want to view the files in the TAR file,
You have to add the Z parameter! This is important!

 

Example 3: Decompress the/tmp/etc.tar.gz file under/usr/local/src.
CD/usr/local/src
Tar-zxvf/tmp/etc.tar.gz
Under the Preset conditions, we can unbind the compression file anywhere! In this example,
First, I will transform the working directory to the/usr/local/src directory, and unlock/tmp/etc.tar.gz,
The directory to be unlocked will be in/usr/local/src/etc! In addition, if you enter/usr/local/src/etc
The file attributes in this directory may be different from those in/etc!

Example 4: Under/tmp, I only want to unbind the etc/passwd in/tmp/etc.tar.gz.
CD/tmp
Tar-zxvf/tmp/etc.tar.gz etc/passwd
I can use tar-ztvf to check the file name in the tarfile. If you only need one file,
You can issue it in this way! Notice! The root directory in etc.tar.gz/is removed!

Example 5: Back up all the files in/etc/and save their permissions!
Tar-zxvpf/tmp/etc.tar.gz/etc
This-P attribute is very important, especially when you want to keep the attribute of the original file!

Example 6: only new files in/home are backed up.
Tar-N "2005/06/01"-zcvf home.tar.gz/home

Example 7: I want to back up/home,/etc, but not/home/dmtsai
Tar-exclude/home/dmtsai-zcvf myfile.tar.gz/home/*/etc

Example 8: package/etc/and unpack it under/tmp without generating a file!
CD/tmp
Tar-CVF-/etc | tar-xvf-
This action is a bit like CP-r/etc/tmp ~ It is still useful!
Note that the output file is changed to-, the input file is changed to-, and another file is | Yes ~
This represents standard output, standard input, and pipeline commands respectively!
In bash shell, we will mention this command again to explain it to you!

From: http://www.sb123.org/602

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.