标签:
By performing this lab, students will learn how to work with archive files.
In this lab, you will perform the following tasks:
In this task, we will use gzip, bzip2, tar and zip/unzip to archive and restore files. These commands are designed to either merge multiple files into a single file or compress a large file into a smaller one. In some cases the commands will perform both functions.
The task of archiving data is important for several reasons, including but not limited to the following:
One potential area of confusion that a beginning Linux user may experience stems from the following question: why are there so many different archiving commands? The answer is that these commands have different features (for example, some of them allow you to password protect the archive file) and compression techniques used.
The most important things to know for now is how these different commands function. Over time you will learn to pick the correct archive tool for any given situation.
Use the following tar command to create an archive of the /etc/udevdirectory. Save the backup in the ~/mybackups directory:
cd mkdir mybackups tar –cvf mybackups/udev.tar /etc/udev ls mybackups
Your output should be similar to the following:

The tar command is used to merge multiple files into a single file. By default it does not compress the data.
The -c option tells the tar command to create a tar file. The -v option stands for "verbose", which instructs the tar command to demonstrate what it is doing. The -f option is used to specify the name of the tar file.
FYI: tar stands for Tape ARchive. This command was originally used to create tape backups, but today it is more commonly used to create archive files.
.tar extension to the archive file name, however it is helpful for determining the file type. It is considered "good style" when sending an archive file to another person.Display the contents of a tar file (t = list contents, v = verbose, f = filename):
tar –tvf mybackups/udev.tar
Your output should be similar to the following:

Notice that files were backed up recursively using relative path names. This is important because when you extract the files, they will be placed in your current directory, not override the current files.
To create a tar file that is compressed use -z option:
tar –zcvf mybackups/udev.tar.gz /etc/udev ls –lh mybackups
Your output should be similar to the following:

Notice the difference in size; first backup (10 Kbytes) is larger than the second backup (1.2 Kbytes).
The -z option makes use of the gzip utility to perform compression.
Extract the contents of an archive. Data is restored to the current directory by default:
cd mybackups tar –xvf udev.tar.gz ls ls etc ls etc/udev ls etc/udev/rules.d
Your output should be similar to the following:

If you wanted the files to "go back" into their original location, you could first cd to the / directory and then run the tar command. However, in this example, this would require you to be logged in as an administrator because creating files in the/etc directory can only be done by the administrator.
To add a file to an existing archive, use the -r option to the tar command. Execute the following commands to perform this action and verify the existence of the new file in the tar archive:
tar -rvf udev.tar /etc/hosts tar –tvf udev.tar
Your output should be similar to the following:

In the following examples, you will use gzip and gunzip to compress and uncompress a file. Execute the following commands to compress a copy of thewords file:
cp /usr/share/dict/words . ls -l words gzip words ls -l words.gz
Your output should be similar to the following:

Notice the size of the zipped file (255996 bytes in the example above) is much smaller than the original file (938848 bytes in the example above).
Very important: When you use gzip, the original file is replaced by the zipped file. In the example above, the file words was replaced with words.gz.
When you unzip the file, the zipped file will be replaced with the original file.
Execute the following commands to uncompress the words.gz file:
ls -l words.gz gunzip words.gz ls -l words
Your output should be similar to the following:

Linux provides a large number of compression utilities in addition togzip/gunzip. Each of them have pros and cons (faster compression, better compression rates, more flexible, more portable, faster decompression, etc.).
The gzip/gunzip commands are very popular in Linux, but you should be aware that bzip2/bunzip2 are also popular on some Linux distributions. It is fortunate that most of the functionality (the way you run the commands) and options are the same as gzip/gunzip.
Using bzip2 and bunzip2 to compress and uncompress a file is very similar to using gzip and gunzip. The compressed file is created with a .bz2 extension. The extension is removed when uncompressed. Execute the following commands to compress a copy of the words file:
ls -l words bzip2 words ls -l words.bz2
Your output should be similar to the following:

If you compare the resulting .bz2 file size (335405) to the .gz file size (255996) from Step #7, you will notice that gzip did a better job compressing this particular file.
Execute the following commands to uncompress the words.bz2 file:
ls -l words.bz2 bunzip2 words.bz2 ls -l words

While gzip and bzip archive files are commonly used in Linux, the zip archive type is more commonly used by other operating systems, such as Windows. In fact, the Windows Explorer application has built-in support to extract zip archive files.
Therefore, if you are planning to share an archive with Windows users, it is usually preferred to use the zip archive type. Unlike gzip and bzip2, when a file is compressed with the zip command, a copy of the original file is compressed and the original remains uncompressed.
Use the zip command to compress the words file:
zip words.zip words ls -l words.zip
Your output should be similar to the following:

The first argument (words.zip in the example above) of the zip command is the file name that you wish to create. The remaining arguments (words in the example above) are the files that you want placed in the compressed file.
.zip extension to the compressed file name; however it is helpful for determining the file type. It is also considered "good style" when sending an archive file to another person.
Compress the /etc/udev directory and its contents with zip compression:
zip -r udev.zip /etc/udev ls -l udev.zip
Your output should be similar to the following:

The tar command discussed earlier in this lab automatically descends through any subdirectories of a directory specified to be archived. With the bzip2,gzip, and zip commands the -r option must be specified in order to perform recursion into subdirectories.
To view the contents of a zip archive, use with the -l option with the unzipcommand:
unzip -l udev.zip
Your output should be similar to the following:

To extract the zip archive, use the unzip command without any options. In this example we first need to delete the files that were created in the earlier tar example:
rm -r etc unzip udev.zip
Your output should be similar to the following:

Lab - Archiving and Unarchiving Files
标签:
原文地址:http://www.cnblogs.com/elewei/p/4820251.html