Extracting Files Using Tar in Linux
Learn how to create, list, and extract tar, tar.gz, tar.bz2, and tar.xz archives in Linux using the tar command.

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, following Linux archive management practices documented in Vultr Docs.
The tar Command Syntax
tar [options] [archive-file] [file or directory to extract]
In the command above:
[options]: Command line options that specify and modify thetarcommand'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 thetarcommand'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
2. Create a sample tar_dir directory:
mkdir tar_dir
3. Switch to the new tar_dir directory:
cd tar_dir
4. Create sample files:
touch file.txt file1.txt file2.txt file3.txt nil.txt
5. Create a new .tar archive with all the files:
tar -cvf archive.tar file.txt file1.txt file2.txt file3.txt nil.txt
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
Output:
file.txt
file1.txt
file2.txt
file3.txt
nil.txt
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"
Output:
file.txt
file1.txt
file2.txt
file3.txt
Extract Files Using the tar Command
1. Extract all files from archive.tar:
tar -xvf archive.tar
Output:
file.txt
file1.txt
file2.txt
file3.txt
nil.txt
2. Extract specific files, such as file1.txt and file3.txt:
tar -xvf archive.tar file1.txt file3.txt
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
2. Extract archive.tar into the extract_dir directory:
tar -xf archive.tar -C extract_dir
3. Navigate to the extract_dir directory:
cd extract_dir
4. List the files to verify the extraction:
ls
Output:
file1.txt file2.txt file3.txt file.txt nil.txt
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
2. Extract a Bzip2-compressed .tar.bz2 archive with the -j option:
tar -xjf archive.tar.bz2
3. Extract an XZ-compressed .tar.xz archive with the -J option:
tar -xJf archive.tar.xz
Next Steps
- Create compressed archives directly with
tar -czf,-cjf, or-cJffor distribution or backups - Combine
tarwithscporrsyncto transfer archives between servers efficiently - Schedule periodic
tarbackups of important directories with cron - Run
man tarfor the full list of options and advanced usage patterns
For the full guide with additional tips, visit the original article on Vultr Docs.
About the authors
- A Developer Advocate with a focus on improving the developer experience through clear communication, technical enablement, and community engagement.
- DevOps Engineer with experience in Kubernetes, automation, cloud infrastructure, and observability. I work in Developer Relations, contribute to technical documentation, and collaborate on engineering-focused projects.
Comments
Join the discussionNo comments yet. Be the first to comment.





