You are on page 1of 11

Compression Utilities in Linux Lab # 6

LAB # 6

Compression Utilities in Linux

How to Archive / Compress Files in Linux

1. Zip/Unzip
Zip is useful for packaging a set of files for distribution, for archiving files,
and for saving disk space by temporarily compressing unused files or directories. Zip
puts one or more compressed files into a single ZIP archive, along with information
about the files (name, path, date, time of last modification, protection, and check
information to verify file integrity). An entire directory structure can be packed into a
ZIP archive with a single command.

Zip automatically chooses the better of the two for each file. Compression ratios of
2:1 to 3:1 are common for text files.

Examples:

 To Compress a file with zip, type the following:

$ Zip <filename.zip> [file]

$ Zip Test.zip file1

OR $ Zip Test.z file1

 To Extract the compressed files type the following:

$ Unzip Test.zip

Uncompressed files in the zip archive named Test.zip into the current directory.

 Tip:

Use the -d option to specify a directory other than the current directory.

$ unzip Test.zip -d /adir

Create a zip archive named code.zip that contains all of the files in the current
directory whose filenames end with .c.

Submitted By: Ali Murad


Submitted To: Muhib Khan
Compression Utilities in Linux Lab # 6

$ zip code.zip *.c


If you want to include entire directory structures in your zip archive use the -r
(recursive) option. For example,

$ Zip -r code.zip cdir

Includes the entire directory structure beginning with cdir in the zip archive code.zip

 You can zip multiple files at one time. For example:

$ Zip -r myfiles.zip file1 file2 file3

2. Tar
The Linux tar command packages files and directories into one large file
called a tar archive which can be expanded to recover the original files at a later time.
Tar was originally created to transfer files to tape but today it is most often used to
transport files over a network. Tar does not compress files. If you include 100 MB of
files in a tar archive, the archive will require 100 MB.

 The basic format of the tar command is:

$ tar [options] file-list

The file-list includes all files that should be included in or extract from the archive. If
file-list contains a directory, tar archives or extracts the entire directory structure
including all files and subdirectories.

A few of the most useful tar options are shown below.

Option Description
 c   Create a new archive.
 r   Append files to an existing archive.
 u   Update an existing archive. Appends files that are not already in an archive or
have been modified.
 t   Table of contents. Prints a list of all files in the file-list that are contained in
the archive. If no file-list is specified, lists all files in the archive.
 x   Extract files from an archive. If no file-list is specified, extracts the entire
archive.
 v   Verbose. Prints detailed information while creating, extracting or listing the
contents of an archive.

Submitted By: Ali Murad


Submitted To: Muhib Khan
Compression Utilities in Linux Lab # 6

Creating a Tar Archive

 The basic format for creating a tar archive is:

tar cvf archive-name file-list

Where archive-name is the filename of the tar archive and file-list is a list of all files
that should be included in the archive. By convention archive names end with the
suffix .tar. For example,

$ ls -F
bin/ docs/ public_html/ temp/

$ tar cvf docs.tar docs


docs/
docs/memo
docs/project1/
docs/project1/bugs

In this example, the archive is named docs.tar and the file-list consists of a single
directory, docs.
The first argument to tar, “cvf”, is the tar “command”. c tells tar to create a new
archive file. v forces tar to use verbose mode, printing each file name as it is archived.
The f option tells tar that the next argument, docs.tar, is the name of the archive to
create. The rest of the arguments to tar are the file and directory names to add to the
archive.

Adding to and Updating an Existing Tar Archive

 You can append files to the end of an archive with the r option. The basic format
for appending files to a tar archive is:

tar rvf archive-name file-list

For example, suppose you create a new file in the docs directory structure,
docs/project1/code3.c.

$ ls -F
bin/ docs/

$ tar rvf docs.tar docs/project1/code3.c


docs/project1/code3.c

Tar will add the docs/project1/code3.c file to the end of the archive.

Submitted By: Ali Murad


Submitted To: Muhib Khan
Compression Utilities in Linux Lab # 6

 You can update an existing archive using the u option. The basic format for
updating a tar archive is:

tar uvf archive-name file-list


Tar will look at the file-list. A file that does not already exist in archive-name will be
added. A file that has been modified since it was included in archive-name will also
be added. Otherwise, the file is ignored. When you add a file that already exists both
versions of the file will remain in the archive; however, only the newest version will
be extracted. For example,
$ tar uvf docs.tar docs

docs/project2/
docs/project2/chap1

Listing the Contents of a Tar Archive

 The format for listing the contents of a tar archive is:

tar tvf archive-name

For example,

$ tar tvf docs.tar


drwxr-xr-x lizr/staff 0 2002-07-18 17:17:10 docs/
-rw-r--r-- lizr/staff 10 2002-07-18 17:16:59 docs/memo
drwxr-xr-x lizr/staff 0 2002-07-18 17:17:27 docs/project1/

Lists the contents of the docs.tar archive. The v (verbose) option gives output similar
to the ls command's long file listing (-l) option. Without the v option, tar lists the
filenames in the archive with no additional information. For example,

$ tar tf docs.tar
docs/
docs/memo
docs/project1/

Extracting from a Tar Archive

 The format for extracting the contents of a tar archive is:

tar xvf archive-name

For example, extract all files from the docs.tar archive.

Submitted By: Ali Murad


Submitted To: Muhib Khan
Compression Utilities in Linux Lab # 6

$ tar xvf docs.tar


docs/
docs/memo
docs/project1/

Because we used the v (verbose) option, tar lists each file as it is extracted.

Important: While extracting, tar will overwrite any file that already exists without
warning.

Summary
tar cvf archive-name file-list
Create an archive.
tar rvf archive-name file-list
Append files to an archive.
tar uvf archive-name file-list
Update files in an archive.
tar tvf archive-name [file-list]
List files in an archive. Without file-list lists all files.
tar xvf archive-name [file-list]
Extract (restore) files from an archive. Without file-list extracts all files.

3. gzip/gunzip
Unlike archiving programs for Linux, tar does not automatically compress files as it
archives them. The gzip command compresses a file (it need not be a tar file). The
command

Examples:

 To Compress a file with gzip, type the following:

$ gzip <filename>

$ gzip file1

 To Extract the compressed files type the following:

$ gunzip file1.gz

Uncompressed files in the guzip archive named file1.gz into the current
directory.

OR

Submitted By: Ali Murad


Submitted To: Muhib Khan
Compression Utilities in Linux Lab # 6

$ gzip -9 backup.tar

Compresses backup.tar and leaves you with backup.tar.gz, a compressed version of


the file. The -9 switch tells gzip to use the highest compression factor.

4. bzip2/bunzip2
While relatively new addition to the Linux compression scene, the bzip2
compressor appears to produce better compression than either zip or gzip. The syntax
for bzip2 has intentionally been made similar to gzip.

Examples:

 To Compress a file with bzip2, type the following:

$ bzip2 <filename>

$ bzip2 file1

 To Extract the compressed files type the following:

$ bunzip2 file1.bz2

Cksum

A checksum is a number mathematically calculated from the contents of a file, for the
purpose of checking its integrity. A script might refer to a list of checksums for
security purposes, such as ensuring that the contents of key system files have not been
altered or corrupted.

Disk Usage by Directory


The du command shows a breakdown of disk usage by directory. For example, cd to
your home directory and type "du -k" (The -k option displays output in Kilobytes)

$ cd ~
$ du –k

1 ./News
3 ./mail
713 ./public_html/photo
1363 .

Submitted By: Ali Murad


Submitted To: Muhib Khan
Compression Utilities in Linux Lab # 6

The output lists the number of Kilobytes used by each subdirectory. In this example,
the user is using a total of 1363 Kilobytes or about 1.5 Megabytes of disk space.

The -s option causes du to work in summary mode. In summary mode, du only


outputs the total amount of disk space used by a directory structure. For example

$ du -k -s
1363.

Uses of Pipes

A pipes is a way to connect the output of one program to the input of another program
without any temporary file.

A pipe is nothing but a temporary storage place where the output of one command is
stored and the passed as the input for second command.Pipes are used to run more
than two commands(Multiply commands) from same command line.

Syntax:command 1/command 2
Command using pipes Meaning or use of pipes
$Is/more Here the output of Is command is given
as input to more command so that output
is printed one screen full page at a time
$ who/ we-1 Here output of who command is given as
input to we command so that it will
number of user who logon to system
$ who/ sort Here output of Is command is given as
input to sort command so that it will print
sorted list of files in current directory.
Here output of who command is given as
$ who/ grep <username> Input to grep command so that it will
print the particular user name if he/she is
logon or nothing is printed (To see
particular user is logon or not)

Use of Pipes as Filters

Many imes we don’t need all of the output produced by a command, In


such a case,we can filter the desired information from the output produced by a
command,Filtering means extracting useful data and trowing away the rest.
If Linux command accepts its input from the standard input and produces its output on
standard output is known as a filter.

A filter performs some kind of process on the input and gives output,

Example:

Submitted By: Ali Murad


Submitted To: Muhib Khan
Compression Utilities in Linux Lab # 6

$ Who |grep<username>
In the above example we use a pipe to filter out our required information.

The T-Junction
This is special type of pipe similar to a T pipe junction in real life.This is used
to redirect incoming data in a pipe to more than one place.
The”tee” command is used to from a T-junction.It takes its input from stdin and
writes the same thing to stout as well as to another file at the same time. Consider the
following example;

$ who| tee whofile

Listing Directory Tree Graphics

Use tree to output an ASCII text tree graph of a given directory tree.
To output a tree graph of the current directory and all its subdirectories,type:
$tree
$ tree
| -- projects
| !- -current
| - - old
| |--1
| --2
| trip

--Schedule.txt

4 directorie, 3files
$
In the preceding example,a tree graph is drawn showing the current
directory,which contains the two directories ‘projects’ and ‘trip’; the ‘projects’
directory in turn contains the directories ’current’ and ‘old’.
The output a tree graph of a specific directory tree,give the name of that
directory tree as an argument.
To ouput a tree graph of your home directory and all
its subdirectories,type:$tree~
To output a graph of a directory tree containing directory names only,use the”-
d” optins. This is useful for outputting a directory tree of the entire system, or for
getting z picture of a particular directory tree.

Lab Work: Check the “-d” options youself.

Finding the Largest Files in a Directory

Submitted By: Ali Murad


Submitted To: Muhib Khan
Compression Utilities in Linux Lab # 6

To find the largest files in a given directory,use Is to list its contents with the “-
S” option,which sorts files in desecending order by their size(normally,Is output
files alphabetically),
To list the files in the current directory,with their attributes,sorted with the
largest files first,type:
$Is-IS

Finding the Smallest Files in Directory


To list the contents of a directory with the smallest files first,use is with both the”-S”
and ”-r” options,which reverse the sorting order of the listing.

To list files in the current directory and their attributes,sorted from smallest to
largest, type:

$Is-ISr

find[pathnames][condition]
An extremely useful command for finding particular groups of files.Find descends the
directory tree beginning at each pathname and location files that meet the specified
condition.

find –name ‘mypage’.htm’


In the above command the system would search for any file named my
page.htm in the current directory and any subdirectory.

find –name ‘mypage’.htm’


In the above command the system would search for any file named my
page.htm on the root and all subdirectories from the root.

find-name’file*’
In the above example the system would search for any file beginning with file in the
current directory and any subdirectory.

find-name’*’-size+1000k
in the above example the system would search for any that is larger then
1000k.find/dev-user”peter”|more
The above command prints the filename for all devices owned by user”peter”.Printing
the filename is the default”action”of find,so it does not have to be specified if this all I
need.

Finding Where a Command Is Located

Use”which”command to find the full path name of a tool or application from its
base file name;when you give the base file name as an option,which output the
absolution file name of the command that would have run had you typed it.This is

Submitted By: Ali Murad


Submitted To: Muhib Khan
Compression Utilities in Linux Lab # 6

useful when you are not sure whether or not a particular command is istalled on the
system.
To find out whether per1 installed on your sytem,and if so,where it resides,type:

$ which perl
/usr/bin/perl

In this example,which output”/usr/bin/per1”,indicating that the per1 binary is installed


in the”/usr/bin”directory.

Lab Work

Pwd,rmdir,telnet,telnet,tar

Assignment:

1. Task: Explain different compression and utility commands in linux.

 Mkdir ruby,to make a directory,them cd ruby

 $Cat > file1 My name is Osama and ctrl+z to save .

 $ Cat> file 2 This is my lab and them ctrl+z to save it.

 If we want to zip our files we use this command.

 $ zip file 1.zip file


(It zip the file 1)

 If we want to zip multiple files so we use this command.


e:g $zip test.zip file1 file 2 file 3

 If we want to uncompressed our files


E:g $ unzip test.zip

 If we want to compressed. E:g $ gzip file1

 If we want to extract compressed file we use this command as:


E:g $ unzip test.zip

 $ zip. Code zip*.c


This command is used to contains all of the files in the current directory whose
filenames end with .c

Submitted By: Ali Murad


Submitted To: Muhib Khan
Compression Utilities in Linux Lab # 6

 If we want to create a tar archive so we use this command as


E:g tar cuf archive-name file – list

 And check by this command


$ ls-f
 If we want to see our diskusage memory so we use this command. E:g $ du-k-s
Using pipe command,for specific or multiple commands.
E:g who 1 grepruby
 It shows who the user logon it specially shows the user name which are logged in the
system.

Submitted By: Ali Murad


Submitted To: Muhib Khan

You might also like