0% found this document useful (0 votes)
43 views4 pages

Lab 4 Solutions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

Lab 4 Solutions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab Experiment - 4

To understand and apply file archiving, compression, and decompression


AIM
techniques using tar and gzip in a Linux environment.

These experiments cover the creation, listing, extraction, compression, and


decompression of files and directories using the tar and gzip commands.
Description
Students will gain practical experience in managing archives and compressed
files efficiently.

Pre-Lab
Tar, gzip
Commands
1. Create a directory named testdir. Inside testdir, create files named
[Link], [Link], and [Link]. Use the tar command to create an archive
named [Link] of the testdir directory. List the contents of the
[Link] archive without extracting it.

2. Ensure [Link] archive from Question 1 exists. Create a directory


named extracted. Extract the contents of [Link] into the extracted
directory.
In Lab
3. Create a text file named [Link] with some sample text. Compress
[Link] using the gzip command. Command: gzip [Link]. Verify
that the file has been compressed by listing the directory contents. Note
the new file [Link].

4. Ensure [Link] from Question 3 exists. Decompress [Link]


using the gunzip command. Verify that the original [Link] file is
restored.

How do you create a tarball of a directory?


What does the -z option do when used with the tar command?
Viva Questions How do you specify the extraction directory using the tar command?
What does the -x option in the tar command stand for?
What is the file extension for files compressed with gzip?

1. Create a directory named project. Inside project, create files named


[Link], [Link], and [Link]. Use the tar command with gzip
Post Lab
compression to create a compressed tarball [Link]. List the
contents of the compressed tarball without extracting it.
tar (Tape Archive)

tar is a Linux/Unix command-line utility used for combining multiple files and directories into a
single archive file. This archive, often referred to as a tarball, typically has a .tar extension. tar is
useful for creating backups or grouping files for easier storage or transfer. However, a .tar file is
not compressed by default; it only bundles files together.

Common tar commands:

• Creating an archive: tar -cvf archive_name.tar directory/

o -c: Create a new archive.

o -v: Verbosely list files processed.

o -f: Specifies the name of the archive file.

• Listing the contents of an archive: tar -tvf archive_name.tar

o -t: List the contents of the archive.

• Extracting an archive: tar -xvf archive_name.tar

o -x: Extract the files from the archive.

gzip (GNU zip)

gzip is a command-line utility used to compress files, reducing their size for storage or transfer.
The resulting compressed file typically has a .gz extension. gzip is often used in conjunction with
tar to create compressed archives, commonly referred to as .[Link] files.

Common gzip commands:

• Compressing a file: gzip filename

• Decompressing a file: gunzip [Link]

1. Create a Directory and Archive it Using tar

# Step 1: Create the directory

mkdir testdir

# Step 2: Create files inside the directory

touch testdir/[Link] testdir/[Link] testdir/[Link]

# Step 3: Create a tar archive of the directory

tar -cvf [Link] testdir/

# Step 4: List the contents of the tar archive without extracting it

tar -tvf [Link]


tar -cvf [Link] testdir/: Creates an archive named [Link] containing the testdir
directory and its contents.

tar -tvf [Link]: Lists the contents of the [Link] archive without extracting it.

2. Extract the Archive into a New Directory

# Step 1: Ensure the [Link] archive exists (already created in step 1)

# Step 2: Create a directory to extract the contents into

mkdir extracted

# Step 3: Extract the contents of [Link] into the extracted directory

tar -xvf [Link] -C extracted/

mkdir extracted: Creates a directory named extracted.

tar -xvf [Link] -C extracted/: Extracts the contents of [Link] into the extracted
directory. The -C option specifies the directory where the files should be extracted.

3. Compress a File Using gzip

# Step 1: Create a text file named [Link] with some sample text

echo "This is some sample text." > [Link]

# Step 2: Compress the [Link] file using gzip

gzip [Link]

# Step 3: Verify that the file has been compressed

ls -l

gzip [Link]: Compresses the [Link] file, resulting in [Link].

ls -l: Lists the contents of the directory, showing the compressed file [Link].

4. Decompress the File Using gunzip

# Step 1: Ensure the [Link] file exists (created in step 3)


# Step 2: Decompress the [Link] file

gunzip [Link]

# Step 3: Verify that the original [Link] file is restored

ls -l

gunzip [Link]: Decompresses [Link], restoring the original [Link]


file.

ls -l: Lists the contents of the directory, confirming that [Link] has been restored.

You might also like