tar command in Linux

How to Compress & Extract File in Linux?

Are you a Linux user looking for a powerful tool to compress and extract files? Look no further! The tar command in Linux is your go-to solution. In this article, we will explore the ins and outs of the tar command, providing you with a comprehensive guide on how to compress and extract files using this versatile utility.

To advance the ideal of tasks, you need to know some essential commands to manage files. Tar is one of them and in the end, you will be able to extract, create, modify, and list tar archive files. As a compressed file, an archived file contains one or more files that are grouped together for easier storage and transfer.

Compress & Extract File with tar Command in Linux

Previously on the OperaVPS blog, you learned How To Unzip Files Using Command On Linux. Join us with this guide to learn how to create archived files with the help of tar command examples.

The basic syntax of tar command

To tar a folder in Linux, you will use a command kike below:

$ tar [options][archive-file] [file or dir to be archived]

To get familiar with this syntax and go through the examples of this tutorial, look at the parameters below to see what options tar command provides:

-c: Creates an archive tar file.

-x:  This extracts archive file.

-f: Specifies the filename of the archive file.

-v: Prints verbose information for any tar operation on the terminal.

-t:  This option lists all the files inside an archive file.

-u: Archives a file and then adds it to an existing archive file.

-r:  This option updates a file or directory located inside a .tar file

-z: This creates a tar file using gzip compression

-j: Create an archive file using the bzip2 compression

-W: Verifies an archive file.

-A:  This option concatenates the archive files.

-wildcards: This specifies patterns in the UNIX tar command.

-exclude: It excludes files and directories when creating the archive.

-delete: This option removes files and directories from the archive.

Linux tar Command Examples

You can build archive files with the tar utility using a variety of compression methods, including xz, gzip, and bzip2. It is standard practice to append the compression suffix to the archive file. For instance, the file should have the.tar.gz suffix when using gzip compression (with the -z option).

Create Archive File

To create a tar archive file, you can run a command like the following example.

# tar -cvf operavps.tar /home/operavps/

If you are interested in checking what each option used in the tar command says, have a look at the below:

c – creates a fresh.tar archive file.

v – Show the.tar file’s progress in great detail.

f – The archive file’s file name type.

Create tar.gz File

As you know, the “tar” command supports the gzip compression format having “tar.gz”, “tgz”, and “tar.gzip” extensions. For example, to create a compressed tar.gz archive file in Linux, run:

tar cvzf Operavideos.tar.gz /home/Operavideos

Since the tar.gz and tgz are the same, you can also type:

tar cvzf Operavideos.tgz /home/Operavideos

Uncompress tar.gz File

Tar command in Linux is also used to uncompress tar.gz archive files. So, you just need to use the command below to uncompress the tar.gz archive file in Linux:

# tar -xvf operavps.tar.gz

To untar in different directories, use option -C and the directory path.

List Content of tar File

You can run the command below to list the content of the tar archive file in Linux.

# tar -tvf operavps.tar

Create tar.bz2 File in Linux

Tar command in Linux helps you create a highly compressed tar archived file. Bz2 is a feature that compresses data and produces an archive file that is smaller than gzip. Compared to gzip, which takes less time, bz2 compression takes longer to compress and decompress files.

For example, when you want to create a Phpfiles-org.tar.bz2 file in Linux, type:

# tar cvfj Phpfiles-org.tar.bz2 /home/php

OR

# tar cvfj Phpfiles-org.tar.tbz /home/php

OR

# tar cvfj Phpfiles-org.tar.tb2 /home/php

Untar tar.bz2 File

You can easily use the following command when you need to uncompress the highly compressed tar.bz2 file in Linux.

# tar -xvf operavideos.tar.bz2

Untar Single file from tar File

A tar command in Linux can help you to extract a single file. Look at the below example where the cleanfiles.sh will be extracted from cleanfiles.sh.tar.

# tar -xvf cleanfiles.sh.tar cleanfiles.sh

OR

# tar --extract --file=cleanfiles.sh.tar cleanfiles.sh

Exclude Directories and Files

While creating a tar.gz file in Linux, you can exclude files and directories. To do this, run:

# tar --exclude='file1.txt' -zcvf backup.tar.gz /home/operavps
# tar --exclude='/home/operavps/uploads' -zcvf backup.tar.gz /home/operavps

Verify the tar, tar.gz, and tar.bz2 Sizes

Tar command in Linux enables you to check the size of the tar, tar.gz, and tar.bz2 Archive File. Run the command below to view the archive file’s size in Kilobytes (KB).

# tar -czf - operavps.tar | wc -c
# tar -czf – Operavideos.tar.gz | wc -c
# tar -czf - Phpfiles-org.tar.bz2 | wc -c

Untar tar File

Use the following command to untar/extract a tar archive file in Linux.

## Untar files in Current Directory ##
# tar -xvf public_html.tar

To untar in a different directory, use option -C.

## Untar files in specified Directory ##
# tar -xvf public_html.tar -C /home/public_html/videos/

Extract Group of Files in Linux

Using Wildcard, you can extract a group of files. Check the following example to see how you can use this command to extract a group of all .php files from a tar, tar.gz, and tar.bz2 archive file.

# tar -xvf Phpfiles-org.tar --wildcards '*.php'
# tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php'
# tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'

Delete File/Directory from tar

Tar command in Linux helps you to remove files and directories from tar archive. To do this, just run the command below and remove them from the already created tar file.

# tar --delete -f backup.tar.gz file1.txt
# tar --delete -f backup.tar.gz '/home/operavps/uploads'

Untar Multiple files from tar, tar.gz, and tar.bz2 File

Using the examples below enables you to untar/extract your considered files from the tar, tar.gz, and tar.bz2 archive files.

# tar -xvf operavps.tar "operafileA" "fileB" 
# tar -zxvf Operavideos.tar.gz "operafileA" "fileB" 
# tar -jxvf Phpfiles-org.tar.bz2 "operafileA" "operafileB"

Extraction of a Specific File Extension in tar

Using the tar command in Linux helps you to extract specific File extensions in the tar archive. To do this, run:

# tar -xvf backup.tar.gz --wildcards '*.png'

The above command extracts files with the specific extension .png from the tar archive file.

Extract Specific Files from a tar.bz2 File

You can extract a specific file(s) from a tar.bz2 file. To do this, append a space-separated list of file names to be extracted after the archive name:

tar -xf archive.tar.bz2 file1 file2

You must specify the exact names of the files, together with the path, as printed by the --list (-t) option while extracting them. Multiple files can be extracted from an archive much like multiple directories may be extracted from an archive:

tar -xf archive.tar.bz2 dir1 dir2

An error message similar to the one shown below will appear if you attempt to extract a file that doesn’t exist in the archive:

tar -xf archive.tar.bz2 README
output
tar: README: Not found in archive
tar: Exiting with failure status due to previous errors

You can use a wildcard pattern to extract files from a tar.bz2 file with the –wildcards option. To stop the shell from reading the pattern, it must be quoted.

For instance, you could use the following command to extract only files with the extension.md (Markdown files):

tar -xf archive.tar.bz2 --wildcards '*.md'

List Content tar.gz File

To list the content of the tar.gz file in Linux, type:

# tar -tvf staging.operavps.com.tar.gz

Add Files/Directories to tar File

To add files or directories to the existing tar archive files in Linux, use the example below in which the file xyz.txt and directory php.

# tar -rvf operavps.tar xyz.txt
# tar -rvf operavps.tar php

Untar Single file from tar.gz File

To extract a single file from the tar.gz archive file in Linux, use the following command as follows.

# tar -zxvf operavpsbackup.tar.gz operavpsbackup.xml

OR

# tar --extract --file=operavpsbackup.tar.gz operavpstbackup.xml

List Content tar.bz2 File

To list the content of the tar.bz2 archive file in Linux, type:

# tar -tvf Phpfiles-org.tar.bz2

Add Files/Directories to tar.gz and tar.bz2 Files

Since the tar command has no option to add files or directories to an existing compressed tar.gz and tar.bz2 archive file, the below error will be shown when you try to do this.

# tar -rvf operavideos.tar.gz xyz.txt

# tar -rvf Phpfiles-org.tar.bz2 xyz.txt

Add Files or Directories to tar.gz and tar.bz2 Files

Untar Single file from tar.bz2 File

Ta command in Linux is also used to untar a single file from tar.bz2 File. To do this, run:

# tar -jxvf Phpfiles-org.tar.bz2 home/php/index.php

OR

# tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php

The above commands extract index.php from the file Phpfiles-org.tar.bz2.

View a tar File

You can use the following command to let all the tar files and folders in Linux be splayed in the terminal:

$ tar -tf operavps.tar.gz

Verify tar, tar.gz, and tar.bz2 File

To verify any tar or compressed archived file in Linux, run the command below and consider that you cannot do verification on a compressed archive file.

# tar tvfW operavps.tar

Best Method to Extract Files Without Extracting Directories

Use the --strip-components option if you don’t want the directory structure in the tar file to be replicated on your hard disk. A numerical parameter is necessary for the --strip-components option. The number indicates how many levels of directories should be disregarded. Although the directory structure is not mirrored on your hard drive, files from the disregarded directories are still extracted.

With our example tar file, the top-most directory for ukulele songs is not produced on the hard disk if we provide --strip-components=1. The target directory extracts the files and directories that would have been extracted from that directory.

tar -xvzf ukulele_songs.tar.gz --strip-components=1

In our example tar file, there are only two layers of directory nesting. As a result, no additional folders are produced when --strip-components=2 is used; all files are extracted into the target directory.

tar -xvzf ukulele_songs.tar.gz --strip-components=2

How to back up in Linux Using tar command?

You can also use the tar command to back up to non-tape devices which means the below examples assume that you do not have a tape drive in your system. So, if you have a tape drive, you just need to replace the filename with your tape device file.

#tar -cf <filename_to_be_created> <filename_to_be backed_up>

Use the -t (table of contents) option along with the -f option, as illustrated below, to view the contents of a tar ball.

#tar -tf <filename>

Listing the contents of the tar ball in detail is something you frequently wish to see. To get more details, use the -v (verbose) option, as seen in this command.

#tar -tvf <filename>

Use the -x (extract) option along with the -f option, as indicated in the following command, to extract the whole contents of the tar ball into the current directory.

#tar -xf <filename>

You can specify a file type at the end of the command if you only want to extract that kind of file. In the example that follows, we have made it clear that I only need to extract the tar ball’s “.doc” files.

#tar xvf <filename> –-wildcards ‘*.doc’

FAQ

You can use File Manager. Right-click on your target file and select ‘’Extract’’.

If a destination location is not specified, the.bz2 file will be extracted in the current working directory.

An uncompressed or compressed tar file is available. generally utilizing gzip or bzip2 to compress them. Both types will be decompressed by the tar program, which will also extract the files from the archive.

No, the czf command is not a valid command. To compress a file, you can use tar -czvf (archive name).tar.gz. Also, tar -xzyf is used to extract an archive to your current folder.

Conclusion

In this article, you learned how to use the tar command in Linux to compress, uncompress, untar, create, and extract files with tar. It offers a variety of command options to assist you in managing archive files produced by several algorithms, including gzip, xz, and bzip2. Reviewing all the practical tar command examples in this article helps you to be an expert in using tar as an essential command. But if you know any more commands, share them with us and our Linux fans.

Leave a Reply

Your email address will not be published. Required fields are marked.


Henio Publish in June 8, 2023 at 6:21 pm

How to uncompress tar archive and create a tar file?

    Ashley Publish in June 9, 2023 at 5:29 pm

    By running the below command, you can create a tar file called file.tar which is the archive of all .c files. $ tar cvf file.tar *.c

Selena20 Publish in June 7, 2023 at 1:29 am

How to compress gzip?

    Ashley Publish in June 8, 2023 at 5:45 pm

    You can use -z option and create a tar file: $ tar cvzf file.tar.gz *.c

Zoei Publish in June 6, 2023 at 5:03 pm

what is tar jxvf used for?

    Ashley Publish in June 6, 2023 at 7:22 pm

    You can use jxvf to extract a single file from .tar.bz2 or extract a group of files as you reviewed the related example in the article.

Sophie8 Publish in June 3, 2023 at 4:33 am

Is utar xz possible on macOS?

    Ashley Publish in June 3, 2023 at 4:35 am

    Yes. The tar utility is pre-installed on macOS. The archive file name comes after the -f option when using the tar command with the --extract (-x) option: tar -xf archive.tar.xz

Karunia Publish in June 3, 2023 at 4:32 am

How to use tar xvf command to extract files from archives.

    Ashley Publish in June 3, 2023 at 4:33 am

    Run the following command: $ tar xvf file.tar