The Linux tar Command – How to Use It Like a Pro

tar-command

The (Tar Archive) tar command is a handy tool used by system administrators to create and manage archive files. It takes files and directories, compresses them to tar archive files, and saves them in the current working directory or a specified path.

Although Linux/Unix has different utilities for creating archive files, the tar command is the most widely used, and as you will see in this post, it has numerous functionalities. This article discusses the tar command and gives examples to demonstrate various ways of using the utility.

tar Command Syntax and Options

The tar utility follows the following syntax.

In the above syntax, the archive-name represents the name you want to use for the archive file to be created when you run the command. You can then add file(s) or directory(s) that you want to archive.

The options are the flags you can include to define the behavior of the command. The table below shows some of these options.

OptionDescription
-cIt is added when creating an archive.
-xIt extracts files from the archive.
-fIt lets you add the name of the archive you want to create.
-tIt displays the contents of an archive.
-rIt appends a file or directory to an existing archive.
-vIt enables verbose mode.
-jIt creates the archive using bzip2.
–excludeIt allows you to exclude files or directories when creating the archive.

Practical Examples of Using the tar Command

Anyone can quickly master using the tar utility to compress files. The best part about using the tar command is how it gives room to utilize different options depending on your goal. The examples covered below will guide you on how to create and manage the archive file. Let’s begin!

1. tar Command Create a tar Archive

Earlier, we saw the syntax for creating tar archives. Before we run the command, let’s display the directory contents using the dir command.

linux-dir-command

Using some of the files listed above, we can create a linuxgeekery.tar archive with the below command.

We’ve compressed two files and a directory. The -v option enables the verbose mode and prints the files and directories compressed. The -c creates the archive, while the -f lets us add the name for the archive file.

tar-command-create-archive

To verify that the archive has been created, we’ve run the ls command below.

The archive takes the .tar extension.

linux-ls-command

2. Listing the Contents of a tar Archive

For an existing archive file, you can use the -t option to view its contents using the following command.

tar-command-view-archive-contents

3. How to Extract a tar Archive

The -x option lets you extract the specified archive file. The extracted files and directories will be saved to your current working directory.

Here’s an example.

extracting-a-tar-archive

You can also extract the archive and put it in a separate directory. For instance, let’s quickly use the mkdir command to create a directory.

Next, we can extract our archive to that directory. However, we must add the -C option before specifying where to save the extracted files and directories.

Our command will be:

extract-tar-archive

The ls command verifies that the created directory contains the extracted files and directories.

linux-ls-command

4. tar Command Create a Compressed Archive

When creating the archive file, you can add the -z option to compress it. The compressed tar archive will have the .tar.gz extension. However, you must still include the -cf option.

Here’s an example.

tar-command-create-compressed-archive

5. Extracting a Compressed tar Archive

Extracting a compressed tar archive follows the same procedure as extracting any other archive file. First, let’s view its contents.

extract-compressed-archive

Next, let’s extract it.

linux-tar-command

6. Extracting a Single Item from an Archive

The tar command allows you to specify a single item that you want to extract. This option is handy when your archive file contains multiple files and directories, but you don’t want to extract them all.

For instance, to extract ‘hosts.py’ from our archive, we will use the below command.

extracting-files-from-archive-files

You can also specify more files that you want to extract instead of extracting a single file or directory.

7. tar Command Append an Archive

When you have an existing archive file, you can add more files or directories using the -r option. Let’s give an example.

tar-command-append-archive

After appending, we can check the archive’s contents to verify that the file and directory were added successfully.

check-archive-contents

8. Excluding Files

You don’t have to include all files in a directory when archiving it. The –exclude option lets you specify what to exclude.

Suppose we want to archive the directory below.

linux-ls-command

We can run the below command to exclude all text files.

tar-command-exclude-files

Lastly, verify that the text files have been excluded by displaying the archive’s contents.

tar-list-archive-contents

9. Deleting Files with the tar Command

If your archive contains an unwanted file or directory, there is an option to delete it. First, let’s display the archive’s contents and include the grep command to find the target file or directory.

We are targeting ‘hosts.py’ in this example.

Once you’ve verified the file or directory exists, delete it using the –delete option.

tar-command-delete-files

Read Also: How to compress files using the gzip command.

Conclusion

Creating archive files is easy when you master the tar command. Luckily, this post discusses the command in detail, giving practical examples of using it. With this insight, you are ready to start creating archive files in Linux/Unix.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top