Polecenia Linuksa

How to Use Linux Tar Command

How to Use Linux Tar Command
The Linux tar command is used to combine multiple files into a single file also known as archiving. It is also used to compress files to reduce the disk space required to store these files and to make it easy to share multiple files over the internet. The tar utility can also be used to decompress a compressed file to recover the original data.

The tar command comes preinstalled in almost every Linux distribution out there. So, it's ready when you need it.

In this article, I am going to show you how to use the Linux tar command to compress files and decompress compressed files. So, let's get started.

Creating tar Archives:

I have a directory ~/projects in my home directory. I have the following files and directories in the ~/projects directory. I will use these files and directories to demonstrate how to make archive files with the tar command in this article.

To create a tar archive of everything in the ~/project directory, run the tar command as follows:

$ tar cvf project.tar project

The archive project.tar should be created.

As you can see, the archive file project.tar is created. It is 51 MB in size.

By default, the tar archive is not compressed. But, if you want you can compress the contents of the archive using gzip and bzip2 algorithm.

To perform gzip compression in the earlier example, you have to use the -z option of the tar command as follows:

$ tar xvzf project.tar.gz poject/

project.tar.gz archive should be created. As you can see, the file size is slightly smaller than the uncompressed version. In real life scenario, you will get better results because I generated these files using the /dev/urandom and dd commands. So, the compression algorithms didn't work that well.

To perform bzip2 compression in the earlier example, you have to use the -z option of the tar command as follows:

$ tar cvjf project.tar.bzip2 project/

As you can see, the project.tar.bzip2 archive is created.

Compressing Specific Files and Directories:

You don't have to compress a directory if you don't want to. You can specify different files and directories in different path (relative or absolute) in the tar command and compress them as follows:

$ tar cvzf important_etc.tar.gz /etc/virc /etc/fstab project/test1.txt project/docs

The specified files and directories are compressed into an archive file is important_etc.tar.gz.

Excluding Files and Directories:

When you need to compress an entire directory with the tar command and you don't want to include some files and directories within, you can use the -exclude option of the tar command as follows:

$ tar cvzf project.tar.gz --exclude=project/docs --exclude=project/test.img project/

As you can see, the test.img file and docs/ directory including its contents are excluded from the archive.

Listing the Contents of a tar Archive:

Before you extract a tar archive, it's always a good idea to know the file and directory structure of a tar archive. You can list all the files and directories inside of a tar archive with the following command:

$ tar tf project.tar

As you can see, the file and directory structure of the tar archive is printed.

To see the file and directory permissions and other information about the files and directories inside a tar archive, run the tar command as follows:

$ tar tvf project.tar

As you can see, the contents of the tar archive and a lot of information about each files and directories are listed.

Extracting tar Archives:

To extract a tar archive, you have to know whether the archive is compressed or not. If the archive is compressed, then you have to know what compression algorithm is used to compress the archive as well.

Usually, you find this information from the archive filename. If the archive filename ends with .tar, then by convention this is a tar archive and is not compressed.

If the filename of the archive ends with .tar.gz, then it's a gzip compressed archive.

If the filename of the archive ends with .tar.bzip2, then it's a bzip2 compressed archive.

Still, people can use any file extension they want to represent the tar archive file. Nothing is stopping them. So, a better way is to use the file command.

To find information about an archive (let's say project2.tar), run the file command as follows:

$ file project2.tar

As you can see, even though the file extension is not correctly set, the file command still says it's a gzip compressed archive.

Now, to extract the non-compressed tar archive project.tar you just created on your current working directory, run the following command:

$ tar xvf project.tar

This command will extract the archive in your current working directory.

If you want to extract the archive to some other directory, let's say ~/Downloads, then run the tar command as follows:

$ tar xvf project.tar -C ~/Downloads

NOTE: The directory you're extracting the archive must exist before you run the command. If it does not, tar will not be able to extract the archive. So, make sure the directory exists and if it does not, create the directory with the mkdir command.

The archive project.tar is extracted into the ~/Downloads directory.

As you can see, the contents of the archive are now available in the ~/Downloads directory.

If the archive is gzip compressed, then use the -z option when you extract the archive as follows.

$ tar xvzf project.tar -C ~/Downloads

If the archive is bzip2 compressed, then use the -j option when you extract the archive as follows.

$ tar xvjf project.tar -C ~/Downloads

Getting Help:

The tar command has a lot of options. It's not possible to cover every one of them in this article. But, you can read the manpage of the tar command to learn more about it. I've shown you how to get started with the tar command in this article. Now, you should be able to move forward on your own.

To open the manpage of the tar command, run the following command:

$ man tar

So, that's how you use the tar command in Linux. Thanks for reading this article.

Gry Bitwa o Wesnoth 1.13.6 Wydanie rozwojowe
Bitwa o Wesnoth 1.13.6 Wydanie rozwojowe
Bitwa o Wesnoth 1.13.6 wydana w zeszłym miesiącu jest szóstą wersją rozwojową w 1.13.Seria x i zapewnia szereg ulepszeń, w szczególności w interfejsie...
Gry Jak zainstalować League Of Legends na Ubuntu 14.04
Jak zainstalować League Of Legends na Ubuntu 14.04
Jeśli jesteś fanem League of Legends, to jest okazja do przetestowania League of Legends. Pamiętaj, że LOL jest obsługiwany w PlayOnLinux, jeśli jeste...
Gry Zainstaluj najnowszą grę strategiczną OpenRA na Ubuntu Linux
Zainstaluj najnowszą grę strategiczną OpenRA na Ubuntu Linux
OpenRA to darmowy silnik gier strategicznych czasu rzeczywistego, który odtwarza wczesne gry Westwood, takie jak klasyczny Command & Conquer: Red Aler...