Archiving and compressing
Notes:
Gzip:
-
Large files can be gzipped the files to save space.
-
The command to gzip the file readme is: gzip readme
This will compress the readme file and save the result in a file called
readme.gz.
-
The command to gunzip the file readme.gz is: gunzip readme.gz
-
Gzip supports 9 levels of compression; 1 being the fastest and least compressed;
9 being the slowest and most compressed; 6 being the default. To get the
best compression, use the command: gzip -9 readme
Tar:
Tar is used to pack the entire contents of a directory or directories
into a single file called a tarball which can then be backed up to tape,
gzipped and saved, or transferred to another system. Tar preserves the entire
directory organization including file ownership, permissions, links, and
the directory structure. Tar stands for tape archive because it was developed
to backup files on tape. Now, most of the Linux software available on the
Internet is stored as gzipped tar files.
The most commonly used tar functions are:
-
c - create an archive
-
x - extract files from an archive
-
t - list the contents of an archive
Additionally, there are commonly used options:
-
v - verbose
-
f filename - use the specified file
-
z - gzip/gunzip
Examples:
-
Back |up the contents of the home directory for alterego (/home/alterego) in
a tarball called a.tar on a floppy disk.
mount /floppy
cd /home
tar -cvf /floppy/a.tar alterego
Explanation:
-
Change to the parent of the /home/alterego directory.
-
Create a backup of alterego in the file /floppy/a.tar.
-
Now, check the contents of the tarball that you just created.
cd /floppy
tar -tvf a.tar
-
Back |up the contents of the etc directory in an archive called etc.tar . Make
sure that the archive is created in your own home directory.
cd /
tar -cvf ~/etc.tar etc
Explanation:
-
Change to the parent of the /etc directory.
-
Create a backup of etc in the file ~/etc.tar
-
Back |up and compress the contents of the home directory into the tarball home.tgz
on a floppy disk.
mount /floppy
cd /
tar -cvzf /floppy/home.tgz home
Explanation:
-
change to the parent of the /home directory
-
Create a compressed backup of home in the file /floppy/home.tgz.
-
Now check the contents of the archive that you just created.
cd /floppy
tar -tvzf home.tgz
-
Unpack the archive home.tgz on your floppy.
cd /floppy
tar -xvzf home.tgz
Explanation:
-
change to the /floppy directory
-
unpack and unzip the tarball home.tgz
Exercises:
-
Practice gunzip.
-
Find the file passwd.1.gz and copy it to your own home directory.
-
Gunzip this file.
-
Unpack a tarball.
-
Find the archive: vtree.tgz and copy it to your own home directory.
-
Unpack the archive vtree.tgz .
-
What directories/files are created?
-
More gzipping/gunzipping:
-
Gunzip the file vtree.1.gz . This file was created when you unpacked vtree.tgz.
-
Gzip all the files in your home directory.
-
Create and unpack a tar archive of the directory /home.
-
Mount /floppy .
-
Change to the directory /home.
-
Create a compressed archive of your own home directory called /floppy/mine.tgz
.
-
Change to the directory /floppy and unpack mine.tgz .
-
More practice:
-
Change to the directory /var.
-
From this directory, create a tarball of the directory spool/mail called
/floppy/mail1.tar
-
Change to the directory /var/spool.
-
From this directory, create a tarball of the directory mail called
/floppy/mail2.tar
-
Change to the /floppy directory.
-
List the contents of mail1.tar ( tar -tvf mail1.tar ).
-
List the contents of mail2.tar.
-
What is the difference between the two tarballs? __________
Questions & Answers:
-
Which command will give best compression?
a. gzip -1 lab10.html
b. gzip -9 lab10.html
Answer: b
-
Which command will display the contents of a gzipped file on the screen but
leave the actual file unchanged?
a. gzip -c lab10.gzip
b. gzip -d lab10.gzip
Answer: a
-
Which command will create archive the labs directory in a tarball called
labs.tar?
a. tar -cvf labs labs.tar
b. tar -cvf labs.tar labs
Answer: b
-
Which command will unpack the tarball labs.tar?
a. tar -xf labs.tar
b. tar -tf labs.tar
Answer: a
Copyright ©
L.M.MacEwan