Search This Blog

Thursday, January 24, 2013

Linux Archive Tools (tar, star, gzip, bzip2, zip, cpio)

This article discusses the archiving tools available in Linux, with specific reference to the information needed for the RHCSA EX200 and RHCE EX300 certification exams.
Remember, the exams are hands-on, so it doesn't matter which method you use to achieve the result, so long as the end product is correct.
All the commands in this article have many options in addition to the basic ones being used here. Please check the man pages for each command. The examples will use the following files.
mkdir -p /tmp/test-dir/subdir1
mkdir -p /tmp/test-dir/subdir2
mkdir -p /tmp/test-dir/subdir3
mkdir -p /tmp/extract-dir
touch /tmp/test-dir/subdir1/file1.txt
touch /tmp/test-dir/subdir1/file2.txt
touch /tmp/test-dir/subdir2/file3.txt
touch /tmp/test-dir/subdir2/file4.txt
touch /tmp/test-dir/subdir3/file5.txt
touch /tmp/test-dir/subdir3/file6.txt
Extracts assume the "/tmp/extract-dir" directory is empty.

tar

Create an archive.
# cd /tmp
# tar -cvf archive1.tar test-dir
Check the contents.
# tar -tvf /tmp/archive1.tar
Extract it.
# cd /tmp/extract-dir
# tar -xvf /tmp/archive1.tar

star

The star command may not be installed by default, but you can install it with the following command.
# yum install star
Create an archive.
# cd /tmp
# star -cv f=archive2.star test-dir
Check the contents.
# star -tv f=/tmp/archive2.star
Extract it.
# cd /tmp/extract-dir
# star -xv f=/tmp/archive2.star

gzip

The gzip command compresses the specified files, giving them a ".gz" extension. In this case we will use it to compress a ".tar" file.
# cd /tmp
# tar -cvf archive3.tar test-dir
# gzip archive3.tar
The "-z" option of the tar command allows you to do this directly.
# cd /tmp
# tar -cvzf archive3.tar.gz test-dir
The files are uncompressed using the gunzip command.
# gunzip archive3.tar.gz
The "-z" option of the tar command allows you to directly ungzip and extract a ".tar.gz" file.
# cd /tmp/extract-dir
# tar -xvzf /tmp/archive3.tar.gz

bzip2

The bzip2 command is similar to the gzip command. It compresses the specified files, giving them a ".bz2" extension. In this case we will use it to compress a ".tar" file.
# cd /tmp
# tar -cvf archive4.tar test-dir
# bzip2 archive4.tar
The "-j" option of the tar command allows you to do this directly.
# cd /tmp
# tar -cvjf archive4.tar.bz2 test-dir
The files are uncompressed using the bunzip2 command.
# bunzip2 archive4.tar.bz2
The "-j" option of the tar command allows you to directly bunzip2 and extract a ".tar.bz2" file.
# cd /tmp/extract-dir
# tar -xvjf /tmp/archive4.tar.bz2

zip

Create an archive.
# cd /tmp
# zip -r archive5.zip test-dir
Check the contents.
# unzip -l archive5.zip
Extract it.
# cd /tmp/extract-dir
# unzip /tmp/archive5.zip

cpio

Create an archive.
# cd /tmp
# find test-dir | cpio -ov > archive6.cpio
Check the contents.
# cpio -t < /tmp/archive6.cpio
Extract it.
# cd /tmp/extract-dir
# cpio -idmv < /tmp/archive6.cpio

No comments:

Post a Comment