码迷,mamicode.com
首页 > 其他好文 > 详细

Lab - Archiving and Unarchiving Files

时间:2015-09-18 21:40:47      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

By performing this lab, students will learn how to work with archive files.

 

In this lab, you will perform the following tasks:

  1. Create archive files using tar with and without compression
  2. Compress and uncompress files into a gzip archive file
  3. Compress and uncompress files into a bzip2 archive file
  4. Use zip and unzip to compress and uncompress archive files

 

Part 1: Overview

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:

  1. Large files may be difficult to transfer. Making these files smaller helps make transfer quicker.
  2. Transferring multiple files from one system to another can become tedious when there are many files. Merging them into a single file for transport makes this process easier.
  3. Files can quickly take up a lot of space, especially on smaller removable media like thumb drives. Archiving reduces this problem.

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.

 

Step 1:

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.

You are not required to use the .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.
 
 

Step 2

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.

 

Step 3

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.

 

Step 4

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.

 

Step 5:

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:

技术分享

 

Step 6:

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.

 

Step 7:

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.

 

Step 8:

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.

 

Step 9:

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.

 

Step 10

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.

You are not required to use the .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.

 

Step 11:

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.

 

step 12:

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:

技术分享

 

Step 13:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!