| by Arround The Web | No comments

How to tar a Folder in Linux?

Linux offers a pre-installed ‘tar (tape archive)’ command utility tool. It is used for the compression and extraction of “.xz”, “gz”, “.tbz”, and “.bz2” extensions. It is beneficial to compress large folders and files into an archive file. The compressed folders take less storage to transfer from one system to another.

This post pens down brief details to tar a folder in Linux with the following highlights:

How to tar a Folder in Linux?

The creation of a tar folder depends on its basic syntax which is defined below:

$ tar cf [File_name.tar] [Directory/Folder to Archive]

The syntax contains the following parameters:

  • tar: Represents the “tar” command line utility.
  • c: Creates the required tar file.
  • f: Specifies the created archive file name.
  • tar: Identifies the compressed tar archive.
  • Directory/Folder to Archive: Shows folder name that needs to be compressed.

By following the above syntax, tar the “Extra” directory having the following files and subdirectories:

$ ls -l

Example 1:  Create a “.tar” Folder

Create the “ComExtra.tar” simple compressed tar archive of “Extra” directory using the above “tar” command syntax:

$ tar cf ComExtra.tar Extra

The “Extra” has been successfully compressed into a “ComExtra.tar” file.

Example 2:  Create a “tar.gz” Folder

The “tar” command also supports the commonly used gzip compression format having “tar.gz”, “tgz”, and “tar.gzip” extensions. For this purpose, “tar” offers the “-z (gzip format)” flag.

Now we compressed the “Extra” folder into the “tar.gz” compression format using the command defined below:

$ tar czf GzipExtra.tar.gz Extra

Append a tar Folder in Linux

Once the “tar” folder is created, it can easily be customized or extended without extraction.

Use the “r(append)” flag of the “tar” command to append the “Extra.tar” folder by adding three files on its end:

$ tar -rvf ComExtra.tar FileA.txt FileB.txt FileC.txt

The “three” desired files have been added into the “ComExtra.tar” folder that can be seen using the “-v(verbose)” flag.

View a tar Folder in Linux

All the tar files and folders can be displayed in the terminal by using the “-t(list tar content)” flag in this way:

$ tar -tf GzipExtra.tar.gz

The output is the same as the output from the “-v” flag.

Extract a tar Folder in Linux

The “tar” command also supports the extraction of “tar” files/folders into the current working directory or the specific directory.

Syntax:

The basic syntax to extract the “tar” file is written here:

$ tar xf [file_name.tar]

The “x” flag denotes the extraction of the tar archive.

Example 1: Extract “.tar” Folder in PWD

To extract the “tar” folder in the current working directory simply utilize the above syntax and specify the compressed “ComExtra.tar” folder in the terminal:

$ tar xf ComExtra.tar

The compressed “ComExtra.tar” folder has been extracted.

Example 2: Extract the “tar.gz” Folder in a Specific Directory

The “-C(–directory)” flag is beneficial to extract the desired tar folder in the particular location where the user wants.

In this example, the “GzipExtra.tar.gz” compressed archive located in the “Home” is extracted into the “Sample” directory in this way:

$ tar xzf GzipExtra.tar.gz -C /home/linuxhint/Sample/

The output verifies that “GzipExtra.tar.gz” has been completely extracted into the “Sample” directory.

Example 3: Extract Specific File From “tar” Folder

Simple type the name of the specified file/subdirectory that you want to extract from the “tar” folder with the “tar” utility in the following way:

$ tar xzvf GzipExtra.tar.gz Extra/Dir1

The “Dir1” directory has been extracted from the “GzipExtra.tar.gz” compressed file.

Conclusion

To tar a folder in Linux, users can execute the “tar cf [File_name.tar] [Directory/Folder to Archive]” command. Using this, you can compress and decompress the tar folder. Additionally, users can append, view, and extract a tar Folder in Linux. This post has demonstrated a set of practical examples that illustrates the creation and extraction of a tar folder in Linux.

Share Button

Source: linuxhint.com

Leave a Reply