The tar command in Linux, short for Tape Archive, is a powerful and widely used tool for creating, viewing, and extracting files from archives. The tar command bundles multiple files into a single archive file, optionally compressing them using options like -z (gzip) or -j (bzip2), but they don't change the directory structures, which makes it ideal for backups or file distribution.
Syntax of `tar` command in Linux
tar [options] [archive-file] [file or directory to be archived]
tar: The command itself.[options]: Optional flags or settings that modify the behavior of the tar command.[archive-file]: The name of the archive file you are creating or working with.[file or directory to be archived]: The file or directory you want to include in the archive.
Examples of tar Command to Compress Files in Linux
1. Creating an uncompressed tar Archive using option -cvf
This tar command creates a tar file called file.tar which is the archive of all .c files in the current directory
tar cvf file.tar *.c
- '-c': Creates a new archive.
- '-v': Displays verbose output, showing the progress of the archiving process.
- '-f': Specifies the filename of the archive
Output :
os2.c
os3.c
os4.c
This Linux tar command extracts files from archives.
tar xvf file.tar
- '-x': Extracts files from an archive.
- '-v': Displays verbose output during the extraction process.
- '-f': Specifies the filename of the archive.
Output :
os2.c
os3.c
os4.c
3. gzip compression on the tar Archive, using option -z
This tar command in Linux creates a tar file called file.tar.gz which is the archive of .c files.
tar cvzf file.tar.gz *.c
- '-z': Uses gzip compression.
- '-j': Uses bzip2 compression.
- '-J': Uses xz compression.
This tar command extracts files from tar archived file.tar.gz files.
tar xvzf file.tar.gz
5. Creating compressed tar archive file in Linux using option -j
This Linux tar command compresses and creates archive files smaller than the size of gzip. Both compress and decompress take more time than gzip.
tar cvfj file.tar.tbz example.cpp
Output:
tar cvfj file.tar.tbz example.cpp
example.cpp
tar tvf file.tar.tbz
-rwxrwxrwx root/root 94 2017-09-17 02:47 example.cpp
6. Untar single tar file or specified directory in Linux:
This tar Linux command will untar a file in the current directory or in a specified directory using the -C option.
tar xvfj file.tar
or
tar xvfj file.tar -C path of file in directory
7. Untar multiple .tar, .tar.gz, .tar.tbz file in Linux:
This tar command in Linux will extract or untar multiple files from the .tar, .tar.gz, and .tar.tbz archive files. For example, the above command will extract “fileA” and “fileB” from the archive files.
tar xvf file.tar "fileA" "fileB"
or
tar zxvf file1.tar.gz "fileA" "fileB"
or
tar jxvf file2.tar.tbz "fileA" "fileB"
8. Check size of existing tar, tar.gz, tar.tbz file in Linux:
This tar command will display the size of an archive file in Kilobytes (KB).
tar czf file.tar | wc -c
or
tar czf file1.tar.gz | wc -c
or
tar czf file2.tar.tbz | wc -c
9. Update existing tar file in Linux
This Linux tar command updates an existing archive.
tar rvf file.tar *.c
Output:
os1.c
10. List the contents and specify the tarfile using option -tf
This tar command Linux will list the entire list of archived files. You can also list specific content in a tar file.
tar tf file.tar
Output:
example.cpp
11. Applying pipe to through 'grep command' to find what we are looking for
This tar command in Linux will list only the mentioned text or image in grep from an archived file.
tar tvf file.tar | grep "text to find"
or
tar tvf file.tar | grep "filename.file extension"
12. We can pass a file name as an argument to search a tarfile:
This tar Linux command views the archived files along with their details.
tar tvf file.tar filename
13. Viewing the Archive using option -tvf
This tar command displays the archive contents.
tar tvf file.tar
Output:
-rwxrwxrwx root/root 191 2017-09-17 02:20 os2.c
-rwxrwxrwx root/root 218 2017-09-17 02:20 os3.c
-rwxrwxrwx root/root 493 2017-09-17 02:20 os4.c
What are wildcards in Linux
Alternatively referred to as a 'wild character' or 'wildcard character', a wildcard is a symbol used to replace or represent one or more characters. Wildcards are typically either an asterisk (*), which represents one or more characters or question mark (?), which represents a single character.
This will extract only files with the extension .png from the archive file.tar. The -wildcards option tells tar to interpret wildcards in the name of the files to be extracted; the filename (*.png) is enclosed in single-quotes to protect the wildcard (*) from being expanded incorrectly by the shell.
tar tvf file.tar --wildcards '*.png'
Note: In the above commands " * " is used in place of file name to take all the files present in that particular directory.
An Archive file is a file that is composed of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compress files to use less storage space.
Options | Description |
|---|
-c | Creates an archive by bundling files and directories together. |
|---|
-x | Extracts files and directories from an existing archive. |
|---|
-f | Specifies the filename of the archive to be created or extracted. |
|---|
-t | Displays or lists the files and directories contained within an archive. |
|---|
-u | Archives and adds new files or directories to an existing archive. |
|---|
-v | Displays verbose information, providing detailed output during the archiving or extraction process. |
|---|
-A | Concatenates multiple archive files into a single archive. |
|---|
-z | Uses gzip compression when creating a tar file, resulting in a compressed archive with the '.tar.gz' extension. |
|---|
-j | Uses bzip2 compression when creating a tar file, resulting in a compressed archive with the '.tar.bz2' extension. |
|---|
-W | Verifies the integrity of an archive file, ensuring its contents are not corrupted. |
|---|
-r | Updates or adds files or directories to an already existing archive without recreating the entire archive. |
|---|
Zip Command to Compress Files in Linux
In Linux file compression, the `zip` wcommand emerges as a powerful and user-friendly tool. Distinct from the tar command, zip specializes in creating compressed files while preserving the integrity of the original content. The straightforward syntax of the zip command simplifies the compression process:
Basic Syntax:
zip [options] zipfile files/directories
[options]: Optional flags or settings for the zip command.zipfile: The name of the ZIP archive to be created.files/directories: The files or directories to be included in the ZIP archive.
zip ./bigfile.zip bigfile
This single line of code encapsulates the essence of the `zip` command, compressing the file `bigfile` and generating a new file, `bigfile.zip`, which holds the compressed version of the original content. Notably, the original file remains untouched, ensuring data safety during the compression process.
The user-friendly nature of the `zip` command makes it an accessible option for those seeking a straightforward solution for creating compressed archives. Whether you're compressing a single file or multiple files, the simplicity and efficiency of the` zip` command contribute to its popularity among Linux users. As a versatile tool in the compression toolkit, `zip` offers a seamless balance between ease of use and effective file compression.
Gzip Command to Compress Files in Linux
In the landscape of Linux compression utilities, the `gzip` command stands out as a straightforward and highly efficient tool. Its usage is elegantly simple—provide the filename, and `gzip` seamlessly compresses it in place. This can be achieved with a single command
Basic Syntax:
gzip [options] filename
[options]: Optional flags or settings you may want to apply.filename: The name of the file you wish to compress.
gzip bigfile
Unlike certain compression commands that generate a separate compressed file, `gzip` operates "in place." In other words, it encrypts the original file, replacing it with the compressed version. This intrinsic simplicity is a key attribute that positions `gzip` as an excellent choice for users seeking a rapid and effective file compression solution.
The in-place compression feature not only reduces the need for managing multiple files but also ensures a smooth integration into various workflows. This makes `gzip` particularly well-suited for scenarios where simplicity and speed are paramount. Whether you are compressing a single file or multiple files, the straightforward nature of the `gzip` command contributes to its popularity among Linux users, making it a reliable and efficient compression tool in diverse contexts.
Bzip2 Command to Compress Files in Linux
In Linux file compression, the `bzip2` command presents itself as a potent and versatile tool, bearing similarities to its counterpart, `gzip`. Functioning in a manner akin to `gzip`, the `bzip2` command compresses files in place, preserving only the original file. The simplicity of its application is illustrated by the following example:
Basic Syntax :
bzip2 [options] filename
Here, `options` represent any additional flags or settings you may want to apply, and `filename` is the name of the file you wish to compress. The basic command without options compresses the file in place, replacing the original file with the compressed version.
bzip2 bigfile
This concise command initiates the compression process, resulting in the creation of `bigfile.bz2`—the compressed iteration of the original file. Much like the decision between `gzip` and `bzip2` often depends on specific use cases, the choice between these two commands is nuanced and influenced by factors such as the nature of the data being compressed and the desired compression ratio.
While both commands share the fundamental concept of in-place compression, `bzip2` is known for its distinctive compression algorithm, which often achieves higher compression ratios compared to `gzip`. Consequently, users may opt for `bzip2` when prioritizing file size reduction and are willing to trade off some compression speed. This nuanced decision-making process underscores the flexibility of the `bzip2` command in catering to diverse compression requirements within the Linux ecosystem.
XZ Command to Compress Files in Linux
A relative newcomer to the compression scene, the `xz` command is recognized for its impressive compression capabilities. While it might take longer for large files, the compression results are noteworthy:
Basic syntax:
xz [options] filename
[options]: Optional flags or settings you may want to apply.filename: The name of the file you wish to compress.
xz bigfile
The `bigfile.xz` showcases the compressed version of the file. `xz` has gained popularity for its ability to achieve significant compression ratios, making it a top choice for those prioritizing file size reduction.
Explore
Linux/Unix Tutorial
5 min read
Getting Started with Linux
Installation with Linux
Linux Commands
Linux File System
Linux Kernel
Linux Networking Tools
Linux Process
Linux Firewall
Shell Scripting & Bash Scripting