Extracting a TAR GZ file in Linux typically involves using the `tar -xzf` command structure. If you’ve ever wondered how to run tar gz file in linux, you’re in the right place. This guide will walk you through everything you need to know, from basic extraction to advanced options, all in a simple, step-by-step manner.
Tar GZ files are everywhere in Linux. They combine multiple files into one archive and compress them. You’ll see them often when downloading software, source code, or backups. Learning to handle them is a core skill for any Linux user.
We’ll cover the basics first. Then we’ll move to more complex scenarios. By the end, you’ll feel confident working with these files. Let’s get started.
What Is A Tar Gz File
A TAR GZ file is actually two things combined. The TAR part stands for Tape Archive. It bundles files together without compression. The GZ part uses gzip compression to shrink the archive size. So a file like “example.tar.gz” is a compressed archive.
Think of it like a zip folder on Windows. But Linux uses TAR and GZ separately. This gives more control over compression methods. You might also see .tgz, which is the same thing. Both are common in the Linux world.
Why Linux Uses Tar Gz Files
Linux distributions often distribute software as source code in TAR GZ files. This lets users compile programs themselves. It also preserves file permissions and directory structures. System administrators use them for backups and transferring data.
The format is lightweight and widely supported. Almost every Linux system has the tar command pre-installed. This makes it a reliable choice for archiving.
How To Run Tar Gz File In Linux
Now let’s dive into the main topic. The exact phrase “How To Run Tar Gz File In Linux” refers to extracting and using the contents. You don’t actually “run” the archive itself. Instead, you extract it, then run the files inside.
The most common command is:
tar -xzf filename.tar.gz
Let’s break down what each part does:
- tar: The archive utility
- -x: Extract files from the archive
- -z: Decompress using gzip
- -f: Specify the archive filename
That’s the basic command. But there are many variations. We’ll explore them in detail.
Step 1: Open The Terminal
First, open your terminal. You can usually find it in your applications menu. Or use a keyboard shortcut like Ctrl+Alt+T on Ubuntu. The terminal is where you’ll type all commands.
Make sure you know where your TAR GZ file is located. Use the `cd` command to navigate to that directory. For example:
cd ~/Downloads
This moves you to the Downloads folder. Replace with your actual file path.
Step 2: Extract The Archive
Now run the extraction command. Let’s say your file is called “software.tar.gz”. Type:
tar -xzf software.tar.gz
Press Enter. The command runs silently if successful. You’ll see no output. That’s normal. The files are now extracted into the current directory.
If you want to see progress, add the `-v` flag for verbose output:
tar -xzvf software.tar.gz
This lists each file as it’s extracted. Helpful for large archives.
Step 3: Verify The Extraction
After extraction, check the contents. Use `ls` to list files:
ls -l
You should see a new folder or files. The archive itself remains unchanged. You can delete it later if needed.
Step 4: Run The Extracted Files
Now you can run the extracted files. If it’s a program, look for a binary or script. Common names are “install.sh”, “configure”, or “makefile”. For example:
./install.sh
Or if it’s a compiled program:
./program_name
Sometimes you need to compile source code first. We’ll cover that later.
Common Tar Gz Extraction Options
The `tar` command has many options. Here are the most useful ones:
- -x: Extract files
- -z: Filter through gzip
- -v: Verbose, show file names
- -f: Specify archive file
- -C: Extract to a specific directory
- –exclude: Skip certain files
- -t: List contents without extracting
You can combine them. For example, to extract to a specific folder:
tar -xzf software.tar.gz -C /target/directory
This extracts everything into /target/directory. The directory must exist first.
Listing Contents Without Extracting
Sometimes you want to see what’s inside before extracting. Use the `-t` option:
tar -tzf software.tar.gz
This prints all files and folders in the archive. No extraction happens. Useful for checking structure.
Extracting Specific Files
You can extract only certain files. Provide the file names after the archive:
tar -xzf software.tar.gz file1.txt file2.txt
This extracts only file1.txt and file2.txt. The rest stay in the archive.
How To Handle Different Tar Gz Scenarios
Real-world usage varies. Here are common situations you might face.
Extracting To A Different Directory
Use the `-C` option. For example:
tar -xzf archive.tar.gz -C /home/user/newfolder
Make sure the target directory exists. If not, create it first with `mkdir`.
Extracting With Progress Bar
For large files, you might want a progress indicator. Use `pv` (pipe viewer) if installed:
pv archive.tar.gz | tar -xz
This shows a progress bar. Install pv with `sudo apt install pv` on Debian/Ubuntu.
Handling Corrupted Archives
Sometimes archives get corrupted. Try using `gunzip` first:
gunzip -c archive.tar.gz | tar -x
Or use `bzip2` for .tar.bz2 files. If still failing, check file integrity with `md5sum`.
How To Create Tar Gz Files
Creating archives is just as important. Use the `-c` flag for create:
tar -czf archive.tar.gz /path/to/folder
This compresses the folder into archive.tar.gz. The `-c` creates, `-z` compresses, `-f` specifies output.
You can also add multiple files:
tar -czf archive.tar.gz file1.txt file2.txt folder1
Excluding Files When Creating
Use `–exclude` to skip certain files:
tar -czf archive.tar.gz --exclude="*.log" /path/to/folder
This excludes all .log files. You can use multiple exclude patterns.
Running Programs From Tar Gz Archives
Many Linux programs come as source code in TAR GZ files. Here’s how to compile and run them.
Step 1: Extract The Source Code
First, extract the archive:
tar -xzf program.tar.gz
cd program-folder
Step 2: Read The Instructions
Look for a README or INSTALL file:
cat README
This tells you how to compile. Most follow the standard pattern.
Step 3: Configure And Compile
Typical steps:
./configure
make
sudo make install
The `./configure` checks dependencies. `make` compiles the code. `sudo make install` places the program in system directories.
Sometimes you need development tools. Install build-essential on Debian/Ubuntu:
sudo apt install build-essential
Step 4: Run The Program
After installation, you can run the program from anywhere:
program-name
If not installed system-wide, run from the build folder:
./program-name
Common Errors And Solutions
Even experienced users hit snags. Here are fixes for common issues.
Error: “Cannot Open: No Such File Or Directory”
This means the file doesn’t exist or path is wrong. Double-check the filename and location. Use `ls` to confirm.
Error: “Gzip: Stdin: Not In Gzip Format”
The file might not be gzip compressed. Try without the `-z` flag:
tar -xf archive.tar
Or it could be a different format like bzip2. Use `-j` for bzip2 files.
Error: “Permission Denied”
You don’t have write permissions in the current directory. Use `sudo` or change to a writable location:
sudo tar -xzf archive.tar.gz -C /tmp
Error: “Tar: Exiting With Failure Status Due To Previous Errors”
This usually means disk space is full or file system is read-only. Check with `df -h` and `mount`.
Advanced Tar Gz Techniques
For power users, here are more advanced methods.
Using Tar With Pipes
You can pipe tar output to other commands. For example, extract and list files:
tar -xzf archive.tar.gz && ls -l
Or extract to a remote server via SSH:
tar -czf - /local/folder | ssh user@remote "tar -xzf - -C /remote/folder"
This copies files over the network without creating a local archive.
Incremental Backups
Tar supports incremental backups with `–listed-incremental`. Create a snapshot file:
tar -czf backup.tar.gz --listed-incremental=backup.snap /data
Next time, only new files are added. Restore with:
tar -xzf backup.tar.gz --incremental
Compression Levels
Gzip uses compression levels 1-9. Default is 6. Use a higher level for smaller files:
tar -czf - --gzip --level=9 archive.tar.gz /data
But higher levels take more time. Balance based on your needs.
Alternatives To Tar Gz
While TAR GZ is common, other formats exist. Here’s a quick comparison.
- .tar.bz2: Better compression but slower. Use `-j` flag.
- .tar.xz: Even better compression. Use `-J` flag.
- .zip: Cross-platform but less efficient. Use `unzip` command.
- .rar: Rare on Linux. Needs `unrar`.
Stick with TAR GZ for most Linux tasks. It’s the standard.
Frequently Asked Questions
What is the difference between tar.gz and tgz?
They are identical. .tgz is just a shorter extension. Both use the same format.
Can I open tar.gz files without terminal?
Yes, most file managers support it. Right-click and select “Extract Here”. But terminal gives more control.
How do I run a tar.gz file after extraction?
Look for executable files inside. Use `./filename` to run. For source code, follow compile steps.
Why is my tar.gz extraction failing?
Check disk space, permissions, and file integrity. Use `file` command to verify format.
Can I password protect a tar.gz file?
Tar doesn’t support encryption. Use gpg instead: `tar -czf – folder | gpg -c > archive.tar.gz.gpg`.
Conclusion
Now you know how to run tar gz file in linux. The process is straightforward once you understand the commands. Start with `tar -xzf`, then explore options as needed.
Practice with sample files. Create your own archives. Soon you’ll handle TAR GZ files with ease. Linux gives you power, and tar is a key tool in your kit.
Remember the core flags: -x for extract, -z for gzip, -f for file. Combine them wisely. And always verify your work with `ls`.
If you run into trouble, check the error messages. Most have simple fixes. The Linux community is also helpful. Search online or ask in forums.
Happy archiving. You’ve got the skills now.