How to archive files and folders and extract archives via SSH

On your HostKnox hosting account you can unpack different types of archives (e.g. zip, gzip, bzip2), and you can also compress content through SSH. There are several commands that can be used for that. A few of them that can do the job without any problems are tar, unzip, and zip.

Tar is the most convenient and widely used command for compiling, compressing and uncompressing contents. Using this command combined with several options you can pack files and folders into one compressed file, and you can unpack an archive. With this command you can manipulate one of the widely used archive formats: GZIP (with a file extension .tar.gz or .tgz). Let's say that you want to compress a folder and all its content into a single gzip archive. If this folder is in your current working directory you only need to execute the command tar cvfz followed by the name of the archive that you want to be created and then the name of the folder that's about to be compressed. In the options of that command c stands for create, v for verbose, f for file and z for gzip. For example:

tar cvfz example_archive.tar.gz exampledir

will compress the folder exampledir (with everything inside it) that's in your current working directory and will create a single file called example_archive.tar.gz. This archive will contain the folder with its content. If you want to compile and compress just several files, use the same command but instead of the source directory list the desired files separated from each other with a single space (e.g. tar cvfz example_archive.tar.gz file.txt file2.txt).

To do the reverse use the command tar xvfz followed by the name of the archive. The only difference with the previous command is that instead of c in the options you have to use x which stands for extract. For example:

tar xvfz example_archive.tar.gz

will uncompress the archive called example_archive.tar.gz that's in your current working directory. If you want to see what's inside the archive before unpacking it, you can use the command tar tf followed by the name of the archive (e.g. tar tf example_archive.tar.gz). With any of the commands, if the name of the archive or of the folder/file has spaces in its name, put quotation marks around it (e.g. tar cvfz "example archive.tar.gz" "example dir").

With the same command you can manipulate another popular archive format: BZIP2. Just change the z option (which stands for the gzip format) with j, so the commands become tar cvfj (e.g. tar cvfj example_archive.tar.bz2 exampledir) and tar xvfj (e.g. tar xvfj example_archive.tar.bz2) for compressing and uncompressing respectively.

You can uncompress files in the ZIP format using the command unzip, and respectively you can compress content into a zip file with the command zip. Keep in mind that the ZIP format compresses less compared to GZIP and BZIP2, so a file in the zip format containing the same contents will be larger in size. To unpack a zip archive just use the unzip command followed by the name of the file (including the .zip extension). For example:


will uncompress a zip file called that's in the current working directory.

Was this answer helpful?

 Print this Article

Also Read