Do you want to download multiple files from a Linux server? Or maybe you want to store the logs to comply with regulations?

In either case, you should compress the files into a RAR archive. In this post, we will show you how to create and extract RAR archives in Linux.

But before we get started, let’s quickly understand what a RAR archive is.

What are RAR Files?

RAR is an acronym for Roshal Archive, which is a type of compressed file format used to reduce the size of files or groups of files. The RAR format offers several advantages over uncompressed files:

  1. Reduced Storage Space: Compressing files into a RAR archive can significantly decrease the amount of disk space they occupy.
  2. Faster Transfer Speeds: Smaller file sizes mean quicker upload and download times, especially over the internet.
  3. Improved Data Management: It’s easier to manage and organize a single RAR file than multiple smaller files.
  4. Data Integrity: RAR files can include error checking and recovery records to repair minor corruptions.
  5. Security: RAR files can be encrypted with passwords, providing an additional layer of security for sensitive data.

It’s clear that RAR files can be pretty useful, so now let’s see how to use them via command line on Linux servers.

Installing RAR Tools

Before you can work with RAR files in Linux, you need to install the necessary tools. Here’s how you can do it:

Step 1: Update System Repositories

Connect to your server via SSH and enter the following command to update your system’s package list:

sudo apt-get update

This ensures that you have the latest information about available packages and their versions.

Step 2: Install unrar & rar Packages

To handle RAR files, you’ll need to install unrar for extracting RAR files, and rar for creating RAR files.

Use the following command to install necessary packages:

sudo apt-get install unrar rar -y

After installation, you can start using these command line utilities to manage RAR archives.

Working with RAR Files

With the necessary tools installed, you can now create and extract RAR files using the following steps:

Creating RAR Files

Step 1: Navigate to the Directory

Change to the directory containing the files you want to archive:

cd /path/to/directory

You can use the pwd command to check your current working directory and the ls command to see the list of all the files and directories present in the current directory.

Step 2: RAR File Creation Command

Once you are in the correct directory, use the rar command to create a new RAR archive:

rar a archive_name.rar file1 file2 file3

Replace archive_name.rar with your desired archive name, and file1 file2 file3 with the files you want to include. If you want to include all the files in current directory, then you can enter * instead of file names.

viewing rar files in linux via command line

In the above example, we can see that the rar utility created a new archive called myData.rar and added all the files listed that were specified in the command line arguments.

Extracting RAR Files

If you have downloaded a RAR file, then you can easily extract this file using the following steps:

Step 1: Locate the RAR File

Before we get started, make sure you know the exact path to the RAR file you wish to extract. You can either use the absolute path (from root) to the file, or the relative path (from the current location).

For example, in the above screenshot, we have an archive in the current directory. For this file, the relative path would be myData.rar and the absolute path would be /tmp/test123/myData.rar.

Step 2: Extraction Command

After you have noted the path to your archive, you can extract its contents using the following command:

unrar x archive_name.rar

Replace archive_name.rar with the path of your RAR file.

extracting rar files in linux via command line

In the above example we can see that we are working in a different directory (test234) and used the absolute path of the archive to extract it into the current working directory.

Step 3: Directory Structure Preservation

By default, unrar preserves the directory structure of the archived files. If you want to extract the files to a specific directory, you can specify the new path as a command line argument:

unrar x archive_name.rar /path/to/destination

Replace /path/to/destination with your desired directory path.

extracting rar archives

In the above example, we can see that the /tmp/test345 directory is empty in the beginning. We use the unrar command to extract data from the archive in the specified directory. After this, we use the ls command once again to check the contents of the /tmp/test345 and confirm whether the file was extracted successfully.

Advanced RAR Operations

Listing Contents

To view the contents of a RAR file without extracting them, you can use the following command:

unrar l archive_name.rar

This will list all the files and folders contained within the RAR archive.

Adding Files to an Archive

The process of adding files to an existing RAR archive is similar to creating a new archive. You can use the following command to add a file:

rar a archive_name.rar file_to_add

Replace file_to_add with the name of the file you wish to add to the archive_name.rar archive.

creating rar archive

Setting Passwords to RAR Archives

If you are working with sensitive data, you can create a password-protected RAR file. To do this, you can use the -p option when creating the archive:

rar a -p archive_name.rar file_to_archive

After entering this command, you’ll be prompted to enter and verify a password for the archive. Keep in mind that when you type your password, it will not be displayed on the screen.

Opening RAR Archives With A Password

The process of opening a password protected archive is similar to opening a simple file. Just use the following command to open the archive and enter your password when prompted:

unrar x archive_name.rar /path/to/destination

After typing your password, press ‘Enter’. If your archive has more than one file, you will be prompted to enter a password for each file – simply press the ‘a’ key on your keyboard to use the same password for all files.

Splitting RAR Archives

When working with extremely large files you sometimes need to split them into multiple parts. This is useful when you want to copy them over a network or store them in a smaller storage space.

To split a large RAR file into smaller parts, you can use the following command:

rar a -vsize archive_name.rar file_to_archive

Replace size with the maximum size for each split file (e.g., 10m for 10MB parts).

In the above example, we can see that the -v10m parameter was used when creating the archive. This made sure that all the archive files were smaller than 10MB. On the other hand, if we had created a single archive, it would have resulted in a much larger file size.

Conclusion

Throughout this guide, we’ve explored the vast capabilities of RAR files within the Linux ecosystem. From creating and extracting archives to advanced operations such as encryption and splitting files. We encourage you to discover new ways to use these utilities and streamline your workflow.

And if you’re looking to take your server management to the next level, consider using RunCloud.

RunCloud simplifies server management tasks, saves you time, and lets you focus on what’s truly important – building great applications.

With RunCloud, you can automate the tedious aspects of server management, ensuring that your Linux servers are running smoothly and efficiently.

Start your journey with RunCloud today and experience the ease of managing servers like never before!