Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

tar
linux

What is the tar command in Linux?

Rukhshan Haroon

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The tar command in Linux comes as part of the GNU Core-utilities Package and creates or extracts archived files. The keyword tar stands for tape archive.

The GNU Core-utilities Package is available on all Unix-like operating systems.

Arguments

The tar command has several functionalities, which can be accessed using different flags. In this shot, we will review some of the most frequently used arguments for tar.

tar cvf arch.tar file1 file2

The cvf flag is used to create an uncompressed tar archive of the files sent as arguments to the tar command. Multiple filenames may be sent at once. In the example above, the name of the archived file would be arch.tar.

The cvf flag combines the following three options:

  • c: to create a new archive of type tar
  • v: to create a verbose report of the tar archive
  • f: to determine the archive file’s file name type

tar xvf arch.tar

The xvf flag is used to extract the files in an archived file. The name of the archived file is sent as an argument to the tar command.

tar cvzf arch.tar.gz file1 file2

The cvzf flag is used to perform gzip compression on file1 and file2, resulting in a single archive file named arch.tar.gz.

tar xvzf arch.tar.gz

Similarly, a gzip tar archive may be extracted using the xvzf flag.

tar xvfj arch.tar

The xvfj flag is used to untar a specified directory or tar file.

tar czf arch.tar | wc -c

The czf flag is used to output the archive file size in Kilobytes (KB).

1 KB is equal to a thousand bytes!

tar rvf arch.tar file3

To add a file to a pre-existing tar archive, we use the rvf flag. The name of the tar archive and the new file are sent as parameters to the tar command.

tar tf arch.tar

The tf flag is used to display the contents of a tar archive.

tar tvfW archive.tar

To verify that a compressed archive exists, we use the tvfW flag and send the name of the archive as the only parameter.

tar -xvf archive.tar --wildcards ‘*.py’

To extract only a subset of files from a tar archive, we use wildcards. The command above would extract all files that end with the .py extension in archive.tar.

tar tvf archive.gz

To extract the contents of a .gz archive, we use the tvf flag.

Example

We use the ls command to list down the files and folders in the current directory in the example below. Using the cvf flag of the tar command, we create a tar archive of the bin folder.

Once the archive has been made, we display its contents using the tvf flag and extract them using the xvf flag of the tar command.

Terminal 1
Terminal

RELATED TAGS

tar
linux

CONTRIBUTOR

Rukhshan Haroon
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring