While on Windows, you'd unzip RAR, ZIP, ISO files to access data using software like WinRAR, 7 Zip, etc., on Linux, you'll extract files with Tar format. The GNU tar command is included in Linux distributions, integrated for extraction. This command can create .tar archive files, then compress them with gzip or bzip2 in a single command. That's why you see files like .tar.gz or .tar.bz2. Refer to how to extract Tar files on Linux in the article below by Mytour.
Compress entire directories or individual files
Utilize the command below to compress an entire directory or a file on Linux. The command also compresses all other directories within the specified directory, meaning the command operates recursively:
tar -czvf name-of-archive.tar.gz /path/to/directory-or-file
Below are the meanings of the command parts:
-c: Create a new archive.
-z: Compress the archive with gzip.
-v: Display progress in the Terminal during archive creation, also known as 'verbose' mode. v is always an option in these commands, but it's genuinely useful.
-f: Allow you to specify the archive file name.
Imagine you have a directory named stuff in the current directory, and you want to save the directory to a file named archive.tar.gz. All you need to do is run the command below:
tar -czvf archive.tar.gz stuff
Alternatively, suppose there's a directory within /usr/local/something on the system, and you want to compress that directory into a file named archive.tar.gz, run the command below:
tar -czvf archive.tar.gz /usr/local/something
Compress multiple directories or files at once
Typically, the tar command is used to compress a single directory, but you can also use it to compress multiple directories, individual files, or both. Just provide a list of files or directories instead of a single file or directory.
For instance, suppose you want to compress the directory /home/ubuntu/Downloads, /usr/local/stuff, and the file /home/ubuntu/Documents/notes.txt. Just run the command below:
tar -czvf archive.tar.gz /home/ubuntu/Downloads /usr/local/stuff /home/ubuntu/Documents/notes.txt
Simply list the directories or files similar to how you want to back up the directories, files.
Exclude specific directories or files
In case you want to compress an entire directory but exclude specific files and directories within it. You can achieve this by appending --exclude to each directory or file you want to exclude.
For example, suppose you want to compress the directory /home/ubuntu, but you don't want to compress the directories /home/ubuntu/Downloads and /home/ubuntu/.cache. Just run the command below:
tar -czvf archive.tar.gz /home/ubuntu --exclude=/home/ubuntu/Downloads --exclude=/home/ubuntu/.cache
The --exclude command comes in quite handy. It doesn't take the names of directories and files. You can use the command to perform various other tasks. For instance, you can archive an entire directory and exclude all .mp4 files using the command below:
tar -czvf archive.tar.gz /home/ubuntu --exclude=*.mp4
Using Bzip2 compression instead
Although gzip compression is commonly used to create .tar.gz or .tgz files, the tar command also supports Bzip2 compression. This allows you to create bzip2 compressed files, often referred to as .tar.bz2, .tar.bz, or .tbz files. To do this, simply replace -z for gzip in the above commands with -j for bzip2.
Gzip is faster but compresses less, so your compressed files are larger. Bzip2 compresses slower but deeper, resulting in smaller compressed files. Gzip is also more popular, with some systems supporting gzip by default but not bzip2. However, in general, both gzip and bzip2 function similarly.
For instance, instead of using the first command provided above to compress the directory stuff, you run the command below:
tar -cjvf archive.tar.bz2 stuff
File Extraction
Once you have a compressed file, you can use the tar command to extract it. The command below will extract the contents of the file archive.tar.gz into the current directory:
tar -xzvf archive.tar.gz
Similar to the command used above for compressing files, except replacing -x with -c, allowing you to extract archive files.
If you want to extract a file into a specific directory, you can do this by appending -c to the end of the command. For example, the command below will extract the file archive.tar.gz into the /tmp directory:
tar -xzvf archive.tar.gz -C /tmp
If the file is compressed with bzip2, replace “z” with “j” in the commands above.
Another way to extract Tar files on Linux
Follow the steps below to extract Tar files on Linux:
Step 1: Open Terminal, you can perform some quick Terminal access methods to open this window faster.
Step 2: Enter tar into the Terminal window.
Step 3: Type a space.
Step 4: Input -x into it.
Step 5: If the tar file is compressed with gzip (with the extension .tar.gz or .tgz), input z into it.
Step 6:Enter f.
Step 7: Type a space.
Step 8: Input the name of the file you want to extract.
Step 9: Press Enter.
Although the tar command offers various additional options, Mytour only introduces you to some basic features for compressing and extracting Tar files. For more detailed information about the Tar command, run the command info tar. Press the q key to exit the information page when finished, or alternatively, you can refer to the online tar command usage guide.
The Mytour article just guided you on how to extract Tar files on Linux. Additionally, if your computer is running in a Desktop Linux graphical environment, you can also use file compression utilities or file managers to create or extract .tar files. On Windows, you can compress and extract files using utilities like 7 Zip, WinRAR, which are free.