Linux or UNIX is a lot different than other graphical operating system like Windows. When it comes to Linux power, it’s combined command line it’s almost magic. You can combine more than one command into a single and powerful command who can do exactly what you want.
At this moment, we will talk about how to compress a whole Linux directory from command line.
It’s very easy to do this, very useful if you wish to backup your entire website, files, or compress a directory and send the link for download to someone else. GNU tar command create the archive and compress it for you very easy.
To compress an entire directory under Linux or UNIX type the following command:
[box] tar -zcvf desired-archive-name.tar.gz desired-directory-name[/box]
And I will try to explain what “-zcvf” means:
- z: It will compress the archive using a gzip program.
- c: It will create the archive.
- v: Verbose mode, display the entire progress while compressing the directory.
- f: Archive file name
If you have a directory called blog with path /var/www/blog and you want to backup the entire files from this directory (creating an archive) you can use the following command:
[box]$ tar -zcvf blog-backup-2017.tar.gz /var/www/blog[/box]
This command will create the archive called blog-backup-2017.tar.gz in the current path. When the file is moved to another machine to decompress you have to use the following command which will extract the files in the current path also:
[box]$ tar -zxvf blog-backup-2017.tar.gz[/box]
The “-x” will be used to extract the files from the given archive.
But how would you do if you wish to extract the files in a specific directory ? For example, if you wish to extract the files in a directory path /new ? Well, you are going to use the following command:
[box]$ tar -zxvf blog-backup-2017.tar.gz -C /new[/box]
[box]$ cd /new[/box]
[box]$ ls -la [/box]