By performing this lab, students would learn how to work with archive files.
In this lab, you'll perform the following tasks:
- Create archive files using tar with and without compression
- Compress and uncompress files into a gzip archive file
- Compress and uncompress files into a bzip2 archive file
- Use zip and unzip to compress and Uncompress archive files
Part 1:overview
In the this task, we'll use gzip
, bzip2
and to tar
zip/unzip
Archive and restore files. These commands is designed to either merge multiple files into a single file or compress a large file into a smaller one. In some cases the commands would perform both functions.
The task of archiving data is important for several reasons, including and not limited to the following:
- Large files May is difficult to transfer. Making These files smaller helps make transfer quicker.
- Transferring multiple files from one system to another can become tedious when there is many files. Merging them into a single file for transport makes this process easier.
- 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, a beginning Linux user may experience stems from the following question:why is ther e so many different archiving commands? The answer is that these commands has different features (for example, some of them allow you to password protect the arc Hive file) and compression techniques used.
The most important things to know for now what these different commands function. Over time you'll learn to pick the correct archive tool for any given situation.
Step 1:
Use the following command to create a archive of the tar
/etc/udev
directory. Save the backup in the ~/mybackups
directory:
Cdmkdir MYBACKUPSTAR–CVF MYBACKUPS/UDEV.TAR/ETC/UDEVLS mybackups
Your output should is 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's 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 the Create tape backups, but today it's more commonly used to create archive files.
You aren't required to use the
.tar
Extension to the archive file name, however it was helpful for determining the file type. It is considered the "good style" when sending a 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 is similar to the following:
Notice that files were backed up recursively using relative path names. This is important because if you extract the files, they would be placed in your current directory, not override the Curr ent files.
Step 3
To create a file which is tar
compressed use -z
option:
tar –zcvf mybackups/udev. tar. gz/etc/udevls –lh mybackups
Your output should is similar to the following:
Notice the difference in size; First backup (Kbytes) is larger than the second backup (1.2 Kbytes).
The option makes use of the -z
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/udevls etc/udev/rules.d
Your output should is similar to the following:
If you wanted the files to ' go back ' into their original location, you could first to the cd
/
Dire Ctory and then run the tar
command. However, in this example, this would require you to being logged in as a administrator because creating files in the /etc
directory can Only is done by the administrator.
Step 5:
To add a file to an existing archive with the option to the -r
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/hoststar –tvf udev. Tar
Your output should is similar to the following:
Step 6:
In the following examples, you'll use and to gzip
gunzip
Compress and uncompress a file. Execute the following commands to compress a copy of the words
file:
CP /usr/share/dict/words. ls -l wordsgzip wordsls -l words.gz
Your output should is 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 byte s in the example above).
Very Important:when gzip
, the original file is replaced by the zipped file. In the example above, the file words is replaced with words.gz.
When you unzip the file, the zipped file is replaced with the original file.
Step 7:
Execute the following commands to uncompress the words.gz
file:
ls -l words.gzgunzip words.gzls -l words
Your output should is similar to the following:
Linux provides a large number of compression utilities in addition to gzip/gunzip
. Each of them has pros and cons (faster compression, better compression rates, more flexible, more portable, faster Decomp Ression, etc.).
gzip/gunzip
The commands is very popular in Linux and you should is aware that is bzip2/bunzip2
also popular on some Linux distributions. It is fortunate, the most of the functionality (the gzip/gunzip
"the" ) and options are the same as.
Step 8:
Using bzip2
and 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 file:
ls -l wordsbzip2 wordsls -l words.bz2
Your output should is similar to the following:
If you compare the resulting. bz2 file size (335405) to the. gz file Size (255996) from Step #7, you'll notice that Did gzip
a better job compressing this particular file.
Step 9:
Execute the following commands to uncompress the words.bz2
file:
ls -l words.bz2bunzip2 words.bz2ls -l words
While gzip
and archive files is 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 have built-in support to Extract zip
archive files.
Therefore, if you be planning to share a archive with Windows users, it's usually preferred to use the zip
ar Chive type. Unlike gzip
bzip2
and, when a file zip
was compressed with the command, a of the copy o Riginal file is compressed and the original remains uncompressed.
Step Ten
Use the zip command to compress the words file:
zip words. Zip words ls -l words. Zip
Your output should is similar to the following:
The first argument (Words.zip in the example above) of the "The command is the", the zip
file name, the wish to create. The remaining arguments (words in the example above) is the files that is want placed in the compressed file.
You aren't required to use theExtension to the
.zip
compressed file name; however it was helpful for determining the file type. It is also considered the "good style" when sending a archive file to another person.
Step One:
Compress the directory and its /etc/udev
contents with zip
compression:
Zip -R udev. zip /etc/udevls -l udev. Zip
Your output should is similar to the following:
The tar
command discussed earlier in this lab automatically descends through any subdirectories of a directory s Pecified to be archived. With bzip2
the, gzip
, and zip
commands the -r
option must is specified in order to perform RECU Rsion into subdirectories.
Step:
To view the contents of a zip
archive, use with the option with the -l
unzip
command:
Unzip-l Udev.zip
Your output should is similar to the following:
Step:
zip
to extract the archive with the unzip
command without any options. In this example we first need to delete the files, were created in the earlier tar example:
RM -R etcunzip Udev. Zip
Your output should is similar to the following:
Lab-archiving and Unarchiving Files