As not everything comes with a manual, or many times it is difficult for us to find documentation for some users, especially if it is a new operating system, such is the case of Linux. If you know how to use it, the terminal is a very powerful tool for any user, and you don't necessarily need to be an expert, we just need to know some commands or the more basics, such as the commands to compress and decompress entire directories including all the files and folders that are within these.
This applies to any Linux distribution, it could be Debian, Ubuntu, CentOS, etc.
Compress an entire directory
The most basic way to compress is by using the command:
tar -zcvf file-2080-04-01.tar.gz /home/user/folder
We can also use the following command, which is exactly the same as the previous one, only using the full parameters:
tar --gzip --create --verbose --file=file-2080-04-01.tar.gz /home/user/folder
The resulting file will be compressed and located in the directory in which we are, for example, if we enter with the root user from the terminal, and immediately execute the previous command, the file file-2080-04-01.tar.gz will remain in the directory /root, which is the default directory in which we are when accessing as root user.
The parameters of the command used are:
- z, --gzip: use the gzip tool to compress.
- c, --create: create a new file, in this case the tar container, which by default has no compression.
- v, --verbose: it is an option that makes the whole process of the command be shown in more detail.
- f, --file=: indicates that the next parameter used will be the output file, in this case the file is tar.gz.
In order to compress a directory and choose the location, we have two options:
1. The first is to navigate with the command cd
towards the directory in which we want the file to be created.
cd /backups/
tar -zcvf file-2080-04-01.tar.gz /home/user/folder
2. The second option is to change the path of the file, placing the command in the following way, using absolute paths at the origin and destination:
tar -zcvf /home/user/file-2080-04-01.tar.gz /home/user/file
Decompress a file .tar.gz
In the current directory.
We use the following command, from the directory where the file is located .tar.gz:
tar -zxvf file-2080-04-01.tar.gz
Alternative with named parameters:
tar --gunzip --extract --verbose --file=file-2080-04-01.tar.gz
In this command with parameters with names gunzip
is the tool in charge of filtering the content and --extract
indicates that the files will be extracted from the file found in --file=
, and --versbose
is the level of detail that the command gives us.
On a custom route.
To choose a custom path where the files are going to be decompressed, we add the parameter: -C
tar -zxvf archivo-2080-04-01.tar.gz -C /tmp/folder
Although we can also navigate to the destination directory, and execute, for example:
tar -zxvf /home/user/file-2080-04-01.tar.gz
With this, all the contents of the archive will be decompressed in the current directory, regardless of whether the .tar.gz
file is elsewhere.
Source: https://www.cyberciti.biz/faq/how-do-i-compress-a-whole-linux-or-unix-directory/