6.4.3 How to Archive and Extract Archives via SSH. Working with Archives over SSH
Archives are an integral part of modern data handling, providing efficient compression of information and saving space on the file system. However, their significance goes beyond compactness – archives also serve as a convenient and reliable way to store information. In this article, we will explore key command-line tools for working with archives via SSH, the connection to which is necessary for any further actions.
Popular archive formats:
- TAR (
.tar
) – widely used for file archiving on Unix systems. The program with the same name allows working with various archive formats. External utilities, such as gzip, bzip2, lzip, and others, are used for compression. Additional information, such as owner information and directory structure, can be recorded when creating an archive. - GZ (
.tar.gz
,.tgz
,.tar.gzip
) – compresses data using the tar utility (for a single file) or the combination of gzip + tar (for multiple files). Its feature lies in the ability to work with a continuous stream of data, making GZ an optimal tool for compressing internet traffic. - BZIP2 (
.tar.bz2
,.tar.bzip2
,.tbz2
,.tb2
,.tbz
) – a free utility that provides high-quality data compression, mainly used on Unix platforms. Compared to GZ, it operates slower and requires more computational resources, but it ensures more efficient compression. - ZIP (
.zip
) – used on Windows operating systems and allows compressing files and directories using various algorithms. ZIP is supported by almost all operating systems, including Windows, Mac OS X, and Unix/Linux. It also allows creating a self-extracting archive in the form of an executable SFX file. - RAR (
.rar
) – used for archiving on Windows family operating systems. Similar to the ZIP format, RAR is supported by many operating systems.
Working with TAR, GZ, BZIP2 Archives
The general syntax of commands is as follows:
Key arguments:
-c
– create an archive.-f
– display the name or path to the archive.-t
– show files in the archive (applies to an already created archive).-v
– display processed files.-a
– compress or extract data from the archive with automatic selection of the utility (gzip, bzip, etc.), depending on the specified extension. For example, if the file is namedfile.tar.gz
, , compression with gzip will be automatically applied to the archive.-x
– extract data from the archive.
Example: creating a .tar
archive
Example: extracting a .tar
archive into the current directory
Example: extracting a .tar
archive into a specific directory
Example: creating a .tar.gz
archive
Example: extracting a .tar.gz
archive into the current directory
Example: extracting a .tar.gz
archive into a specific directory
Example: creating a .tar.bz2
archive
Example: extracting a .tar.bz2
archive into the current directory
Example: extracting a .tar.bz2
archive into a specific directory
Working with .zip Archives
To work with this type of archives, the zip and unzip programs will be used.
Note: On some distributions, these utilities may be absent, so it is necessary to install the corresponding packages before starting work.
CentOS/AlmaLinux/RockyLinux
Debian/Ubuntu
Example: creating a .zip
archive
Example: extracting data from a .zip
archive
If the -d
argument is not specified, the files will be extracted to the directory where the archive specified in the command is located.
Example: adding files to an existing .zip
archive
Example: viewing the contents of a .zip
archive
Example: extracting specific files from a .zip
archive
Working with .rar Archives
To work with .rar
archives, the rar and unrar utilities are required.
Note: On some distributions, these utilities may be absent, so it is necessary to install the corresponding packages before starting work.
CentOS/AlmaLinux/RockyLinux
Debian/Ubuntu
Example: creating a .rar
archive
To archive a directory completely (with all nested folders and files), you need to add the -r
key. The command will look like this:
Example: extracting data from a .rar
archive
This is not an error; you specifically need the x
argument for extraction. The -x
argument allows specifying exceptions when executing the command.