How to tar a Folder in Linux

9

Tape Archive or tar is a file format for creating files and directories into an archive while preserving filesystem information such as permissions. We can use the tar command to create tar archives, extract the archives, view files and directories stored in the archives, and append files to an existing archive. Tar is a simple yet powerful archiving utility.

This guide will walk you through creating and extracting tar archives on your Linux machine.

Install Tar

In most Linux distributions, you will have tar pre-installed. However, in case you don’t. If that’s your case, use the commands:

Debian/Ubuntu:

Use apt package manager as:

$ sudo apt-get install tar

Arch/Manjaro:

On Arch-based distributions:

REHL/CentOS:

On REHL Family, use yum as:

Tar File Formats

Tar supports uncompressed and compressed archives. Common extensions of the tar archives include:

  • .tar – A raw tar file.
  • .tar.gz, .tgz, .tar.gzip – Gzip tar archive.
  • .tar.bz2, .tbz, .tbz2, .tar.bzip2 – Bzipped tar archive.
  • .tar.Z, .Z, .taz – Compress tar archive.

Tar Basic Usage

Using tar in the command-line adopts the general syntax:

$ tar [OPERATIONS] [OPTIONS] archive_name files/directories_to_archive

We start by invoking the tar utility, followed by the operation to perform. Operations may include:

  • -c – create an archive
  • -x – extract archive
  • -t – shows files and directories in the archive.

Next, we pass the options to modify the utility’s behavior. These options can be -v for verbose or -f for archive name, -z to filter the archive via gzip, and more.

Finally, we pass the archive name and the files and directories to add to the archive.

How to Create a Simple Archive

As we mentioned above, tar supports a range of compressions. To specify the type of archive to create, add the desired extension to the file name. For example, to create a gzipped tar archive, enter the filename as myarchive.tar.gz

Use the command below to create the archive with the files: file1, file2, file3, file4

$ sudo tar -c -f myarchive.tar file1, file2, file3

The -c tells tar to create a new archive. The -f flag specifies the file name.

How to Tar a Directory

To create a tar archive in a directory with all the files and sub-directories, pass the directory path. From there, tar recursively finds all the files and directories and adds them to the archive.

An example command is:

$ sudo tar -c -f -z -v gzipped.tar.gz /home/ubuntu

You can suppress recursive directory archiving using the –no-recursion flag.

How to Show Contents of a tar Archive

To view the files and directories in a tar archive, we use the -t option. For example:

$ tar -t -f gzipped.tar.gz

How to Extract a Tar Archive

To extract a tar archive, use the -x operation flag for extraction. You can pass any tar compression format such as gzip, lzma, bz2, and more.

The most common command for extracting tar archive files is:

$ tar -xvf archive.tar.[extension]

To extract a simple tar archive:

The command will extract the contents of the archive to the current directory.

How to Extract a Specific File

In some instances, you may need to get specific files from an archive. To do this, pass the filenames to the tar command separated by space.

For example:

$ tar -xvf sample.wma info.txt backup.log

How to extract to a specific directory

As mentioned, tar extracts the archive in the current working directory. To change the directory where the archived files are extracted, use the -C flag as:

$ mkdir ~/myarchive && tar -xvf myarchive.tar -C /home/ubuntu/myarchive

How to Append Files to an Archive

To add files to an existing archive, we use the -r fo append followed by the name of the archive and the files to add.

For example:

$ tar -rvf myarchive file1 file2 file3

How to Remove Files from an Archive

Using the –delete option, we can remove specific files from an archive as:

$ tar -xvf –delete myarchive.tar file1 file2 file3

Conclusion

Tar is a valuable utility in Linux, most packages are archived in tar format, and it is helpful to know how to use the tool.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More