Introduction
TAR files are created and compressed to allow easy data transfer between different devices. There are many names for the tar command in Linux and Unix, including gzip, tarball, and bzip. Tar symbolizes tape archives in Linux and is commonly used to back up tape drives in Unix and Linux systems. In the following paragraphs, we will discuss tar command illustrations and how to use tar in more detail.
Tar Linux Command Syntax Examples
Tape archives are created using the Linux tar command and extracted with the Linux unzip command. As part of Linux, the tar Linux command functions as an essential tool for archiving.
One definition of an Archive file is a file that contains one or more files along with metadata. Multiple files can be gathered into one to enhance storage and portability or compressed to make storage space more efficient with the right tar.gz commands and other tar options
With Linux tar, we can create and maintain Archive files. This list of commands shows you how to tar a directory in UNIX with examples, as well as compress and uncompress them by modifying the tar command using these options:
c | Generates an Archive |
x | Extraction of the archive |
t | Lists or displays files within an archive |
r | updates or adds files or directories to existing .tar files |
u | archiving and adding to existing archives |
v | Displays detailed information |
j | using tbzip to filter tar archives |
W | Verify or check the integrity of a file |
A | Concatenates the archives |
z | zip tells tar Linux command to use gzip to create the tar file |
f | Creates a file-based archive |
Making a New tar Archive File
# tar -cvf (name of file).tar /home/(name of directory)
This line command tar example creates a new tar archive file, complete with the name and date for a particular directory. Note that the file and directory names and dates used here are fictional. The letters ‘tar command in Linux’ in this tar Unix command refers to the specific function listed in the bale above – c: create a new .tar file, v: shows progress on the .tar file verbosely, and f: type of file name of the new archive file.
Extracting Archive files
$ tar xvf file.tar
These tar commands in Linux will extract specific archive files using the syntax ‘xvf’.
Creating a tar.gz Archive File
# tar -cvf (name of file)-11-05-2021.tar.gz home/(name of compressed file)
$ tar cvzf file.tar.gz *.c
The option ‘z’ placed in the line tar Linux commands will make a compressed gzip file. Note that ‘tgz’ and ‘tar.gz’ are identical while the file name is ‘file.tar.gz. If the file is an archive of the .c files, then apply the second Linux tar command.
Extracting gzip Archive File
$ tar xvzf file.tar.gz
This tarball Linux command extracts files from the tar archive directly ending with ‘file.tar.gz’.
Making New Compressed Linux files
$ tar cvfj file.tar.tbz example.cpp
The list shows a ‘j’ option in the tar command in Linux that can be applied to make new archive files and compress them into a smaller size than the gzip file. The decompress and compress function takes longer to process.
Making New tar.bz2 Archive Files
# tar cvfj (name of file).tar.bz2 /home/(Directory name)
# tar cvfj (name of file).tar.tbz /home/(Directory name)
# tar cvfj (name of file).tar.tb2 /home(Directory name
The ‘j’ options compresses a tar file, and the bz2 option compresses and archive files that are smaller than gzip files. The bz2 file compression option takes even more time.
Untar Specific Files or Directories in Linux
$ tar xvfj file.tar
$ tar xvfj file.tar -c (file/directory path)
This tar xvf line will untar a file in the directory that you specify. You need to add the ‘c’ option.
Untar Single tar, tar.gz, tar.bz2 Files
# tar -xvf file name.sh.tar cleanfiles.sh
# tar --extract --file=file name.sh.tar cleanfiles.sh
Or
# tar -zxvf filename.tar.gz directory.xml
# tar --extract --file= filename.tar.gz directory.xml
Or
# tar -jxvf filename.tar.bz2 home/(directory name).php
# tar --extract --file= filename.tar.bz2 /home/(directory name).php
You have two options when using this command – extract a single .tar file from the archive using the filename.sh affix to extract from the filename.sh. These commands will bring up specified tar.gz files from the tar.gz pr tar.bz2 directories.
Untar Multiple Files in the .tar, .tar.tbz or .tar.gz Formats
$ tar xvf file.tar "fileA" "fileB"
$ tar zxvf file1.tar.gz "fileA" "fileB"
$ tar jxvf file2.tar.tbz "fileA" "fileB"
These commands extract or untar several files from the archive files at a time if they have the .tar, .tar.tbz, tar.bz2 or .tar.gz names. File A and B refer to the specific file names.
Decompress tar.gz Files
# tar -xvf thumbnails-14-09-12.tar.gz
# tar -xvf (file name)-14-09-12.tar -C /home/(name of the directory)
This command decompresses files with the tar.gz name. If you need to apply this function to multiple directories, then apply the ‘c’ option.
Decompress tar.bz2 Files
# tar -xvf (file name)-14-09-12.tar.bz2
The tar.bz2 is a heavily compressed file, and decompressing it takes time and patience. The command untars file from an archive file.
Viewing all tar, tar.gz, tar.bz2 Archive File Content
# tar -tvf uploadprogress.tar
# tar -tvf staging.directory.com.tar.gz
# tar -tvf file name-directory name.tar.bz2
To see all of the tar content you have stored in the directory, try this command. Just like the above command, this one will show you all of the current tar.gz archive content. Similarly, view all tar.bz2 with the third command.
Wildcard Group File Extract
# tar -xvf filename.tar --wildcards '*.php'
# tar -zxvf filename.tar.gz --wildcards '*.php'
# tar -jxvf filename.tar.bz2 --wildcards '*.php'
Linux understands a wildcard extracting command for extracting a group of files at a single time from tar, tar.gz, or tar.bz2 archive files. Ideally, the file should have a similar name, for example – .php.
Adding New Directories or File to Existing tar Archive Directory
# tar -rvf filename-14-09-12.tar xyz.txt
# tar -rvf filename-14-09-12.tar php
The ‘r’ option in this command lets you add a new file or directory to existing tar archive directories. This command example adds the file called ‘xyz.txt’ and a new ‘PHP’ directory to the ‘file name’.tar archive file.
Adding New Directories or File to Existing tar.bz2 Archive Directory
# tar -rvf filename-14-09-12.tar.gz xyz.txt
# tar -rvf filename.tar.bz2 xyz.txt
Leads to error notification:
“tar: This is not a tar archive”
“tar: Skipping to next header xyz.txt”
“tar: Error exit delayed from previous errors”
So far, no command adds new files or directories to a compressed existing tar.bz2 or tar.gz. If you attempt it using the above example command, you get the resulting error shown below.
Preventing this Error
# tar tvfW (archive name)-14-09-12.tar
Using the ‘ W ‘ option, you can use the above command to verify the compressed archive or tar file. Note that no verification can be done on compressed tar.bz2 or tar.gz archive files.
Conclusion
There you have it: how to tar a file in Linux examples from different angles. Combining many files into one file is possible using the tar command in Linux. In Linux, you can use gzip, tar, tar czf, tar cvzf and bzip to compress a group of files into one file. In addition to compressing files, it is also used to make sharing files over the Internet more manageable and to reduce disk space requirements. In addition to decompressing a compressed file, the tar utility can also be employed to recoup the original data in the compressed file.