Tar command in Linux/Unix with ExamplesThe tar command is short for tape archive in Linux. This command is used for creating Archive and extracting the archive files. In Linux, it is one of the essential commands which facilitate archiving functionality. We can use this command for creating uncompressed and compressed archive files and modify and maintain them as well. Tar is a utility of computer software to collect several files into a single archive file in computing. Often, it is known as tarball for backup and distribution purposes. The title is acquired from "tape archive" because it was actually developed to specify data on sequential I/O devices using none of their file systems. The archive data groups made by tar include many file system parameters like directory organization, file access permission, ownership, timestamps, and name. In favor of pax, POSIX abandoned tar, tar yet sees widespread use. First, it was announced in Version 7 Unix in January 1979, substituting the tp program. To store the data, the file structure was standardized in POSIX.1-1988 and later POSIX.1-2001 and became a pattern supported by almost all modern file archiving systems. Unix-like operating systems generally contain tools for supporting tar files and utilities used for compressing them, like bzip2 and gzip. File formats of tarThere are many tar file formats available, including current and historical ones. Two tar formats are written in POSIX: pax and ustar. HeaderThe file header record includes the metadata of a file. In the header record, the information is encoded in the ASCII standards to ensure flexibility across distinct architectures using distinct byte orderings. Hence, if every file is an ASCII text file in an archive and contains ASCII names, the archive is an ASCII text file (having several NUL characters). Several fields are mentioned in the following table, defined by the actual Unix tar format. The link file/indicator type table contains a few modern extensions. A field is filled with various NUL bytes if it's unused. Pre-POSIX.1-1988 tar header:
Ustart formatIn the Ustar format, almost every modern tar program write and read archives, announced by the POSIX IEEE P1003.1 standard from 1988. It introduced extra header fields. Previous tar programs would avoid the additional information, but new programs will check the "Ustar" string presence to decide if the newer format is in use.
POSIX.1-2001/paxSun proposed a technique to add extensions to a tar format in 1997. Later, it was approved for the POSIX.1-2001 standard. The format is called pax format or extended tar format. Some tags are specified by the POSIX standard, including mtime, atime, linkpath, gname, uname, sizes, gid, uid, and a character set definition for group/user names and path names. Key implementations of tarThe key implementations are mentioned in the origin order:
Additionally, most cpio and pax implementations can create and read two or more tar file types. Syntax of tar command:Options in the tar commandVarious options in the tar command are listed below:
Introduction to Archive FileThe archive file can be defined as a file that contains multiple files with metadata. These files are used for collecting more than one data file together in an individual file for easier storage and portability. It can be also used for compressing files to consume less storage space. Examples of tar commandSome of the important examples which are widely used in tar command are as follows: 1. Making an uncompressed tar archive with -cvf optionThis option makes a tar file known as file.tar. It is the archive of every .txt file inside mydir directory. The command is as follows: 2. Extracting files through the archive with -xvf optionThis option can extract files through archives. The command is as follows: 3. gzip compression over tar archive with -z optionThis option makes a tar file known as file.tar.gz. It is the archive of every .txt file. The command is as follows: 4. Extracting the gzip tar archive with -xvzf optionThis option can extract the files through file.tar.gz tar archived files. The command is as follows: 5. Making compressed tar files with the -j optionThis option will help us to create and compress archive files. Both decompress and compress takes more time as compared to gzip. The command is as follows: 6. Untar single specified directory or file in LinuxThis option is used to untar any file in our current directory or inside the specified directory with the -C option. The command is as follows: Or, 7. Untar multiple .tar.tbz, .tar.gz, .tar files in LinuxThis option will help us to untar or extract more than one file from tar.bz2, tar.gz, and a tar archive file. The example of this option is as follows: Or, Or, 8. Check the size of the existing tar.tbz, tar.gz, tar fileThe command will help us to show the archive file's size in kilobytes (KB) which is mentioned above. The command is as follows: Or, Or, 9. Update the existing tar fileIn Linux, the command for updating an existing tar file is as follows: 10. Content list and describe the tar file with the tf optionThis option will help us to list the whole archive file's list. Also, we can list particular content inside any tar file. The command is as follows: 11. View the archive with the -tvf optionIn Linux, we can use the -tvf option to view the archive. The command is as follows: 12. Pass the filename as the argument to find a tar fileThis option can view the archived files with their information. The command is as follows: 13. Using pipe for throwing 'grep command' to search what we are searching forThis option will help us to only list the mentioned image or text in grep through archived files. The command is as follows: Or, Introduction to WildcardsAlternatively, wildcards are referred to as a wildcard character or wild character in Linux. It is a symbol that is used for representing or replacing multiple characters. Typically, wildcards are either a question mark (?) which illustrates an individual character or an asterisk (*) which illustrates multiple characters. Example-14. Find a .png format imageIt will help us for extracting only files along with the .png extension from the file.tar archive. The -wildcards option informs tar for interpreting wildcards in the file name to be extracted. The name of the file (*.png) is enclosed inside the single quotes for protecting the wildcard (*) through being incorrectly expanded by any shell. The command is as follows: Note: In the above command, the "*" symbol is applied in the position of the name of the file for taking each file available in that specific directory.15. Delete files from the tar archiveWe can use the --delete option for removing files and a tar archive. The command is as follows: Example: Output: hello1.txt file has been removed from the file.tar archive: Next TopicLinux Find File |