How to use the command 'tar' (with examples)

How to use the command 'tar' (with examples)

The ’tar’ command is a versatile archiving utility that is commonly used in Unix-like operating systems. It allows users to create and manipulate tar archives, which are collections of files bundled together into a single file. Additionally, tar can be combined with compression methods such as gzip or bzip2 to create compressed archives.

Use case 1: Create an archive and write it to a file

Code:

tar cf path/to/target.tar path/to/file1 path/to/file2 ...

Motivation: This use case is useful when you want to create a tar archive from one or multiple files and save it to a specific location.

Explanation:

  • ‘c’ flag: Specifies the action to create a new archive.
  • ‘f’ flag: Indicates that the following argument is the filename of the archive.
  • ‘path/to/target.tar’: Specifies the path and filename of the target archive.
  • ‘path/to/file1 path/to/file2 …’: Specifies the path and filename(s) of the file(s) to be included in the archive.

Example output:

If the command is executed as follows:

tar cf /tmp/archive.tar /home/user/file1.txt /home/user/file2.txt

The command will create a tar archive named ‘archive.tar’ in the ‘/tmp’ directory, including ‘file1.txt’ and ‘file2.txt’ from the ‘/home/user’ directory.

Use case 2: Create a gzipped archive and write it to a file

Code:

tar czf path/to/target.tar.gz path/to/file1 path/to/file2 ...

Motivation: This use case allows you to create a compressed tar archive in gzip format, which can greatly reduce the file size for efficient storage or transmission.

Explanation:

  • ‘c’ flag: Specifies the action to create a new archive.
  • ‘z’ flag: Enables gzip compression for the archive.
  • ‘f’ flag: Indicates that the following argument is the filename of the archive.
  • ‘path/to/target.tar.gz’: Specifies the path and filename of the target archive.
  • ‘path/to/file1 path/to/file2 …’: Specifies the path and filename(s) of the file(s) to be included in the archive.

Example output:

If the command is executed as follows:

tar czf /tmp/archive.tar.gz /home/user/file1.txt /home/user/file2.txt

The command will create a gzipped tar archive named ‘archive.tar.gz’ in the ‘/tmp’ directory, including ‘file1.txt’ and ‘file2.txt’ from the ‘/home/user’ directory.

Use case 3: Create a gzipped archive from a directory using relative paths

Code:

tar czf path/to/target.tar.gz --directory=path/to/directory .

Motivation: This use case is useful when you want to create a gzipped archive from an entire directory, preserving the directory structure.

Explanation:

  • ‘c’ flag: Specifies the action to create a new archive.
  • ‘z’ flag: Enables gzip compression for the archive.
  • ‘f’ flag: Indicates that the following argument is the filename of the archive.
  • ‘path/to/target.tar.gz’: Specifies the path and filename of the target archive.
  • ‘–directory=path/to/directory’: Changes to the specified directory before adding files to the archive.
  • ‘.’: Specifies that all files and subdirectories under the current directory should be included in the archive.

Example output:

If the command is executed as follows:

tar czf /tmp/archive.tar.gz --directory=/home/user .

The command will create a gzipped tar archive named ‘archive.tar.gz’ in the ‘/tmp’ directory, including all files and subdirectories under the ‘/home/user’ directory.

Use case 4: Extract a (compressed) archive file into the current directory verbosely

Code:

tar xvf path/to/source.tar[.gz|.bz2|.xz]

Motivation: This use case allows you to extract the contents of a tar archive, displaying the verbose output to see the progress and details of the extraction.

Explanation:

  • ‘x’ flag: Specifies the action to extract files from the archive.
  • ‘v’ flag: Displays verbose output during extraction.
  • ‘f’ flag: Indicates that the following argument is the filename of the source archive.
  • ‘path/to/source.tar[.gz|.bz2|.xz]’: Specifies the path and filename of the source archive.

Example output:

If the command is executed as follows:

tar xvf /tmp/archive.tar.gz

The command will extract the contents of the ‘archive.tar.gz’ into the current directory, displaying the verbose output with the list of extracted files.

Use case 5: Extract a (compressed) archive file into the target directory

Code:

tar xf path/to/source.tar[.gz|.bz2|.xz] --directory=path/to/directory

Motivation: This use case is useful when you want to extract the contents of a tar archive into a specific target directory.

Explanation:

  • ‘x’ flag: Specifies the action to extract files from the archive.
  • ‘f’ flag: Indicates that the following argument is the filename of the source archive.
  • ‘path/to/source.tar[.gz|.bz2|.xz]’: Specifies the path and filename of the source archive.
  • ‘–directory=path/to/directory’: Changes to the specified directory before extracting files from the archive.

Example output:

If the command is executed as follows:

tar xf /tmp/archive.tar.gz --directory=/home/user

The command will extract the contents of the ‘archive.tar.gz’ into the ‘/home/user’ directory.

Use case 6: Create a compressed archive and write it to a file, using the file extension to automatically determine the compression program

Code:

tar caf path/to/target.tar.xz path/to/file1 path/to/file2 ...

Motivation: This use case allows you to create a compressed archive while automatically determining the compression program based on the file extension (.tar.xz).

Explanation:

  • ‘c’ flag: Specifies the action to create a new archive.
  • ‘a’ flag: Automatically determines the compression program based on the file extension.
  • ‘f’ flag: Indicates that the following argument is the filename of the archive.
  • ‘path/to/target.tar.xz’: Specifies the path and filename of the target archive.
  • ‘path/to/file1 path/to/file2 …’: Specifies the path and filename(s) of the file(s) to be included in the archive.

Example output:

If the command is executed as follows:

tar caf /tmp/archive.tar.xz /home/user/file1.txt /home/user/file2.txt

The command will create a compressed tar archive named ‘archive.tar.xz’ in the ‘/tmp’ directory, including ‘file1.txt’ and ‘file2.txt’ from the ‘/home/user’ directory.

Use case 7: List the contents of a tar file verbosely

Code:

tar tvf path/to/source.tar

Motivation: This use case allows you to view the contents of a tar archive in a verbose format, displaying detailed information about each file stored in the archive.

Explanation:

  • ’t’ flag: Specifies the action to display the contents of the archive.
  • ‘v’ flag: Displays verbose output during listing.
  • ‘f’ flag: Indicates that the following argument is the filename of the source archive.
  • ‘path/to/source.tar’: Specifies the path and filename of the source archive.

Example output:

If the command is executed as follows:

tar tvf /tmp/archive.tar

The command will list the contents of the ‘archive.tar’ in a verbose format, showing the file names and additional information such as permissions, owner, size, and modification timestamp.

Use case 8: Extract files matching a pattern from an archive file

Code:

tar xf path/to/source.tar --wildcards "*.html"

Motivation: This use case allows you to extract specific files from a tar archive using a wildcard pattern, which can be handy when you only need to retrieve certain files matching a specific pattern.

Explanation:

  • ‘x’ flag: Specifies the action to extract files from the archive.
  • ‘f’ flag: Indicates that the following argument is the filename of the source archive.
  • ‘path/to/source.tar’: Specifies the path and filename of the source archive.
  • ‘–wildcards “*.html”’: Specifies the wildcard pattern for selecting files to be extracted. In this example, it selects all files with the extension ‘.html’.

Example output:

If the command is executed as follows:

tar xf /tmp/archive.tar --wildcards "*.html"

The command will extract all files with the extension ‘.html’ from the ‘archive.tar’ into the current directory.

Related Posts

Article: Working with the dir Command (with examples)

Article: Working with the dir Command (with examples)

1: Show the contents of the current directory: Code: dir Motivation: This command allows you to quickly see what files and directories are present in the current directory.

Read More
How to use the command burpsuite (with examples)

How to use the command burpsuite (with examples)

Burp Suite is a GUI-based application that is mainly used in web application penetration testing.

Read More
How to use the command pnmtorle (with examples)

How to use the command pnmtorle (with examples)

This article will illustrate various use cases of the command pnmtorle, which is used to convert a PNM file to an Utah Raster Tools (URT) RLE image file.

Read More