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.
$ tar [options] [archive-name] [files or directories]
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.
Option | Description |
-c | It is added when creating an archive. |
-x | It extracts files from the archive. |
-f | It lets you add the name of the archive you want to create. |
-t | It displays the contents of an archive. |
-r | It appends a file or directory to an existing archive. |
-v | It enables verbose mode. |
-j | It creates the archive using bzip2. |
–exclude | It 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.
$ tar [options] [archive-name] [files or directories]
Using some of the files listed above, we can create a linuxgeekery.tar archive with the below command.
$ tar -cvf linuxgeekery.tar hosts.py names.txt users/
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.
To verify that the archive has been created, we’ve run the ls command below.
$ ls *.tar
The archive takes the .tar extension.
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 -tvf linuxgeekery.tar
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.
$ tar -xvf linuxgeekery.tar
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:
$ mkdir extracted/
$ tar -xvf linuxgeekery.tar -C extracted/
The ls command verifies that the created directory contains the extracted files and directories.
$ ls extracted/
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 -cvzf linuxgeekery.tar.gz hosts.py names.txt users/
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.
$ tar -tvf linuxgeekery.tar.gz
Next, let’s extract it.
$ tar -xvf linuxgeekery.tar.gz
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.
$ tar -xvf linuxgeekery.tar hosts.py
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 -rvf linuxgeekery.tar users
After appending, we can check the archive’s contents to verify that the file and directory were added successfully.
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.
We can run the below command to exclude all text files.
$ tar -cvf desktop.tar --exclude=’ *.txt’ Desktop/
Lastly, verify that the text files have been excluded by displaying the archive’s contents.
$ tar -tf desktop.tar
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.
$ tar -tf desktop.tar | grep hosts.py
Once you’ve verified the file or directory exists, delete it using the –delete option.
$ tar --delete -f desktop.tar Desktop/hosts.py
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.