The tar commands are the Linux or Unix-like operating system commands that are used to compress, gather, and archive files. In this post, we will be discussing the tar command in Linux. So, make sure to read and note them all to use them on your system.
The tar stands for tape archive files that create a single archive file from a large number of files for easiness of the user. You can easily know the files with their names that will be ending with “tar” and such files are called “tarballs”.
There are various commands in Linux to create tarballs such as .tar.gz or .tgz archive files. Only a little part of the command keeps changing while the else part remains the same. Since most of the Linux beginner users are not familiar with all the commands, here we will be giving various examples for Linux tar command tape archive files.
- 1 Tar Command In Linux
- 1.1 Create A Tar Archive
- 1.2 Create a tar.gz Archive
- 1.3 Create tar.bz2 Archive
- 1.4 Untar tar Archive
- 1.5 Untar tar.gz Archive
- 1.6 Untar tar.bz2 Archive
- 1.7 View List Content Of tar Archive
- 1.8 View List Content Of tar.gz Archive
- 1.9 View List Content Of tar.bz2 Archive
- 1.10 Untar A Single File From tar Archive
- 1.11 Untar A Single File From tar.gz Archive
- 1.12 Untar A Single File From tar.bz2 File
- 1.13 Untar Multiple Files From tar, tar.gz, and tar.bz2 Archive
- 1.14 Untar Group Of Files Using Wildcard
- 1.15 Add Directories To tar Archive
- 1.16 Add Directories To tar.gz and tar.bz2 Archive
- 1.17 Verify tar, tar.gz, and tar.bz2 Archive
- 1.18 Check The Size Of tar, tar.gz, and tar.bz2 Archive
- 2 How To Use Tar Command In Linux
Tar Command In Linux
The tar command in Linux is not only one command. There are various commands that can be used to compress so many files into an archive. Also, you can extract, modify, and maintain the tape archive files with the tar commands. Therefore, knowing the tar commands is a must.
Create A Tar Archive
To create a tar archive, use the command given as follows. The tar command in Linux will be
The options that we have used above are for the purpose as given below.
- c – Creates a new tar archive.
- v – This verbosely shows tar file progress.
- f – The file type of the archive file.
Create a tar.gz Archive
For creating an archive with the gzip files, we have to use the “z” option. The commands tar.gz and tgz are the same and can be used to compress the gzip files into an archive file.
Create tar.bz2 Archive
The commands tar.bz2, tar.bz, and tb2 do the same function. To compress files that are lesser in size when compared to gzip files, we use the tar command in Linux as follows. The tar.bz2 takes more time to archive files when compared to the tar.gz archive files. Therefore, to create a highly compressed tar.bz2 archive file, we use the “j” option here.
Untar tar Archive
To untar means to extract an archive file. To extract a directory from the archive, we use the “x” option. If you want to extract from a different directory, you can use the “-c” option. The below tar command in Linux will untar the specified file.
Untar tar.gz Archive
The command to uncompress the tar.gz archive files is as shown in the image. Use the “-c” option to extract from a different directory.
Untar tar.bz2 Archive
The highly compressed tar.bz2 files can be untarred with the following command. All the .flv files will be extracted from the archive file.
View List Content Of tar Archive
For the command for viewing all the list contents that are compressed into an archive file, you have to run the command given below. This command will get you the list of all uploadprogress.tar files.
View List Content Of tar.gz Archive
To view the list content of tar.gz archive file, run the command as given below. You will get all the compressed tar.gz archive files on the screen.
View List Content Of tar.bz2 Archive
To view the list content of tar.bz2 archive file, give the command as follows.
Untar A Single File From tar Archive
To uncompress or extract a single file called cleanfiles.sh from the archive tar file cleanfiles.sh.tar, use the following command.
Untar A Single File From tar.gz Archive
To untar a single file from the tar.gz highly compressed archive files, use the following tar command in Linux.
Untar A Single File From tar.bz2 File
To extract a single from a tar.bz2 highly compressed archive file, run the following tar command in Linux.
Untar Multiple Files From tar, tar.gz, and tar.bz2 Archive
If you want to untar multiple files from more than one archive file, you can give the following command.
Untar Group Of Files Using Wildcard
For extracting a group of files from archive files, we have to use Wildcard. To extract a group of files with .php type from tar, tar.gz, or tar.bza2 archive files, use the Linux tar command as given below.
Add Directories To tar Archive
To add a new directory to the existing archive file, we use the path option as r(append). For example, here we are adding php and xyz.txt directories to the already archive file.
Add Directories To tar.gz and tar.bz2 Archive
There are no commands for adding directories to an already existing tar.gz or tar.bz2 highly compressed archive file. If you try adding a directory to either of them, you will get an error.
Verify tar, tar.gz, and tar.bz2 Archive
The option we use to verify is W(verify). To verify a tar archive file, use the tar command in Linux as given below. However, verifying tar.gz and tar.bz2 is not supported.
Check The Size Of tar, tar.gz, and tar.bz2 Archive
You can check the size of tar, tar.gz, and tar.bz2 archive files with the following command. The size will be given in KB.
How To Use Tar Command In Linux
To use the tar command in Linux, just follow the simple steps given here.
- Go to the terminal program on your Linux system.
- Now, run “tar -zcvf file. tar. gz/path/to/dir/ command to compress a directory into a file.
- To compress a single file into an archive, use the “tar -zcvf file. tar .gz/path/to/dir/ command.
- And to compress multiple directories into an archive, run the “tar -zcvf file. tar. gz dir1 dir2 dir3 command.
This was the simple procedure to use the tar command in the Linux system. The commands vary according to the file size and number of files. Use the tar command in Linux that we have given above using this procedure.
We hope you found this article helpful and got an idea about the tar command in Linux. You can use the tar commands to compress large files into an archive file. For more related articles, visit our website Techly Fire.