Linux tar Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux tar command for compression and archiving tasks. The tar command is a powerful tool for creating, extracting, and managing archive files, commonly known as "tarballs" or "tar files". You will start by understanding the basic syntax and options of the tar command, then move on to creating and extracting tar archives, including compressed archives using gzip. The lab covers the essential steps to effectively utilize the tar command in your Linux workflow.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/CompressionandArchivingGroup -.-> linux/tar("`Archiving`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/CompressionandArchivingGroup -.-> linux/gzip("`Gzip`") subgraph Lab Skills linux/tar -.-> lab-422951{{"`Linux tar Command with Practical Examples`"}} linux/cd -.-> lab-422951{{"`Linux tar Command with Practical Examples`"}} linux/touch -.-> lab-422951{{"`Linux tar Command with Practical Examples`"}} linux/gzip -.-> lab-422951{{"`Linux tar Command with Practical Examples`"}} end

Understand the Basics of tar Command

In this step, we will learn the basics of the tar command in Linux. The tar command is used to create, extract, and manage archive files, which are commonly known as "tarballs" or "tar files".

First, let's understand the basic syntax of the tar command:

tar [options] [archive_name.tar] [files_or_directories]

The most common options used with the tar command are:

  • -c: Create a new archive
  • -x: Extract files from an archive
  • -v: Verbose output (show the files being processed)
  • -f: Specify the archive file name
  • -z: Compress/decompress the archive using gzip
  • -j: Compress/decompress the archive using bzip2

Now, let's create a simple tar archive:

cd ~/project
mkdir files
touch files/file1.txt files/file2.txt files/file3.txt
tar -cvf archive.tar files/

Example output:

files/file1.txt
files/file2.txt
files/file3.txt

In this example, we created a new tar archive named archive.tar that contains the files in the files/ directory.

Next, let's extract the files from the archive:

tar -xvf archive.tar

Example output:

files/file1.txt
files/file2.txt
files/file3.txt

The tar command has many more options and features, which we will explore in the next steps.

Create and Extract tar Archives

In this step, we will learn how to create and extract tar archives with more advanced options.

First, let's create a tar archive with compression using the gzip utility:

cd ~/project
mkdir compressed
touch compressed/file1.txt compressed/file2.txt compressed/file3.txt
tar -czf archive.tar.gz compressed/

Example output:

compressed/file1.txt
compressed/file2.txt
compressed/file3.txt

In this example, we used the -z option to compress the archive using gzip. The resulting file is archive.tar.gz.

Now, let's extract the contents of the compressed archive:

tar -xzf archive.tar.gz

Example output:

compressed/file1.txt
compressed/file2.txt
compressed/file3.txt

The -x option extracts the files from the archive, and the -z option decompresses the archive using gzip.

Next, let's create a tar archive that includes multiple directories:

cd ~/project
mkdir dir1 dir2
touch dir1/file1.txt dir1/file2.txt dir2/file3.txt dir2/file4.txt
tar -cvf multi_dir.tar dir1/ dir2/

Example output:

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

In this example, we included two directories (dir1 and dir2) in the tar archive.

Finally, let's extract the contents of the multi-directory archive:

tar -xvf multi_dir.tar

Example output:

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

The tar command allows you to create and extract archives with various options to suit your needs.

Compress and Decompress tar Archives

In this step, we will learn how to compress and decompress tar archives using different compression methods.

First, let's create a tar archive and compress it using the bzip2 utility:

cd ~/project
mkdir uncompressed
touch uncompressed/file1.txt uncompressed/file2.txt uncompressed/file3.txt
tar -cjf archive.tar.bz2 uncompressed/

Example output:

uncompressed/file1.txt
uncompressed/file2.txt
uncompressed/file3.txt

In this example, we used the -j option to compress the archive using bzip2. The resulting file is archive.tar.bz2.

Now, let's extract the contents of the bzip2 compressed archive:

tar -xjf archive.tar.bz2

Example output:

uncompressed/file1.txt
uncompressed/file2.txt
uncompressed/file3.txt

The -x option extracts the files from the archive, and the -j option decompresses the archive using bzip2.

Next, let's create a tar archive and compress it using the xz utility:

cd ~/project
mkdir compressed_xz
touch compressed_xz/file1.txt compressed_xz/file2.txt compressed_xz/file3.txt
tar -cJf archive.tar.xz compressed_xz/

Example output:

compressed_xz/file1.txt
compressed_xz/file2.txt
compressed_xz/file3.txt

In this example, we used the -J option to compress the archive using xz. The resulting file is archive.tar.xz.

Finally, let's extract the contents of the xz compressed archive:

tar -xJf archive.tar.xz

Example output:

compressed_xz/file1.txt
compressed_xz/file2.txt
compressed_xz/file3.txt

The -x option extracts the files from the archive, and the -J option decompresses the archive using xz.

The tar command supports various compression methods, allowing you to choose the one that best suits your needs.

Summary

In this lab, we learned the basics of the tar command in Linux, including how to create, extract, and manage archive files. We started by understanding the basic syntax and common options of the tar command, such as -c for creating a new archive, -x for extracting files, and -v for verbose output. We then created a simple tar archive and extracted its contents.

Next, we explored more advanced features of the tar command, including how to create and extract compressed archives using the gzip utility. We learned how to use the -z option to compress and decompress the archive, resulting in a .tar.gz file.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like