Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


LPI Linux Essentials Exam 010-160 - Topic 3.1 - tar Archives

tar Overview

The tar command is a file archiving tool used in Linux systems. It allows you to create, view, extract, and manipulate files and directories within an archive. The tar command combines multiple files and directories into a single archive file, often referred to as a "tarball." The archive can be compressed using other compression tools like gzip, bzip2 or xz to reduce its size depending on the option provided to the tar command.

Create Practice Directory & Files

Before working with the tar command, let's create some directories and files to practice with:

Exercise: Create the required directories & files for testing the tar command with:

Create practice files and directories:

mkdir -p ~/tar-practice
cd ~/tar-practice
echo "This is file 1." > file1.txt
echo "This is file 2." > file2.txt
echo "This is file 3." > file3.txt
mkdir -p dir1
echo "This is file 4." > dir1/file4.txt
mkdir -p dir2
echo "This is file 5." > dir2/file5.txt

Confirm required files created:

ls -lRh

Output should resemble:

total 20K
drwxrwxr-x 2 dtvlinux dtvlinux 4.0K Oct 25 07:53 dir1
drwxrwxr-x 2 dtvlinux dtvlinux 4.0K Oct 25 07:53 dir2
-rw-rw-r-- 1 dtvlinux dtvlinux   16 Oct 25 07:53 file1.txt
-rw-rw-r-- 1 dtvlinux dtvlinux   16 Oct 25 07:53 file2.txt
-rw-rw-r-- 1 dtvlinux dtvlinux   16 Oct 25 07:53 file3.txt

./dir1:
total 4.0K
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 25 07:53 file4.txt

./dir2:
total 4.0K
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 25 07:53 file5.txt

Creating a Tar Archive

To create a tar archive, use the tar command with the -c (create) option. The -f archive-filename is used to specify the archive name. The list of files and directories to include in the archive follow afterwards.

The -v verbose flag is optional and shows the progress of the archiving taking place, which is useful for long running archives to gauge time remaining.

Exercise: Create a tar archive named archive.tar containing file1.txt, file2.txt, dir1, and dir2:

tar -cvf archive.tar file1.txt file2.txt dir1 dir2

The command output should be:

file1.txt
file2.txt
dir1/
dir1/file4.txt
dir2/
dir2/file5.txt

Viewing the Contents of a Tar Archive

To view the contents of a tar archive, use the -t (list) option:

Exercise: Display a list of files and directories included in the archive.tar archive:

tar -tvf archive.tar

The command output should resemble:

-rw-rw-r-- dtvlinux/dtvlinux 16 2023-10-25 07:53 file1.txt
-rw-rw-r-- dtvlinux/dtvlinux 16 2023-10-25 07:53 file2.txt
drwxrwxr-x dtvlinux/dtvlinux  0 2023-10-25 07:53 dir1/
-rw-rw-r-- dtvlinux/dtvlinux 16 2023-10-25 07:53 dir1/file4.txt
drwxrwxr-x dtvlinux/dtvlinux  0 2023-10-25 07:53 dir2/
-rw-rw-r-- dtvlinux/dtvlinux 16 2023-10-25 07:53 dir2/file5.txt

Extracting Files from a Tar Archive

To extract files from a tar archive, use the -x (extract) option followed by the archive file name:

Extract all files and directories from the archive.tar archive. They will be extracted relative to the directory you are in so removal of the files is required to prove extraction works:

Remove files and directories:

rm -rvf file1.txt file2.txt dir1 dir2

The output will look like:

removed 'file1.txt'
removed 'file2.txt'
removed 'dir1/file4.txt'
removed directory 'dir1'
removed 'dir2/file5.txt'
removed directory 'dir2'

Extract all files from the tar archive to restore the deleted files and directories:

tar -xvf archive.tar

The output will print the extracted files and directories:

file1.txt
file2.txt
dir1/
dir1/file4.txt
dir2/
dir2/file5.txt

Extracting Files from a Tar to a Specified Directory

To extract files from a tar archive into a specified directory, use the -C directory option:

Exercise: Extract all files and directories from the archive.tar archive. They will be extracted to the /tmp directory:

Extract files and directories into the /tmp directory:

tar -C /tmp -xvf archive.tar

Confirm files extracted to /tmp:

ls -lh /tmp/{dir{1,2},file{1,2}.txt}

Output should resemble the following output which shows file1.txt, file2.txt, dir1/file4.txt & dir2/file5.txt existing:

-rw-rw-r-- 1 dtvlinux dtvlinux   16 Oct 25 07:53 /tmp/file1.txt
-rw-rw-r-- 1 dtvlinux dtvlinux   16 Oct 25 07:53 /tmp/file2.txt

/tmp/dir1:
total 4.0K
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 25 07:53 file4.txt

/tmp/dir2:
total 4.0K
-rw-rw-r-- 1 dtvlinux dtvlinux 16 Oct 25 07:53 file5.txt

Adding Files to an Existing Tar Archive

To add files to an existing tar archive, use the -r (append) option followed by the archive file name and the list of files to add:

Exercise: Add file3.txt to the archive.tar archive:

Append file3.txt to archive:

tar -rvf archive.tar file3.txt

Test the archive to ensure the file is present:

tar -tvf archive.tar file3.txt

The output should resemble:

-rw-rw-r-- dtvlinux/dtvlinux 16 2023-10-25 07:53 file3.txt

Updating Files in an Existing Tar Archive

To update files in an existing tar archive, use the -u (update) option followed by the archive file name and the list of files to update:

Exercise: Update file1.txt in the archive.tar archive with the latest version:

Update the contents of file1.txt:

echo "I am an updated file1.txt" > file1.txt

Update the archive with the updated file:

tar -uvf archive.tar file1.txt

Remove file1.txt as the next exercise will demonstrate extracting a single file to prove the archive was updated:

rm -vf file1.txt

Extracting a Single File from a Tar Archive

To extract a single file from a tar archive, specify the file name after the archive file name:

Exercise: Extract only file1.txt from the archive.tar archive & check the contents to ensure it was updated in previous example:

Extract file1.txt from the archive:

tar -xvf archive.tar file1.txt

Display the contents of file1.txt:

cat file1.txt

The output should read:

I am an updated file1.txt

Compression Using tar

When creating an archive you can have tar utilize gzip, bzip2 or xz to compress and archive at the same time.

You do not need to use any additional flags when extracting from a gzip, bzip2 or xz tar archive. The tar command will automatically detect the archive type and extract accordingly. Therefore the above extraction sections are valid for these compressed archives also.

Creating gzip Compressed Archive

To create a tar archive using gzip compression, you use the -z option:

Exercise: Create a gzip compressed tar archive:

tar -cvzf archive.tar.gz file1.txt file2.txt file3.txt dir1 dir2

Compare the archive size difference between archive.tar & archive.tar.gz to validate compression being used.

ls -l archive.tar archive.tar.gz

The output should resemble the following where in my example the gzip archive is smaller at 290B:

-rw-rw-r-- 1 dtvlinux dtvlinux 10240 Oct 25 08:13 archive.tar
-rw-rw-r-- 1 dtvlinux dtvlinux   290 Oct 25 08:14 archive.tar.gz

Creating bzip2 Compressed Archive

To create a tar archive using bzip2 compression, you use the -j option:

Exercise: Create a bzip2 compressed tar archive:

tar -cvjf archive.tar.bz2 file1.txt file2.txt file3.txt dir1 dir2

Compare the archive size difference between archive.tar & archive.tar.bz2 to validate compression being used.

ls -l archive.tar archive.tar.bz2

The output should resemble the following where in my example the bzip2 archive is smaller at 283B:

-rw-rw-r-- 1 dtvlinux dtvlinux 10240 Oct 25 08:13 archive.tar
-rw-rw-r-- 1 dtvlinux dtvlinux   283 Oct 25 08:17 archive.tar.bz2

Creating xz Compressed Archive

To create a tar archive using xz compression, you use the -j option:

Exercise: Create a xz compressed tar archive:

tar -cvJf archive.tar.xz file1.txt file2.txt file3.txt dir1 dir2

Compare the archive size difference between archive.tar & archive.tar.xz to validate compression being used.

ls -l archive.tar archive.tar.xz

The output should resemble the following where in my example the xz archive is smaller at 300B:

-rw-rw-r-- 1 dtvlinux dtvlinux 10240 Oct 25 08:13 archive.tar
-rw-rw-r-- 1 dtvlinux dtvlinux   300 Oct 25 08:18 archive.tar.xz

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.