6.3.3 How to Archive and Extract Archives via SSH. Working with Archives over SSH

TheHost FTP Banner

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:

tar -argument archive_name_or_path target_file_or_directory

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 named file.tar.gz, , compression with gzip will be automatically applied to the archive.
  • -x – extract data from the archive.
Example: creating a .tar archive
tar -cfv /var/www/example.tar /var/www/examplesite
Example: extracting a .tar archive into the current directory
tar -xfv /var/www/example.tar
Example: extracting a .tar archive into a specific directory
tar -xfv /var/www/example.tar -С /var/www/examplesite
Example: creating a .tar.gz archive
tar -cfva /var/www/example.tar.gz /var/www/examplesite
Example: extracting a .tar.gz archive into the current directory
tar -xfva /var/www/example.tar.gz
Example: extracting a .tar.gz archive into a specific directory
tar -xfva /var/www/example.tar.gz -C /var/www/examplesite
Example: creating a .tar.bz2 archive
tar -cfva /var/www/example.tar.bz2 /var/www/examplesite
Example: extracting a .tar.bz2 archive into the current directory
tar -xfva /var/www/example.tar.bz2
Example: extracting a .tar.bz2 archive into a specific directory
tar -xfva /var/www/example.tar.bz2 -C /var/www/examplesite

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
sudo yum insatall zip unzip -y
Debian/Ubuntu
sudo apt-get install zip unzip -y
Example: creating a .zip archive
zip /var/www/example.zip /var/www/examplesite
Example: extracting data from a .zip archive
unzip /var/www/example.zip -d /var/www/examplesite

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
zip /var/www/example.zip /var/www/newfile1.txt /var/www/newfile2.txt
Example: viewing the contents of a .zip archive
unzip -l /var/www/example.zip
Example: extracting specific files from a .zip archive
unzip /var/www/example.zip file1.txt file2.txt -d /var/www/examplesite

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
sudo yum install unrar rar -y
Debian/Ubuntu
sudo apt-get install unrar rar -y
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:

rar -r /var/www/example.rar /var/www/examplesite
Example: extracting data from a .rar archive
unrar x /var/www/example.rar /var/www/examplesite

This is not an error; you specifically need the x argument for extraction. The -x argument allows specifying exceptions when executing the command.

Example: adding files to an existing .rar archive
rar a /var/www/example.rar /var/www/newfile1.txt /var/www/newfile2.txt
Example: viewing the contents of a .rar archive
unrar l /var/www/example.rar
Example: extracting specific files from a .rar archive
unrar e /var/www/example.rar file1.txt file2.txt /var/www/examplesite