tar has many options and parameters! We will only cover a few commonly used options.
[root@www ~]# tar [-j|-z] [cv] [file name created by -f] filename... <==Packaging and Compression
[root@www ~]# tar [-j|-z] [tv] [File name created by -f] <==View file name
[root@www ~]# tar [-j|-z] [xv] [File name created by -f] [-C directory] <==Decompression
options and parameters:
-c: Create a packaged file, which can be used with -v to view the file name packaged during the process ( filename)
-t: Check which file names are contained in the contents of the packaged file, focusing on the "file name";
-x: Unpacking or decompression function, can be used with -C (capital) in Unlock
in a specific directory. Please note that -c, -t, -x cannot appear in a series of command lines at the same time.
-j: Compression/decompression through the support of bzip2: the file name is preferably *.tar.bz2
-z: Compression/decompression through the support of gzip: the file name at this time It is best to use *.tar.gz
-v: During the compression/decompression process, the file name being processed will be displayed!
-f filename: -f should be followed immediately by the file name to be processed! It is recommended to write a separate option -f!
-C Directory: This option is used for decompression. If you want to decompress in a specific directory, you can use this option.
Introduction to options that will be used in other subsequent exercises:
-p: retain the original permissions and attributes of the backup data, often used for backing up (-c) important configuration files
-P: retain absolute Path, which means that the backup data is allowed to contain the existence of the root directory;
--exclude=FILE: During the compression process, do not package FILE!
In fact, the easiest way to use tar is to remember the following method:
Compression: tar -jcv -f filename.tar.bz2 The file to be compressed Or directory name query: tar -jtv -f filename.tar.bz2 Decompression: tar -jxv -f filename.tar.bz2 -C The filename.tar.bz2 of the directory to be decompressed is the file name we chose ourselves, tar The created file name will not be automatically generated! We’re going to customize it! So the extension is very important! If [-j|-z] is not added, the file name is best *.tar. If it is the -j option, it means that there is bzip2 support, so the file name is best to be *.tar.bz2, because bzip2 will generate the .bz2 extension! As for gzip support with -z, the file name is best *.tar.gz! Do you understand?
In addition, since "-f filename" is followed closely together, many articles in the past often wrote "-jcvf filename". This is correct, but since the order of options can theoretically be changed, so Many readers will mistakenly think that "-jvfc filename" is also possible ~ In fact, this will cause the generated file name to become c! Because of -fc! Therefore, it is recommended that you separate "-f filename" from other options when learning tar, so that problems will be less likely to occur.
Without further ado, let’s test some commonly used tar methods!
Use tar to add the -j or -z parameter to back up the /etc/ directory
It is a good thing to back up the /etc directory if something happens! The easiest way to back up /etc is to use tar! Let's play around first:
[root@www ~]# tar -zpcv -f /root/etc.tar.gz /etc
tar: Removing leading `/' from member names < ==Pay attention to this warning message
/etc/
....Omit the middle....
/etc/esd.conf
/etc/crontab
# due to adding -v This option, therefore the active file name will be displayed on the screen.
# If you can turn to the first page, you will find the above error message! It will be explained below.
# As for the -p option, the focus is on "preserving the permissions and attributes of the original file".
[root@www ~]# tar -jpcv -f /root/etc.tar.bz2 /etc
# The information displayed will be exactly the same as above!
[root@www ~]# ll /root/etc*
-rw-r--r-- 1 root root 8740252 Nov 15 23:07 /root/etc.tar.bz2
-rw-r--r-- 1 root root 13010999 Nov 15 23:01 /root/etc.tar.gz
[root@www ~]# du -sm /etc
118 /etc
# Why is it recommended that you use the -j option? Can you tell from the above values? ^_^
From the above exercises, we know that when using bzip2, that is, the -j option to make backups, we can get a better compression ratio! As shown in the table above, the original /etc/ (118MBytes) dropped to about 8.7Mbytes! The reason for adding the "-p" option is to preserve the permissions and attributes of the original file! In the introduction of the cp command in Chapter 7, we talked about the different effects of permissions and file types (such as link files) on copying. Similarly, when backing up important system data, it is better to make a complete backup of the permissions of these original files. This is where the -p option comes in handy. Next, let's see what data exists in the packaged file?
Check the data content of the tar file (you can check the file name), and whether the backup file name has the meaning of the root directory
It is very simple to check the file name! You can do this:
[root@www ~]# tar -jtv -f /root/etc.tar.bz2
....Omit the front....
-rw-r--r-- root/ root 1016 2008-05-25 14:06:20 etc/dbus-1/session.conf
-rw-r--r-- root/root 153 2007-01-07 19:20:54 etc/esd .conf
-rw-r--r-- root/root 255 2007-01-06 21:13:33 etc/crontab
If you add the -v option, details The file permissions/attributes will be listed! If you just want to know the file name, just remove -v. From the above data, we can find a very interesting thing, that is, each file name has no root directory! This is also what the warning message "tar: Removing leading /' from member names (removed the /' at the beginning of the file name)" that appeared in the previous exercise told us!
Then why do we need to remove the root directory? Mainly for safety! The data we backed up using tar may need to be decompressed for use. The file name recorded in tar (the file name we just observed using tar -jtvf) is the actual file name after decompression. If the root directory is removed, assuming you decompress the backup data in /tmp, the decompressed file name will become "/tmp/etc/xxx". But "if the root directory is not removed, the decompressed file name will be an absolute path, that is, the decompressed data will definitely be placed in /etc/xxx!" 』In this way, your original data under /etc/ will be overwritten by the backup data! Tips: You will say: "Since it is a backup data, there is no problem in restoring it back, right?" 』Imagine a situation where the data you backed up is the old version of CentOS 4. /etc under 5.x is overwritten by the backup data of the old version! What should you do at this point? So, of course it is safer to remove the root directory. If you are sure that you need to back up the root directory to a tar file, you can use the -P (capital) option. Please see the example analysis below: Example: Back up the (root) directory in the file name and check Check the content file name of the backup file
[root@www ~]# tar -jpPcv -f /root/etc.and.root.tar.bz2 /etc
....The intermediate process is omitted. ...
[root@www ~]# tar -jtf /root/etc.and.root.tar.bz2
/etc/dbus-1/session.conf
/etc/esd.conf
/etc/crontab
# This time the file name checked does not contain the -v option, so it is just the file name! There are no detailed attributes/permissions and other parameters.
Have you noticed any differences? If you add the -P option, the root directory in the file name will exist! However, Brother Bird’s personal suggestion is not to add the -P option for backup! After all, many times, we back up to track problems in the future, and we don’t necessarily need to restore it back to the original system! Therefore, after removing the root directory, the application of backup data will be more flexible! It’s safer too!
Decompress the backup data and consider the decompression action of the specific directory (application of the -C option)
What if you want to unpack it? A very simple action is to unpack it directly!
[root@www ~]# tar -jxv -f /root/etc.tar.bz2
[root@www ~]# ll
....(previously omitted).. ..
drwxr-xr-x 105 root root 12288 Nov 11 04:02 etc
....(omitted later)....
At this time, the packaging file will Perform the action of "unzip in this directory"! So, you will find a directory named etc under your home directory after a while! So, if you want to unzip the file under /tmp, you can cd /tmp and then issue the above command. However, this seems very troublesome~ Is there an easier way to "specify the directory to be unlocked"? Yes, you can use the -C option! For example:
[root@www ~]# tar -jxv -f /root/etc.tar.bz2 -C /tmp
[root@www ~]# ll /tmp
....(the first one is omitted)....
drwxr-xr-x 105 root root 12288 Nov 11 04:02 etc
....(the second one is omitted)....
In this way, you can unzip the file in a different directory! Brother Niao personally believes that this -C option must be remembered! Okay, after processing, please remember to delete these two directories!
[root@www ~]# rm -rf /root/etc /tmp/etc
Again, this "rm -rf" is a very dangerous command! When downloading, please be sure to confirm the file name that follows. What we want to delete are /root/etc and /tmp/etc. Please do not delete /etc/! The system will die~ ^_^
Method of decompressing only a single file
When we decompressed just now, we decompressed all the contents of the entire packaged file! Imagine a situation, if I only want to unpack one of the files in the package file, how should I do it? It's very simple, you just use -jtv to find the file name you want, and then unzip the file name. Let’s use the following example to illustrate:
# 1. First find the file name we want, assuming that the shadow file is unzipped:
[root@www ~]# tar -jtv -f /root/etc.tar.bz2 | grep 'shadow'
-r-------- root/root 1230 2008-09-29 02:21:20 etc/shadow-
-r-------- root/root 622 2008- 09-29 02:21:20 etc/gshadow-
-r-------- root/root 636 2008-09-29 02:21:25 etc/gshadow
-r--- ----- root/root 1257 2008-09-29 02:21:25 etc/shadow <==This is what we want!
# Search important file names first! Among them, grep is the function of "retrieval" keyword! We will explain in the third article!
# You just need to have an idea here! That pipeline | Used with grep to extract the meaning of keywords!
# 2. Unzip the file! The syntax and actual practice are as follows:
[root@www ~]# tar -jxv -f packaged file.tar.bz2 file name to be unpacked
[root@www ~]# tar -jxv -f /root/ etc.tar.bz2 etc/shadow
etc/shadow
[root@www ~]# ll etc
total 8
-r-------- 1 root root 1257 Sep 29 02:21 shadow <==Yo drink! There is only one file!
# very funny! Only one file will be unlocked at this time! However, the key point is the file name! You need to find the correct file name.
# In this case, you can't write /etc/shadow ! Because of the file name recorded in etc.tar.bz2!
How to package a directory without certain files in the directory
Suppose we want to package the important directories /etc/ /root, but do not We don’t want to package the files starting with /root/etc*, because these files are all backup files we just created! And assuming that this new packaging file is to be placed as /root/system.tar.bz2, of course this file should not be packaged by itself (because this file is placed under /root!), at this time we can use the help of --exclude ! That exclude means not to include! So you can do this:
[root@www ~]# tar -jcv -f /root/system.tar.bz2 --exclude=/root/etc*
> --exclude= /root/system.tar.bz2 /etc /root
The above command is a whole list ~ In fact, you can type: "tar -jcv -f /root/system.tar.bz2 - -exclude=/root/etc* --exclude=/root/system.tar.bz2 /etc /root』, if you want to input two lines, add a backslash () at the end and press [enter] immediately , you can go to the second line and continue typing. We will explain the method of issuing this order in detail in Chapter 3. Through this --exclude="file" action, we can remove several special files or directories from the packaging list, making the packaging action easier! ^_^
In addition, in the new version of the tar command, Brother Bird found that the original "--exclude file" seemed to be unable to actually run! I clearly saw this option when using man tar, but when I used info tar, I discovered that the option function has changed to the "--exclude=file" mode! You need to pay special attention to this place!
Only back up files that are newer than a certain time
In some cases you will want to back up new files, not old files! At this time, the --newer-mtime option is very important! In fact, there are two options, one is "--newer" and the other is "--newer-mtime". What is the difference between these two options? We talked about three different time parameters in the introduction of touch in Chapter 7. When using --newer, it means that the subsequent date includes "mtime and ctime", while --newer-mtime is just mtime! So you know it! ^_^. Then let us try to deal with Luo!
# 1. First use find to find files newer than /etc/passwd
[root@www ~]# find /etc -newer /etc/passwd
.... (Process omitted)....
# At this time, a file name that is newer than the mtime of the /etc/passwd file will be displayed.
# This result is different on each host! You can check your own host first, you won’t be like Brother Bird!
[root@www ~]# ll /etc/passwd
-rw-r--r-- 1 root root 1945 Sep 29 02:21 /etc/passwd
# 2. Okay, let’s use tar to package it! The date is 2008/09/29
[root@www ~]# tar -jcv -f /root/etc.newer.then.passwd.tar.bz2
> --newer-mtime ="2008/09/29" /etc/*
....(middle omitted)....
/etc/smartd.conf <== There are really backup files*&*/etc/yum.repos.d/ <= =The directories will all be recorded!
tar: /etc/yum.repos.d/CentOS-Base.repo: file is unchanged; not dumped
# The last line shows "not backed up", which means not dumped!
# 3. Display the file
[root@www ~]# tar -jtv -f /root/etc.newer.then.passwd.tar.bz2 |
> grep -v '/$'
# Through this command, you can call out the file names in tar.bz2 that do not end with /! That's what we want!
Now you know how useful this command is! You can even record and back up differential files~ Backing up like this will be much easier! You can imagine like this, if I only performed a complete data backup a month ago, then when I want to back up this month, I can of course back up only the upgraded files after the point in time when I backed up last month! why? Because the original files have been backed up! Why do it again? Just back up the new data. This can reduce the backup capacity!
Basename: tarfile, tarball?
It is also worth mentioning that the files packaged by tar have different names whether they are compressed or not! If it is just for packaging, it is just "tar -cv -f file.tar". We call this file tarfile. If there is support for compression, such as "tar -jcv -f file.tar.bz2", we call it a tarball (tar ball?)! This is just a basic title, but many books and the Internet will use the name of this tarball! So I have to introduce it to you.
In addition, tar can not only package data into files, but also package files into certain special devices. For example, tape drive (tape) is a common example. Since the tape drive is a one-time read/write device, we cannot use commands such as cp to copy! Then if you want to back up /home, /root, /etc to a tape drive (/dev/st0), you can use: "tar -cv -f /dev/st0 /home /root /etc", it is very simple and easy Bar! It is very common for tape drives to be used in backups (especially enterprise applications)!
Special Application: Utilizing Pipeline Commands and Data Flows
In the use of tar, there is one of the most special ways, which is to redirect the data flow through standard input/output (standard input/standard output), and the pipeline command (pipe) method, the files to be processed are packaged and decompressed to the target directory. We will introduce more detailed data about data flow redirection and pipeline commands in Chapter 11 bash. Let’s take a look at an example first!
# 1. Pack the entire /etc directory and unzip it in /tmp
[root@www ~]# cd /tmp
[root@www tmp]# tar -cvf - / etc | tar -xvf -
# This action is a bit like cp -r /etc /tmp~ It still has its uses!
# What should be noted is that the output file becomes - and the input file also becomes -, and there is another | ~
# These represent standard output, standard input and pipeline commands respectively!
# In simple terms, you can think of - as a device (buffer) in memory.
# For more detailed data flow and pipeline commands, please turn to the bash chapter!
In the above example, we want to "copy the data under /etc directly to the current path, which is under /tmp", but we feel that using cp -r is a bit troublesome. Then just package it in this way. The - in the command means the packaged file! Since we don’t want the intermediate file to exist, we use this method to copy it!
The above is the content of the tar command commonly used in Note 004 Linux. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!