Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
RHCSA - Understand and Use Essential Tools: Archive & Unpack Using tar & star
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
or bzip2
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:
Create the required directories & files for testing the tar
command with:
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
ls -lh
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.
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
Viewing the Contents of a Tar Archive
To view the contents of a tar archive, use the -t
(list) option:
Display a list of files and directories included in the archive.tar
archive:
tar -tvf archive.tar
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:
rm -rvf file1.txt file2.txt dir1 dir2
tar -xvf archive.tar
ls -lh
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:
Extract all files and directories from the archive.tar
archive. They will be extracted to the /tmp
directory:
tar -C /tmp -xvf archive.tar
ls -lhd /tmp/{dir{1,2},file{1,2}.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:
Add file3.txt
to the archive.tar
archive:
tar -rvf archive.tar 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:
Update file1.txt
in the archive.tar
archive with the latest version:
echo "I am an updated file1.txt" > file1.txt
tar -uvf archive.tar file1.txt
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:
Extract only file1.txt
from the archive.tar
archive & check the contents to ensure it was updated in previous example:
tar -xvf archive.tar file1.txt
cat file1.txt
Compression Using tar
When creating an archive you can have tar
utilize gzip
or bzip2
to compress and archive at the same time.
Note
You do not need to use any additional flags when extracting from a gzip
or bzip2
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:
Create a gzip
compressed tar
archive:
tar -cvzf archive.tar.gz file1.txt file2.txt file3.txt dir1 dir2
If following along with all the examples in this guide, compare the archive size difference between archive.tar
& archive.tar.gz
to validate compression being used.
Creating bzip2 Compressed Archive
To create a tar archive using bzip2
compression, you use the -j
option:
Create a bzip2
compressed tar
archive:
tar -cvjf archive.tar.bz2 file1.txt file2.txt file3.txt dir1 dir2
If following along with all the examples in this guide, compare the archive size difference between archive.tar
& archive.tar.bz2
to validate compression being used.
star Command
The star
command, although mentioned on the exam objectives is no longer shipped with Red Hat 9 so may be an oversight.
Using star
would of been better for compatibility with extended attributes and SELinux, however the tar
command has the following options available that has that functionality now anyway:
--acls
: Enable POSIX ACLs support.--selinux
: Enable SELinux context support.--xattrs
: Enable extended attributes support.
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.