0% found this document useful (0 votes)
25 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)
25 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
You are on page 1/ 4

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
file1.txt, file2.txt, and file3.txt. Use the tar command to create an archive
named testdir.tar of the testdir directory. List the contents of the
testdir.tar archive without extracting it.

2. Ensure testdir.tar archive from Question 1 exists. Create a directory


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

4. Ensure sample.txt.gz from Question 3 exists. Decompress sample.txt.gz


using the gunzip command. Verify that the original sample.txt 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


doc1.txt, doc2.txt, and doc3.txt. Use the tar command with gzip
Post Lab
compression to create a compressed tarball project.tar.gz. 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 .tar.gz files.

Common gzip commands:

• Compressing a file: gzip filename

• Decompressing a file: gunzip filename.gz

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/file1.txt testdir/file2.txt testdir/file3.txt

# Step 3: Create a tar archive of the directory

tar -cvf testdir.tar testdir/

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

tar -tvf testdir.tar


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

tar -tvf testdir.tar: Lists the contents of the testdir.tar archive without extracting it.

2. Extract the Archive into a New Directory

# Step 1: Ensure the testdir.tar 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 testdir.tar into the extracted directory

tar -xvf testdir.tar -C extracted/

mkdir extracted: Creates a directory named extracted.

tar -xvf testdir.tar -C extracted/: Extracts the contents of testdir.tar 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 sample.txt with some sample text

echo "This is some sample text." > sample.txt

# Step 2: Compress the sample.txt file using gzip

gzip sample.txt

# Step 3: Verify that the file has been compressed

ls -l

gzip sample.txt: Compresses the sample.txt file, resulting in sample.txt.gz.

ls -l: Lists the contents of the directory, showing the compressed file sample.txt.gz.

4. Decompress the File Using gunzip

# Step 1: Ensure the sample.txt.gz file exists (created in step 3)


# Step 2: Decompress the sample.txt.gz file

gunzip sample.txt.gz

# Step 3: Verify that the original sample.txt file is restored

ls -l

gunzip sample.txt.gz: Decompresses sample.txt.gz, restoring the original sample.txt


file.

ls -l: Lists the contents of the directory, confirming that sample.txt has been restored.

You might also like