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.
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
.
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
archivef
: to determine the archive file’s file name typeThe 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.
The cvzf
flag is used to perform gzip compression on file1
and file2
, resulting in a single archive file named arch.tar.gz
.
Similarly, a gzip tar archive may be extracted using the xvzf
flag.
The xvfj
flag is used to untar a specified directory or tar
file.
The czf
flag is used to output the archive file size in Kilobytes (KB).
1 KB is equal to a thousand bytes!
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.
The tf
flag is used to display the contents of a tar
archive.
To verify that a compressed archive exists, we use the tvfW
flag and send the name of the archive as the only parameter.
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.
To extract the contents of a .gz archive, we use the tvf
flag.
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.
Free Resources