We’ll show you how to archive and extract files in Linux using the
tar command. The
tar command, short for ‘tape archive,’ is a critical skill in mastering the Linux environment. This article offers an in-depth guide on how to use tar to archive and extract files in three major Linux distributions: Ubuntu, CentOS, and Fedora.
Table of Contents
What is Tar?
Tar is a Unix/Linux utility used for creating archives – grouping multiple files into one. This tool proves its worth when dealing with data backup, network transfer, and disk space management. Its efficiency and flexibility have made tar a preferred choice for Linux users worldwide.
How to Use Tar to Archive Files
Before you embark on tar commands, you should understand their basic syntax. A tar command follows this structure:
tar [options] [archive-file] [file or directory to be archived].
For instance, to create a tar archive in Ubuntu, CentOS, or Fedora, the command is
tar -cvf archive_name.tar /path/to/directory.
- ‘c’ creates a new archive,
- ‘v’ stands for verbose mode, detailing the process,
- ‘f’ designates the following parameter as the archive file’s name.
So, if you have a directory called ‘Documents’ and you want to archive it into ‘docs_archive.tar,’ the command would be
tar -cvf docs_archive.tar Documents.
How to Extract Files from a Tar Archive
Extracting files from a tar archive is as straightforward as creating one. The syntax to extract a tar archive is
tar -xvf archive_name.tar.
In this command,
- ‘x’ stands for extracting files from an archive,
- ‘v’ and ‘f’ function just as they did while creating an archive.
Thus, to extract our previously created ‘docs_archive.tar,’ the command is
tar -xvf docs_archive.tar.
Additional Tar Options
Several other options might come in handy while managing tar archives.
- ‘z’: Dealing with a tar.gz file? Use the ‘z’ option for gzip compression or decompression, as in
tar -czvf archive_name.tar.gz directoryto create and
tar -xzvf archive_name.tar.gzto extract.
- ‘j’: Working with a tar.bz2 file? Utilize the ‘j’ option for bzip2 compression or decompression. Use
tar -cjvf archive_name.tar.bz2 directoryto create and
tar -xjvf archive_name.tar.bz2to extract.
Tar and the Top 3 Linux Distributions
Whether you’re using Ubuntu, CentOS, or Fedora, the tar command operates consistently across all distributions. While there may be minor differences in installing the tar utility, the command syntax remains unchanged.
Ubuntu, by default, comes with the tar utility installed. If, for some reason, you lack it, use the
sudo apt install tar command to get it.
In CentOS, tar also comes pre-installed. If it’s missing, the command to install it is
sudo yum install tar.
Just like Ubuntu and CentOS, Fedora pre-installs tar. If needed, you can install it using
sudo dnf install tar.
Understanding how to use tar to archive and extract files in Linux is a cornerstone of effective data management in a Linux environment. While tar’s versatility extends beyond the scope of this guide, these basics will get you started on the journey of mastering this indispensable Linux tool. If you have any questions about using tar or any other Linux utilities, don’t hesitate to reach out for additional tips.