Using the GUI zip tools are easy and user friendly, but if you want some more advanced options with better compression you can turn to the command line to make a tar and gzip archive. The syntax will be the same in Mac OS X as it is in Linux. I ran tar -tvf and list is HUGE, there is no chance of just extracting the deleted folders/files so it has to be a complete var folder restore. And now several features of the server are missing/do not work. I have a backup and just want to know if I'm going the right way to restore the folder. Chemal Posts: 572. CentOS 5 died.
The tar command on Linux is often used to create .tar.gz or .tgz archive files, also called “tarballs.” This command has a large number of options, but you just need to remember a few letters to quickly create archives with tar. The tar command can extract the resulting archives, too.
The GNU tar command included with Linux distributions has integrated compression. It can create a .tar archive and then compress it with gzip or bzip2 compression in a single command. That’s why the resulting file is a .tar.gz file or .tar.bz2 file.
Compress an Entire Directory or a Single File
Use the following command to compress an entire directory or a single file on Linux. It’ll also compress every other directory inside a directory you specify–in other words, it works recursively.
Here’s what those switches actually mean:
- -c: Create an archive.
- -z: Compress the archive with gzip.
- -v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful.
- -f: Allows you to specify the filename of the archive.
Let’s say you have a directory named “stuff” in the current directory and you want to save it to a file named archive.tar.gz. You’d run the following command:
Or, let’s say there’s a directory at /usr/local/something on the current system and you want to compress it to a file named archive.tar.gz. You’d run the following command:
Compress Multiple Directories or Files at Once
RELATED:How to Manage Files from the Linux Terminal: 11 Commands You Need to Know
While tar is frequently used to compress a single directory, you could also use it to compress multiple directories, multiple individual files, or both. Just provide a list of files or directories instead of a single one. For example, let’s say you want to compress the /home/ubuntu/Downloads directory, the /usr/local/stuff directory, and the /home/ubuntu/Documents/notes.txt file. You’d just run the following command:
Just list as many directories or files as you want to back up.
Exclude Directories and Files
In some cases, you may wish to compress an entire directory, but not include certain files and directories. You can do so by appending an
--exclude
switch for each directory or file you want to exclude.For example, let’s say you want to compress /home/ubuntu, but you don’t want to compress the /home/ubuntu/Downloads and /home/ubuntu/.cache directories. Here’s how you’d do it:
The
--exclude
switch is very powerful. It doesn’t take names of directories and files–it actually accepts patterns. There’s a lot more you can do with it. For example, you could archive an entire directory and exclude all .mp4 files with the following command:Use bzip2 Compression Instead
While gzip compression is most frequently used to create .tar.gz or .tgz files, tar also supports bzip2 compression. This allows you to create bzip2-compressed files, often named .tar.bz2, .tar.bz, or .tbz files. To do so, just replace the -z for gzip in the commands here with a -j for bzip2.
Gzip is faster, but it generally compresses a bit less, so you get a somewhat larger file. Bzip2 is slower, but it compresses a bit more, so you get a somewhat smaller file. Gzip is also more common, with some stripped-down Linux systems including gzip support by default, but not bzip2 support. In general, though, gzip and bzip2 are practically the same thing and both will work similarly.
For example, instead of the first example we provided for compressing the stuff directory, you’d run the following command:
Extract an Archive
Once you have an archive, you can extract it with the tar command. The following command will extract the contents of archive.tar.gz to the current directory.
It’s the same as the archive creation command we used above, except the
-x
switch replaces the -c
switch. This specifies you want to extract an archive instead of create one.You may want to extract the contents of the archive to a specific directory. You can do so by appending the
-C
switch to the end of the command. For example, the following command will extract the contents of the archive.tar.gz file to the /tmp directory.If the file is a bzip2-compressed file, replace the “z” in the above commands with a “j”.
This is the simplest possible usage of the tar command. The command includes a large number of additional options, so we can’t possibly list them all here. For more information. run the info tar command at the shell to view the tar command’s detailed information page. Press the q key to quit the information page when you’re done. You can also read tar’s manual online.
If you’re using a graphical Linux desktop, you could also use the file-compression utility or file manager included with your desktop to create or extract .tar files. On Windows, you can extract and create .tar archives with the free 7-Zip utility.
READ NEXT- › What Does “FWIW” Mean, and How Do You Use It?
- › How to Automatically Delete Your YouTube History
- › What Is “Mixed Content,” and Why Is Chrome Blocking It?
- › How to Manage Multiple Mailboxes in Outlook
- › How to Move Your Linux home Directory to Another Drive
Tar (Tape Archive) is a popular file archiving format in Linux. It can be used together with gzip (tar.gz) or bzip2 (tar.bz2) for compression. It is the most widely used command line utility to create compressed archive files (packages, source code, databases and so much more) that can be transferred easily from machine to another or over a network.
Read Also: 18 Tar Command Examples in Linux
In this article, we will show you how to download tar archives using two well known command line downloaders – wget or cURL and extract them with one single command.
How to Download and Extract File Using Wget Command
The example below shows how to download, unpack the latest GeoLite2 Country databases (use by the GeoIP Nginx module) in the current directory.
The wget option
-O
specifies a file to which the documents is written, and here we use -
, meaning it will written to standard output and piped to tar and the tar flag -x
enables extraction of archive files and -z
decompresses, compressed archive files created by gzip.To extract tar files to specific directory, /etc/nginx/ in this case, include use the
-C
flag as follows.Note: If extracting files to particular directory that requires root permissions, use the sudo command to run tar.
Download and Extract File to Directory
Alternatively, you can use the following command, here, the archive file will be downloaded on your system before you can extract it.
To extract compressed archive file to a specific directory, use the following command.
How to Download and Extract File Using cURL Command
Considering the previous example, this is how you can use cURL to download and unpack archives in the current working directory.
To extract file to different directory while downloading, use the following command.
That’s all! In this short but useful guide, we showed you how to download and extract archive files in one single command. If you have any queries, use the comment section below to reach us.