DEV Community

Cover image for Extracting Files Using Tar in Linux
Sanskriti Harmukh for Vultr

Posted on with Aashish Chaurasiya Originally published at docs.vultr.com

Extracting Files Using Tar in Linux

Tar, short for Tape Archive, is a built-in Linux utility for creating and managing archives by combining multiple files into a single .tar file. The tar command creates, compresses, and extracts archives in several formats, giving you an organized way to manage large amounts of data. This guide covers the tar command syntax and options, then walks through creating a sample archive, listing its contents, and extracting files from compressed and uncompressed archives, including tar.gz, tar.bz2, and tar.xz. By the end, you'll be able to create, inspect, and extract tar archives on any Linux system.


The tar Command Syntax

$ tar [options] [archive-file] [file or directory to extract]
Enter fullscreen mode Exit fullscreen mode

In the command above:

  • [options]: Command line options that specify and modify the tar command's functionality
  • [archive-file]: The target archive name, such as .tar, .tar.gz, or .tar.bz2, depending on the compression method
  • [file or directory to extract]: Optional files or directories to extract from the archive; you can specify multiple

tar Command Options

  • c: Creates a new archive
  • -x: Extracts files from an archive
  • -f: Specifies the target archive
  • -v: Verbose mode; displays the tar command's progress
  • -t: Lists the archive contents
  • -z: Enables Gzip compression
  • -j: Enables Bzip2 compression
  • -J: Enables XZ compression

Create an Archive Using Tar

1. Switch to your user's home directory:

$ cd
Enter fullscreen mode Exit fullscreen mode

2. Create a sample tar_dir directory:

$ mkdir tar_dir
Enter fullscreen mode Exit fullscreen mode

3. Switch to the new tar_dir directory:

$ cd tar_dir
Enter fullscreen mode Exit fullscreen mode

4. Create sample files:

$ touch file.txt file1.txt file2.txt file3.txt nil.txt
Enter fullscreen mode Exit fullscreen mode

5. Create a new .tar archive with all the files:

$ tar -cvf archive.tar file.txt file1.txt file2.txt file3.txt nil.txt
Enter fullscreen mode Exit fullscreen mode

In the command above, -c creates a new archive, v displays progress, f specifies the archive filename, and the file list specifies what to add.

List Archive Contents Using Tar

1. List the contents of archive.tar:

$ tar -tf archive.tar
Enter fullscreen mode Exit fullscreen mode

Output:

file.txt
file1.txt
file2.txt
file3.txt
nil.txt
Enter fullscreen mode Exit fullscreen mode

The -t option lists the archive contents while f specifies the target archive.

Search Specific Files in an Archive

1. List the archive and filter the output with grep:

$ tar -tf archive.tar | grep "file"
Enter fullscreen mode Exit fullscreen mode

Output:

file.txt
file1.txt
file2.txt
file3.txt
Enter fullscreen mode Exit fullscreen mode

Extract Files Using the tar Command

1. Extract all files from archive.tar:

$ tar -xvf archive.tar
Enter fullscreen mode Exit fullscreen mode

Output:

file.txt
file1.txt
file2.txt
file3.txt
nil.txt
Enter fullscreen mode Exit fullscreen mode

2. Extract specific files, such as file1.txt and file3.txt:

$ tar -xvf archive.tar file1.txt file3.txt
Enter fullscreen mode Exit fullscreen mode

Extract Files to a Specific Directory Using Tar

tar extracts files to your working directory by default unless you specify a target with the -C option.

1. Create a new extract_dir directory:

$ mkdir extract_dir
Enter fullscreen mode Exit fullscreen mode

2. Extract archive.tar into the extract_dir directory:

$ tar -xf archive.tar -C extract_dir
Enter fullscreen mode Exit fullscreen mode

3. Navigate to the extract_dir directory:

$ cd extract_dir
Enter fullscreen mode Exit fullscreen mode

4. List the files to verify the extraction:

$ ls
Enter fullscreen mode Exit fullscreen mode

Output:

file1.txt  file2.txt  file3.txt  file.txt  nil.txt
Enter fullscreen mode Exit fullscreen mode

Extract Files From Compressed Archives Using Tar

1. Extract a Gzip-compressed .tar.gz or .tgz archive with the -z option:

$ tar -xzf archive.tar.gz
Enter fullscreen mode Exit fullscreen mode

2. Extract a Bzip2-compressed .tar.bz2 archive with the -j option:

$ tar -xjf archive.tar.bz2
Enter fullscreen mode Exit fullscreen mode

3. Extract an XZ-compressed .tar.xz archive with the -J option:

$ tar -xJf archive.tar.xz
Enter fullscreen mode Exit fullscreen mode

Next Steps

  • Create compressed archives directly with tar -czf, -cjf, or -cJf for distribution or backups
  • Combine tar with scp or rsync to transfer archives between servers efficiently
  • Schedule periodic tar backups of important directories with cron
  • Run man tar for the full list of options and advanced usage patterns

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (0)