In this lecture, you'll learn how to archive and compress files and directories in Linux. 1
Tar is the most widely used command to create compressed archive files that can be moved easily from 2
one disk or machine to another. 3
This command has a large number of options, but you need to remember only a few of them so we can quickly 4
work with it. Tar is a complex tool used to both archive and compress files as well as extract the 5
resulting archives. 6
Before continuing, I want to make a distinction between two terms: archiving and compressing. Archiving 7
means that if you take 10 files of 100 kilobytes and archive them, the resulting single file is 1000 8
kilobytes. 9
On the other hand, if you compress those 10 files, you might find that the resulting file has a wide 10
range from only a few kilobytes to close to the original size, depending on the original file type. 11
In general, text files have a higher compression ratio. 12
Tar alone only archives the files, it does not compress them as well. But using specific options tar can 13
pass the files to the compression algorithm and the final result will be compressed, without using another 14
dedicated command for that. 15
Let's get practical and see the most used options. 16
The most important ones are c, which creates an archive x, which extracts the archives and t which 17
displays the contents of the archives or its table of contents. 18
In the next example, I'll compress an entire directory into a single file. 19
It will also compress every other directory inside the specified directory itself. 20
In other words, it works recursively. I'll run the command as root because I compress /etc 21
directory and there are files that can be read only as root. 22
tar -czvf 23
the name of the archive etc..tar.gz 24
and the path to the director /etc 25
I'm hitting enter! Now, you are seeing in the terminal the process of the command. This is what the 26
-V option, which means verbose does; this is optional, but it's helpful. If I run the command 27
again without the -v option it will run quietly without displaying anything on the screen. 28
I'm running it again without V. 29
It hasn't displayed its progress. 30
By the way, these archive files are called tarballs. 31
Let's take a closer look at each option. 32
- c means create an archive - z tells tar to also compress the archive using gzip or gnuzip compression. 33
This is the most used compression protocol on Linux. 34
By the way, that's why the resulting file has .tar.gz expansion, a double extension. 35
However sometimes a .tgz extension is used instead. 36
Note that the type of the file is not given by its extension. 37
It's only helpful to visually find out what kind a specific file is. In this case 38
it's an archive that was further compressed with gzip. 39
And f allows you to specify the filename of the archive. 40
In other Linux commands, it doesn't matter the order of the options, but in the case of tar it's 41
mandatory to specify the archive file name after the option f. 42
If I write the `-f` option bat the beginning I’ll get an error like this. 43
The archive was created in the current directory, but you can create it anywhere else by specifying 44
a path. 45
For example, I'm creating the archive in /tmp 46
And another useful compression algorithm is bzip2 which creates smaller files, but at the cost 47
of speed. 48
However, nowadays computers are so fast that most users won't notice much of a difference between the 49
between the times taken by gzip or bzip2 to compress a group of files. To bzip2 instead of gzip, simply 50
use the `j` option instead of `z`. 51
It's common to use .tar.bzip2 for such an archive ! 52
Tar is frequently used to compress a single directory, but you could also use it to compress multiple directories, 53
multiple individual files or both; just provide a list of files or directories instead of a single 54
one, like this: tar -cjvf 55
archive.tar.gz2 56
and the files, etc password etc group /var/ log /dmesg 57
I have archived and compressed 3 files, of course, I can add the directory as well. 58
There are some permission denied errora, so I ran the command again with sudo. 59
In some cases, it's useful to exclude some files from the archive. 60
Imagine that you make a back up of a directory and want to exclude the music, video or other non important 61
files. 62
To do that, use one or more exclude options like this: tar --exclude = and the files 63
to exclude, for example, a *.mkv; another one --exclude = .config and 64
--exclude =.cache 65
Now the options, the name of the archive, 66
and the name of the directory, the user's home directory / home /student; it will exclude 67
these files from the archive. 68
Perfect, once you have an archive, you can extract it with the same command, which is * 69
I'm going to extract the contents of an archive in the current working directory so tar -x 70
which comes from extract and the same options zvf and the name of the archive. 71
The archive was extracted in the current directory, this is its contents. 72
etc, the etc director. If you want to extract an archive into another directory use -C option 73
and the destination directory. I'm creating a new directory called my_backup and I’ll extract 74
the contents of the bzip2 archive in that directory: mkdir my_backup 75
tar -xjvf the name of the archive,etc.tar.bz2-C and the destination 76
directory using a correct path. 77
And it was extracted in that directory. 78
Let's check which algorithm has compressed better and sudo du -sh etc 79
OK. 80
Let's take a look at the archives, we notice that bzip2 is better. 81
1.1/1.3* 100, I did the Math and found out that bzip2 82
occupies only 85% of the space occupied by the gzip; and the size of the original 83
directorate is 12 megabytes. 84
So the compression ratio was 1.1/12 85
Approximately 90% Note that these two compression algorithms are incompatible; if you want to 86
extract a gzip archive using the -j option, which is the option for archives of type bzip2, you’ll 87
get an error. Specify the correct option, which is z for gzip and j for bzip, or allow tar 88
to auto-detect the compression type by specifying no option for the compression algorithm. 89
tar-xzvf and the bzip2 archive. 90
We cannot use z for archives of type bzip; specify g or no option at all. 91
To see the contents of an archive use the -t option, which comes from the table of contents. When 92
you want to check if a specific file is in the archive pipe the output of the previous command to grep. 93
I want to check that the configuration file of the ssh demand is in the archive so tar - t f 94
the name of the archive and | grep sshd_config and it's there 95
but the configuration file of the Apache Web server is not there; there's this directory 96
but it's not that the configuration file of the Web server. 97
The examples shown here in this lecture are extremely useful because the /etc directory contains the 98
configuration files of the entire system. In my opinion, it's mandatory to have a back up of this 99
directories so it can be restored if after an update or change, something goes wrong. 100
And I'll show you a trick. 101
If you want to have versions, which are backups made in different days, then you need to insert the 102
date into the back up filename. 103
For that, you need to do a command substitution like this:tar -cjvf, the name of the archive 104
etc- and now the trick $ 105
(date +%F).tar.bz2 106
and the name of the directory. 107
And I'll run it as root. 108
See how a new archive with the current date in the file name was created. 109
What the command did was substitute the date command with its output so date +%F 110
The 111
command was substituted with the output. 112
We'll dive deeper into this in the shell scripting section. 113
Lately, I have seen more and more files with the .xz extension. These are files compressed with the 114
LZMA algorithm and tar will autodetect the compression type and extract the archive. 115
Let me show you a quick example. 116
I'll download an archive of type xz from gnu.org 117
It's the core utils archive. 118
It contains all the basic Linux commands. 119
The archive is in the current directory and tar -xvf and the name of the archive, and it's 120
extracting the archive. 121
In most cases, you'll tar to compress files using different compression protocols. 122
However, note that there are also dedicated commands that only compress or extract files and directories 123
using gzip and bzip2. Gzip is used to compress a file or directory. 124
gunzip is used to extract a gzip archive 125
Then there's bzip2 and bunzip2