untar Command in Linux

Syntax:

 tar -xvf archive.tar
  • -x: It extract files from the archive
  • -v: It is used for verbose mode which shows the extraction process
  • -f: It specifies the archive file

How to Use the Untar Command

1. Extract a .tar File

This takes out all the files and puts them into the current folder.

tar -xvf filename.tar

2. Extract a .tar.gz (Gzipped Tarball) File

The -z flag is added to process files that are compressed with gzip.

 tar -xzvf filename.tar.gz

3. Extract a .tar.bz2 (Bzip2 Compressed) File

The -j flag is added to process files compressed with bzip2.

 tar -xjvf filename.tar.bz2

4. Extract to a Specific Directory

Using the command below, the extraction can be redirected to a different folder using option -C.

 tar -xvf filename.tar -C /path/to/directory

5. Extract Specific Files from an Archive

Only file1.txt and file2.txt will be extracted from the archive.

 tar -xvf filename.tar file1.txt file2.txt

6. List Contents of a Tar File Without Extracting

This retrieves the contents of the archive without extracting it.

 tar -tvf filename.tar

Advanced Tar Options

Linux has the tar command that provides multiple advanced options for improving file archiving and extraction processes. With these options, users can extract individual files, manage compressed archives, and guide extractions into designated directories with ease. Learning these options is important to manage.tar,.tar.gz, and.tar.bz2 files properly.

OptionDescription
-xExtract files from the archive
-vDisplay extraction details
-fSpecify archive filename
-zHandle gzip-compressed archives
-jHandle bzip2-compressed archives
-CExtract to a specified directory
-tList files inside an archive
--wildcardsExtract files using patterns

Troubleshooting Common Issues

While extracting files with the tar command on Linux using the command line could give the user a tough time, here are some common errors and ways to fix them.

1. «Permission Denied» Error

If you do not have the requisite permission to extract a file, then to execute the command try using the sudo option alongside the command. use the below command:

sudo tar -xvf filename.tar

2. «No such file or directory» Error

The problem arises as soon as the designated archive file could not be tracked down in the current working directory. Use the below command:

ls -l filename.tar

3. «Archive File Corrupted» Error

Before trying to extract files from an archive, always check to see if the archive is damaged. When damaged, try to do the following:

tar -tvf filename.tar

Источник: https://www.geeksforgeeks.org/linux-unix/untar-command-in-linux/