Master the tar command for Efficient Linux File Management
The tar command is a Linux utility used to create, extract, and manage archive files. Developers use the tar command to back up data, compress files, and transfer multiple items as a single file. Common options allow for archiving, compression with gzip or bzip2, and listing or extracting contents. The tar command is essential for file management tasks in Linux environments.
🤖AI Overview:
The tar command is a widely used utility in Unix and Linux systems that gathers multiple files and directories into a single archive file. Its main purpose is to simplify storage, backup, and transfer of data. The tar command makes it easy to manage groups of files as one unit while preserving their structure and permissions. This tool is essential for efficient file management and backup tasks.
Compress & Extract File with tar Command in Linux
Previously on the OperaVPS blog, you learned How To Unzip Files Using Command On Linux. Join us with this guide to learn how to create archived files with the help of tar command examples.
The basic syntax of tar command
To tar a folder in Linux, you will use a command kike below:
$ tar [options][archive-file] [file or dir to be archived]
To get familiar with this syntax and go through the examples of this tutorial, look at the parameters below to see what options tar command provides:
-c: Creates an archive tar file.
-x: This extracts archive file.
-f: Specifies the filename of the archive file.
-v: Prints verbose information for any tar operation on the terminal.
-t: This option lists all the files inside an archive file.
-u: Archives a file and then adds it to an existing archive file.
-r: This option updates a file or directory located inside a .tar file
-z: This creates a tar file using gzip compression
-j: Create an archive file using the bzip2 compression
-W: Verifies an archive file.
-A: This option concatenates the archive files.
-wildcards: This specifies patterns in the UNIX tar command.
-exclude: It excludes files and directories when creating the archive.
-delete: This option removes files and directories from the archive.
Linux tar Command Examples
You can build archive files with the tar utility using a variety of compression methods, including xz, gzip, and bzip2. It is standard practice to append the compression suffix to the archive file. For instance, the file should have the.tar.gz suffix when using gzip compression (with the -z option).
Create Archive File
To create a tar archive file, you can run a command like the following example.
# tar -cvf operavps.tar /home/operavps/
If you are interested in checking what each option used in the tar command says, have a look at the below:
c – creates a fresh.tar archive file.
v – Show the.tar file’s progress in great detail.
f – The archive file’s file name type.
Create tar.gz File
As you know, the “tar” command supports the gzip compression format having “tar.gz”, “tgz”, and “tar.gzip” extensions. For example, to create a compressed tar.gz archive file in Linux, run:
tar cvzf Operavideos.tar.gz /home/Operavideos
Since the tar.gz and tgz are the same, you can also type:
tar cvzf Operavideos.tgz /home/Operavideos
Uncompress tar.gz File
Tar command in Linux is also used to uncompress tar.gz archive files. So, you just need to use the command below to uncompress the tar.gz archive file in Linux:
# tar -xvf operavps.tar.gz
To untar in different directories, use option -C and the directory path.
List Content of tar File
You can run the command below to list the content of the tar archive file in Linux.
# tar -tvf operavps.tar
Create tar.bz2 File in Linux
Tar command in Linux helps you create a highly compressed tar archived file. Bz2 is a feature that compresses data and produces an archive file that is smaller than gzip. Compared to gzip, which takes less time, bz2 compression takes longer to compress and decompress files.
For example, when you want to create a Phpfiles-org.tar.bz2 file in Linux, type:
# tar cvfj Phpfiles-org.tar.bz2 /home/php
OR
# tar cvfj Phpfiles-org.tar.tbz /home/php
OR
# tar cvfj Phpfiles-org.tar.tb2 /home/php
Untar tar.bz2 File
You can easily use the following command when you need to uncompress the highly compressed tar.bz2 file in Linux.
# tar -xvf operavideos.tar.bz2
Untar Single file from tar File
A tar command in Linux can help you to extract a single file. Look at the below example where the cleanfiles.sh will be extracted from cleanfiles.sh.tar.
# tar -xvf cleanfiles.sh.tar cleanfiles.sh
OR
# tar --extract --file=cleanfiles.sh.tar cleanfiles.sh
Exclude Directories and Files
While creating a tar.gz file in Linux, you can exclude files and directories. To do this, run:
# tar --exclude='file1.txt' -zcvf backup.tar.gz /home/operavps
# tar --exclude='/home/operavps/uploads' -zcvf backup.tar.gz /home/operavps
Verify the tar, tar.gz, and tar.bz2 Sizes
Tar command in Linux enables you to check the size of the tar, tar.gz, and tar.bz2 Archive File. Run the command below to view the archive file’s size in Kilobytes (KB).
# tar -czf - operavps.tar | wc -c
# tar -czf – Operavideos.tar.gz | wc -c
# tar -czf - Phpfiles-org.tar.bz2 | wc -c
Untar tar File
Use the following command to untar/extract a tar archive file in Linux.
## Untar files in Current Directory ##
# tar -xvf public_html.tar
To untar in a different directory, use option -C.
## Untar files in specified Directory ##
# tar -xvf public_html.tar -C /home/public_html/videos/
Extract Group of Files in Linux
Using Wildcard, you can extract a group of files. Check the following example to see how you can use this command to extract a group of all .php files from a tar, tar.gz, and tar.bz2 archive file.
# tar -xvf Phpfiles-org.tar --wildcards '*.php'
# tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php'
# tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'
Delete File/Directory from tar
Tar command in Linux helps you to remove files and directories from tar archive. To do this, just run the command below and remove them from the already created tar file.
# tar --delete -f backup.tar.gz file1.txt
# tar --delete -f backup.tar.gz '/home/operavps/uploads'
Untar Multiple files from tar, tar.gz, and tar.bz2 File
Using the examples below enables you to untar/extract your considered files from the tar, tar.gz, and tar.bz2 archive files.
# tar -xvf operavps.tar "operafileA" "fileB"
# tar -zxvf Operavideos.tar.gz "operafileA" "fileB"
# tar -jxvf Phpfiles-org.tar.bz2 "operafileA" "operafileB"
Extraction of a Specific File Extension in tar
Using the tar command in Linux helps you to extract specific File extensions in the tar archive. To do this, run:
# tar -xvf backup.tar.gz --wildcards '*.png'
The above command extracts files with the specific extension .png from the tar archive file.
Extract Specific Files from a tar.bz2 File
You can extract a specific file(s) from a tar.bz2 file. To do this, append a space-separated list of file names to be extracted after the archive name:
tar -xf archive.tar.bz2 file1 file2
You must specify the exact names of the files, together with the path, as printed by the --list
(-t
) option while extracting them. Multiple files can be extracted from an archive much like multiple directories may be extracted from an archive:
tar -xf archive.tar.bz2 dir1 dir2
An error message similar to the one shown below will appear if you attempt to extract a file that doesn’t exist in the archive:
tar -xf archive.tar.bz2 README
output
tar: README: Not found in archive
tar: Exiting with failure status due to previous errors
You can use a wildcard pattern to extract files from a tar.bz2 file with the –wildcards option. To stop the shell from reading the pattern, it must be quoted.
For instance, you could use the following command to extract only files with the extension.md (Markdown files):
tar -xf archive.tar.bz2 --wildcards '*.md'
List Content tar.gz File
To list the content of the tar.gz file in Linux, type:
# tar -tvf staging.operavps.com.tar.gz
Add Files/Directories to tar File
To add files or directories to the existing tar archive files in Linux, use the example below in which the file xyz.txt and directory php.
# tar -rvf operavps.tar xyz.txt
# tar -rvf operavps.tar php
Untar Single file from tar.gz File
To extract a single file from the tar.gz archive file in Linux, use the following command as follows.
# tar -zxvf operavpsbackup.tar.gz operavpsbackup.xml
OR
# tar --extract --file=operavpsbackup.tar.gz operavpstbackup.xml
List Content tar.bz2 File
To list the content of the tar.bz2 archive file in Linux, type:
# tar -tvf Phpfiles-org.tar.bz2
Add Files/Directories to tar.gz and tar.bz2 Files
Since the tar command has no option to add files or directories to an existing compressed tar.gz and tar.bz2 archive file, the below error will be shown when you try to do this.
# tar -rvf operavideos.tar.gz xyz.txt
# tar -rvf Phpfiles-org.tar.bz2 xyz.txt
Untar Single file from tar.bz2 File
Ta command in Linux is also used to untar a single file from tar.bz2 File. To do this, run:
# tar -jxvf Phpfiles-org.tar.bz2 home/php/index.php
OR
# tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php
The above commands extract index.php from the file Phpfiles-org.tar.bz2.
View a tar File
You can use the following command to let all the tar files and folders in Linux be splayed in the terminal:
$ tar -tf operavps.tar.gz
Verify tar, tar.gz, and tar.bz2 File
To verify any tar or compressed archived file in Linux, run the command below and consider that you cannot do verification on a compressed archive file.
# tar tvfW operavps.tar
Best Method to Extract Files Without Extracting Directories
Use the --strip-components
option if you don’t want the directory structure in the tar file to be replicated on your hard disk. A numerical parameter is necessary for the --strip-components
option. The number indicates how many levels of directories should be disregarded. Although the directory structure is not mirrored on your hard drive, files from the disregarded directories are still extracted.
With our example tar file, the top-most directory for ukulele songs is not produced on the hard disk if we provide --strip-components=1
. The target directory extracts the files and directories that would have been extracted from that directory.
tar -xvzf ukulele_songs.tar.gz --strip-components=1
In our example tar file, there are only two layers of directory nesting. As a result, no additional folders are produced when --strip-components=2
is used; all files are extracted into the target directory.
tar -xvzf ukulele_songs.tar.gz --strip-components=2
How to back up in Linux Using tar command?
You can also use the tar command to back up to non-tape devices which means the below examples assume that you do not have a tape drive in your system. So, if you have a tape drive, you just need to replace the filename with your tape device file.
#tar -cf <filename_to_be_created> <filename_to_be backed_up>
Use the -t (table of contents) option along with the -f option, as illustrated below, to view the contents of a tar ball.
#tar -tf <filename>
Listing the contents of the tar ball in detail is something you frequently wish to see. To get more details, use the -v (verbose) option, as seen in this command.
#tar -tvf <filename>
Use the -x (extract) option along with the -f option, as indicated in the following command, to extract the whole contents of the tar ball into the current directory.
#tar -xf <filename>
You can specify a file type at the end of the command if you only want to extract that kind of file. In the example that follows, we have made it clear that I only need to extract the tar ball’s “.doc” files.
#tar xvf <filename> –-wildcards ‘*.doc’
FAQ
2. How do I create an archive using the tar command?
To create an archive, use the tar command followed by the "-c" option for creation and the "-f" option to specify the filename. For example, "tar -cf archive.tar /path/to/directory" will create an archive of the specified directory.
3. What is the difference between tar and gzip or bzip2?
The tar command bundles multiple files and directories into a single archive file, while gzip and bzip2 are compression tools that reduce the size of files or archives. Frequently, tar is combined with gzip or bzip2 for compressed archives, resulting in files with ".tar.gz" or ".tar.bz2" extensions.
4. How can I extract files from a tar archive?
To extract files, use the tar command with the "-x" option for extraction and the "-f" option to specify the archive file. For example, "tar -xf archive.tar" will extract the contents of the archive to the current directory.
5. Can the tar command compress files during the archiving process?
Yes, the tar command supports compression during archive creation by including options such as "-z" for gzip or "-j" for bzip2. For example, "tar -czf archive.tar.gz /path/to/directory" creates a compressed archive using gzip.
6. How do I view the contents of a tar archive without extracting it?
To view the contents, use the tar command with the "-t" option along with "-f" to specify the archive. For example, tar "-tf archive.tar" will list all files and directories contained in the archive.
7. Is it possible to extract specific files from a tar archive?
Yes, you can extract specific files or directories by specifying their paths after the archive name. For example, "tar -xf archive.tar file.txt" will extract only file.txt from the archive.
8. How do I add files to an existing tar archive?
While tar does not support direct modification of existing archives, you can append files using the "-r" option. For example, "tar -rf archive.tar newfile.txt" appends newfile.txt to archive.tar. Note that this works only with uncompressed archives.
9. What are some common errors when using the tar command and how can they be resolved?
Common errors include permission issues, incorrect file paths, or unsupported options. Ensure you have appropriate permissions, verify the accuracy of command syntax, and consult the tar manual with man tar for additional guidance on option usage.
10. How can I ensure the integrity of a tar archive?
To verify the integrity of a tar archive, you can test extraction using the "-W" option for verification. For example, "tar -xf archive.tar --verify" will confirm that files in the archive match the extracted files, helping prevent data corruption during archiving and extraction processes.
Conclusion
The tar command remains a crucial asset for developers working on Linux systems by providing versatile file packaging, extraction, and compression functionalities.
Adhering to these step by step procedures and observing recommended best practices ensures robust data management and operational accuracy.
Mastery of the tar command underpins effective development workflows and secure system administration.
How to uncompress tar archive and create a tar file?
By running the below command, you can create a tar file called file.tar which is the archive of all .c files.
$ tar cvf file.tar *.c
How to compress gzip?
You can use -z option and create a tar file:
$ tar cvzf file.tar.gz *.c
what is tar jxvf used for?
You can use jxvf to extract a single file from .tar.bz2 or extract a group of files as you reviewed the related example in the article.
Is utar xz possible on macOS?
Yes. The tar utility is pre-installed on macOS. The archive file name comes after the -f option when using the tar command with the --extract (-x) option:
tar -xf archive.tar.xz
How to use tar xvf command to extract files from archives.
Run the following command:
$ tar xvf file.tar