Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Linux untar Command Syntax
3.
Examples of Usages
3.1.
Example 1: Extracting a Tar File
3.2.
Example 2: Extracting to a Specific Directory
3.3.
Example 3: Extracting Specific Files
4.
Advanced Use Cases of untar Command in Linux
4.1.
Using Wildcards to Extract Specific Patterns
4.2.
Extracting Files Without the Original Directory Structure
4.3.
Keeping Newer Files When Extracting
5.
Frequently Asked Questions
5.1.
Can I untar a file without using the command line?
5.2.
How do I handle a tar.gz file?
5.3.
What if I get an error saying the file cannot be found?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Untar Command in Linux

Author Gaurav Gandhi
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Unpacking files on a Linux system is like opening a treasure chest; you never know what useful tools and information you'll find inside. One of the handiest tools for this task is the untar command, a fundamental utility for managing tarball files (.tar) or compressed archives. This article will guide you through the basics of using the untar command, from simple syntax to more advanced applications.

Untar Command in Linux

By the end of our journey, you'll be equipped with the knowledge to extract content from tar files, understand the command's various options, and tackle some of its more complex uses.

Linux untar Command Syntax

When you're ready to see what's inside a tar file on your Linux system, the untar command is your go-to tool. Think of it as the key that unlocks the contents of a packed box. In its most basic form, the command to extract or 'untar' files looks pretty straightforward. Here's the blueprint:

tar -xvf file_name.tar


Let's break this down:

  • tar: This is the command we use to work with tar files, whether we're packing or unpacking them.
     
  • -x: This stands for 'extract.' It tells the tar command we want to open up a tar file and see its contents.
     
  • -v: This option stands for 'verbose.' It's like asking tar to tell us a story about what it's doing. With -v, tar lists out the files it's extracting, so we can see what's happening.
     
  • -f: This one stands for 'file.' It's our way of telling tar, "Hey, the next thing I type is the name of the tar file I want to work with."
     

So, when we put it all together and run tar -xvf file_name.tar, we're telling our Linux system, "Please open up this tar file and show me everything you're pulling out of it."

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Examples of Usages

Using the untar command might seem tricky at first, but with a few examples, you'll be handling tar files like a pro in no time. Let's walk through some common scenarios you might encounter.

Example 1: Extracting a Tar File

Imagine you've got a file named documents.tar. To open this up and see what's inside, you'd use:

tar -xvf documents.tar


This command tells your computer to extract the files from documents.tar and list them out as they're being extracted, so you can see what's happening.

Example 2: Extracting to a Specific Directory

What if you want to extract the files into a specific folder instead of where you are? No problem! Let's say you want to put them in a folder named myfolder. Here's how:

tar -xvf documents.tar -C myfolder


The -C option changes the directory. It's like telling your files, "Don't stay here; move into myfolder."

Example 3: Extracting Specific Files

Sometimes, you don't need everything in the tar file, just a few specific files. Suppose documents.tar contains file1.txt, file2.txt, and file3.txt, but you only want file1.txt. You'd do this:

tar -xvf documents.tar file1.txt


This command says, "Only grab file1.txt from the tar file and ignore the rest."

With these examples, you're well on your way to mastering the art of extracting files using the untar command. Remember, practice makes perfect, so don't hesitate to try these out yourself!

Advanced Use Cases of untar Command in Linux

As you become more comfortable with the untar command, you might find yourself needing to do more than just extract files. Let's look at some advanced tricks that can make your life easier.

Using Wildcards to Extract Specific Patterns

When you have a large tar file and only need files that match a certain pattern, wildcards are your friends. For example, if you want to extract all .txt files from data.tar, you could use:

tar -xvf data.tar --wildcards '*.txt'


This command tells your computer, "Look through data.tar and only pull out the files that end in .txt."

Extracting Files Without the Original Directory Structure

Sometimes, the files in a tar file are buried within multiple layers of folders, but you want to extract them all into one place. You can do this by combining the --strip-components option with the number of directory levels you want to skip. For instance:

tar -xvf project.tar --strip-components=1


This command says, "Extract the files from project.tar but skip the first level of directories and put everything right here."

Keeping Newer Files When Extracting

If you're updating files and don't want to overwrite newer versions with older ones from the tar file, the --keep-newer-files option is super handy. Here's how to use it:

tar -xvf backup.tar --keep-newer-files


With this command, your computer checks the dates and says, "If there's a newer version already here, leave it be and only extract the older stuff from backup.tar."

These advanced techniques can help you tailor the untar command to your specific needs, making file management much more efficient.

Frequently Asked Questions

Can I untar a file without using the command line?

Yes, you can use graphical tools like File Roller in GNOME or Ark in KDE. Just right-click the tar file & choose 'Extract Here'.

How do I handle a tar.gz file?

Use the command tar -zxvf file_name.tar.gz. The z option tells tar the file is compressed with gzip.

What if I get an error saying the file cannot be found?

Check your spelling & make sure you're in the right directory where the tar file is located. Use ls to see files in your current directory.

Conclusion

Unlocking the contents of a tar file with the untar command in Linux is like opening a door to a room full of resources. Whether you're a beginner just starting to explore the Linux environment or someone with a bit more experience looking to refine your skills, understanding how to effectively use this command can significantly ease the way you manage files and directories. From extracting basic tar files to employing advanced options for more complex tasks, the untar command offers a versatile toolkit for your file handling needs.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
SCP command in Linux
Next article
How to Install Chrome on Linux
Live masterclass