The tar command in Linux, which stands for ‘tape archive’, is an essential command for creating and managing archives. It was originally designed for use with tape drives but is now widely used for different purposes, such as making tar backups, sharing files, and compressing data. In this article, we will explore how to use the ‘tar’ command in Linux, including its basic functions, and provide practical examples.
Table of Contents:
- Syntax of ‘tar’ command in Linux
- Tar Command Usage and Options
- What is an Archive file?
- Tar Command Examples
- Creating Tar Archives in Linux
- Generating an Uncompressed Archive
- Extracting Files and Directories in Linux
- Listing and Verifying Archives in Linux
- Appending Files to an Archive
- Updating Files in an Archive
- Concatenating Multiple Tar Files in Linux
- Estimating Archive Size
- Using Wildcards with the Tar Command
- Summary
Syntax:
The basic syntax for the tar command is as follows:
tar [options] [archive-name] [file/directory...]
- options: These are various flags and parameters you can use to customize the behavior of the tar command.
- archive-name: This is the name of the archive file you want to create or manipulate.
- file/directory: These are the files or directories you want to include in the archive.
Note: archive file name is optional. If the archive file name is omitted, the tar command will create an archive file with the name tar.
tar Command Options
Here are some commonly used options for the ‘tar’ command in Linux:
- -c: The ‘-c’ option with the ‘tar’ command is used to create a new archive.
- -x: If you need to extract files from an archive, you can use the ‘-x’ option with the ‘tar’ command.
- -v: When you want to see a detailed progress report during the archiving or extraction process, you can activate verbose mode with the ‘-v’ option in the ‘tar’ command.
- -f: To specify a particular name for your archive file, use the ‘-f’ option in conjunction with the ‘tar’ command.
- -z: When you want to compress your archive using the gzip compression algorithm, you can employ the ‘-z’ option with the ‘tar’ command.
- -j: Similarly, if you prefer bzip2 compression for your archive, you can enable it using the ‘-j’ option in the ‘tar’ command.
- -t: The ‘-t’ option allows you to list the contents of an archive, providing you with an overview of what’s stored within it.
- -r: If you want to add or append files to an existing archive, the ‘-r’ option can be used with the ‘tar’ command.
- -p: It is used to preserve permissions and ownership of files and directories when creating or extracting an archive.
What is an Archive file?
An archive file is a single file that holds multiple compressed or uncompressed files and folders, making data storage and organization more convenient.
tar Command Examples:
Example 1: Creating a Tar Archive
To initiate the archiving process with the ‘tar’ command, you can use the following syntax:
tar -c[f|j|z] [archive-name] [file/directory...]
- The ‘-c‘ flag instructs ‘tar’ to create a new archive.
- ‘[f|j|z]‘ indicates the compression method: ‘f’ for no compression, ‘j’ for bzip2 compression, and ‘z’ for gzip compression.
- ‘[archive-name]‘ specifies the name of the resulting archive file.
- ‘[file/directory…]‘ lists the files or directories you want to include in the archive.
Example:
For instance, to create a compressed archive named ‘myarchive.tar.gz’ from the ‘myfolder’ directory, you can use:
tar -czf myarchive.tar.gz myfolder
Example 2: Creating an Uncompressed Archive using tar Command
Syntax for Creating an Uncompressed Archive:
You can also create an uncompressed archive with the ‘tar’ command. Here’s how:
$ tar cvf archive_name.tar dirname/
- ‘c’: To create a new archive.
- ‘v’ (optional): To verbosely list files being processed.
- ‘f’: To specify the archive file name.
Example:
This command creates ‘archive_name.tar’ from all the files in the ‘dirname/’ directory. Alternatively, you can specify individual files:
$ tar cvf archive_name.tar abc def
Example 3: Creating a Compressed Archive using tar Command
(i) gzipped tar:
To create a gzip-compressed tar archive, use the ‘z’ option like this:
$ tar cvzf archive_name.tar.gz dirname/
Note: ‘.tgz’ is the same as ‘.tar.gz’. Do not use a hyphen (-) before the options for this to work properly.
(ii) bzipped tar:
For a bzip2-compressed tar archive, use the ‘z’ option as follows:
$ tar cvfj archive_name.tar.bz2 dirname/
Note: ‘.tbz’ and ‘.tb2’ are the same as ‘.tar.bz2’. Please remember that bzip2 compression takes more time to compress and decompress compared to gzip. However, bzip2 archives have smaller sizes.
Example 4: Extracting Files from an Archive
To extract files from an archive, use the ‘x’ option followed by the ‘f’ option to specify the archive file. For instance, to extract the contents of “archive_name.tar” into the current directory, you would use:
$ tar xvf archive_name.tar
Example 5: Extracting a compressed tar archive
(i) Extracting a gzipped tar:
To open a compressed tar file created with gzip, use this command:
$ tar xvfz archive_name.tar.gz
(ii) Extracting a bzipped tar:
For a compressed tar file created with bzip2, use this command:
$ tar xvfj archive_name.tar.bz2
Example 6: Listing an uncompressed archive using the tar command
Before extracting, you can view the content of a *.tar file with this command:
$ tar tvf archive_name.tar
Example 7: Listing a compressed archive using the tar command
(i) Listing a gzipped tar:
To list the contents of a gzip-compressed tar archive, use the ‘z’ option:
$ tar tvfz archive_name.tar.gz
(ii) Listing a bzipped tar:
For a bzip2-compressed tar archive, use the ‘j’ option:
$ tar tvfj archive_name.tar.bz2
Note: If there are lots of files in the archive, you can use a “less” command to read through the list.
Example 8: Extract a single file from tar, tar.gz, tar.bz2 file
To extract a specific file from a tar archive, include the file name at the end of the ‘tar xvf’ command:
$ tar xvf archive_file.tar /path/file
Use the ‘z’ or ‘j’ option as per the compression method (gzip or bzip2):
$ tar xvfz archive_file.tar.gz /path/file
OR
$ tar xvfj archive_file.tar.bz2 /path/file
Note: When dealing with multiple files sharing the same extension, you can use the wildcard option:
$ tar xvf archive_file.tar --wildcards '*.c'
Example 9: Extract directory from tar, tar.gz, tar.bz2 file
To extract a directory from a tar archive, add the directory name at the end of the ‘tar xvf’ command like this:
$ tar xvf archive_file.tar /path/dir/
If you need to extract multiple directories from the archive, specify each directory individually at the end:
$ tar xvf archive_file.tar /path/dir1/ /path/dir2/
Make sure to use the ‘z’ or ‘j’ option based on whether the file is gzip or bzip2 compressed.
Example 10: Appending Files to an Archive
To add a file or directory to an existing archive, you can use the ‘-r’ option:
$ tar rvf archive.tar abc
This command appends the file ‘abc’ to ‘archive.tar’. Adding a directory follows a similar approach:
$ tar rvf archive.tar dir/
Note: You cannot add files or directories to a compressed archive. Attempting to do so will result in a “tar: Cannot update compressed archives” error.
Example 11: Updating Files in an Archive
The -u option allows you to update files in an archive. If a file with the same name already exists in the archive, it will be replaced. For example, to update “existingfile.txt” in “myarchive.tar,” use:
tar uf myarchive.tar existingfile.txt
Example 12: Concatenating Multiple Tar Files in Linux
You can concatenate multiple archives into one using the -A option. For example, to concatenate “archive1.tar” and “archive2.tar” into a new archive called “merged.tar,” use:
tar -Af merged.tar archive1.tar archive2.tar
Example 13: Estimate the tar archive size
To estimate the size of a tar file in kilobytes (KB) before creating it, you can use the following command:
$ tar -cf - /directory/to/archive/ | wc -c
This command calculates the size of the tar file (in bytes) by piping the result of the ‘tar’ command to the ‘wc’ command, which counts the bytes.
Again, make sure to use the ‘z’ or ‘j’ option depending on whether you intend to use gzip or bzip2 compression.
Using Wildcards with the Tar Command
The tar command in Linux supports wildcards for specifying files or directories based on patterns. Here are some common wildcards:
- * matches any sequence of characters.
- ? matches a single character.
- [ ] matches any single character within the brackets.
- { } lets you specify a list of characters or patterns.
- *.py to archive all Python file.s
- myfile* to archive all files that start with the name myfile.
- */foo/ to archive all directories that contain a subdirectory named foo.
For example, to archive all text files, use *.txt. This feature simplifies archiving and managing files with similar naming patterns.
Conclusion:
In summary, the tar command in Linux is a valuable tool for creating, extracting, and managing archive files. With its various options and capabilities, including compression and file manipulation within archives, Linux users can efficiently optimize storage, organize data, and streamline file management.
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Check Information Technology Books
- Apply for Programming Internship
- Check Linux Books
- Practice Programming MCQs