
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
Linux tar Command
Introduction
The tar command in Linux is one of the most essential commands when it comes to file management. It is short for Tape Archive and is used to create and extract archive files. An archive file is a compressed file that contains one or more files bundled together for more accessible storage and portability. In this guide, we'll demonstrate, through examples, how to create, list, edit, and extract tar archive files, and cover some of the more commonly used tar command options.
Syntax of the tar command
The tar command accepts the following syntax −
$ $ tar [options][archive-file] [file or dir to be archived]
Let's look at some of the options provided with the tar command.
tar command options
The tar command provides the following options −
-c − This creates an archive file.
-x − The option extracts the archive file.
-f − Specifies the filename of the archive file.
-v − This prints verbose information for any tar operation on the terminal.
-t − This lists all the files inside an archive file.
-u − This archives a file and then adds it to an existing archive file.
-r − This updates a file or directory located inside a .tar file
-z − Creates a tar file using gzip compression
-j − Create an archive file using the bzip2 compression
-W − The -w option verifies an archive file.
Creating an archive file
The tar utility allows you to create archive files using various compression algorithms such as xz, gzip and bzip2. The accepted convention is to add the compression suffix to the compressed file.
For example, when using gzip compression (using the “-z” option), the file must have the suffix “.tar.gz”.
$ tar -czvf sales.tar.gz sales1.pdf sales2.pdf sales3.pdf
In this example, the command creates a file named “sales.tar.gz” from the three PDF files.
Similarly, when using bzip2 compression (using the “-j” option), the archive file must have the extension “.tar.bz2” as a suffix.
$ tar -cjvf sales.tar.bz2 sales1.pdf sales2.pdf sales3.pdf
Besides archiving files, you can also compress directories. For example, the following command creates a simple home directory tarball.
$ tar -cvf home.tar /home/james
List the contents of an archive file
The “-t” option can be used to list the contents of an archive file without extracting it.
$ tar -tf sales.tar.gz
This command lists all files within the “sales.tar.gz” archive.
Extracting an archive file
There are several ways to extract a compressed file using the tar command.
Extract to current directory
To extract an archive into the current working directory, use the “-x” option as shown below.
$ tar -xvf documents.tar.gz
In this example, we are unzipping or extracting the “documents.tar.gz” file, which contains three text files.
Extraction in separate directory
To extract an archive to a different directory, the ‘-C’ option is followed by the destination path, as shown in the following example.
$ tar -xvf documents.tar.gz -C /tmp/files
This command extracts the contents of the “documents.tar.gz” archive file into the /tmp/files directory.
Extract specific files from an archive
You can extract certain specified files by listing them one by one on the command line.
$ tar -xvf documents.tar.gz file1.txt file2.txt
In this example, we are extracting the files “file1.txt” and “file2.txt” from the “documents.tar.gz” archive.
Adding a File to a .tar Archive
To add or append a ‘.tar’ archive file, use the ‘-r’ option as shown.
$ tar -rvf files.tar file3.txt
In this example, we are adding the file3.txt file to the archives.tar file.
Deleting a file from a .tar file
To remove a file from a ‘.tar’ archive, use the --delete option as shown.
$ tar --delete -f archives.tar file3.txt
In this example, we are removing the “file3.txt” file from the archives.tar file.
Compress an archive
The tar command can also be used to compress an archive using gzip or bzip2 compression. To create a compressed file, the -z or -j option can be used in conjunction with the -c option.
$ tar -zcvf archive.tar.gz files_to_compress
This command creates a “.tar.gz” archive of the specified files.
$ tar -jcvf archive.tar.bz2 files_to_compress
This command creates a “.tar.bz2” archive of the specified files.
unzip a archive
To decompress a ‘.tar.gz’ file −
$ tar -zxvf archive.tar.gz
To decompress a ‘.tar.bz2’ file −
$ tar -jxvf archive.tar.bz2
Conclusion
The tar command is a versatile and powerful tool for managing files on Linux. It can be used to create, extract, and modify archives in a variety of formats and provides a wide range of options to suit different needs. By understanding the syntax and options of the tar command, you can easily create, manage, and extract archives in your workflows. With the knowledge of the tar command, you will be able to handle your file archives more efficiently and effectively.
- Related Articles
- How to Download and Extract Tar Files with One Command in Linux
- The best way to compress and extract files using the tar command on linux
- Find and tar Files on Linux
- Linux comm Command
- Linux watch Command
- Linux sort Command
- Linux source Command
- Linux ping Command
- Linux man Command
- Linux last Command
- Linux ps Command
- Linux tr Command
- Sudo Command in Linux
- The Linux join Command
- Free Command in Linux
