How to use the command unar (with examples)

How to use the command unar (with examples)

The unar command is used to extract the contents from archive files. It is a versatile tool that can handle a variety of archive formats such as ZIP, RAR, Tar, and more.

Use case 1: Extract an archive to the current directory

Code:

unar archive

Motivation:

Extracting an archive to the current directory is useful when you want to unpack the contents without creating a new directory. This is convenient in situations where you only need to extract specific files from the archive.

Explanation:

  • archive: Specifies the path to the archive file that you want to extract. Replace “archive” with the actual filename and extension of the archive you want to unpack.

Example output:

When the unar archive command is executed, it will extract the contents of the “archive” file to the current directory. The extracted files will be placed in the current directory where the command was executed.

Use case 2: Extract an archive to the specified directory

Code:

unar -o path/to/directory archive

Motivation:

Sometimes, you may want to extract an archive to a specific directory to keep your files organized. This is particularly useful if you are working with multiple archive files and want to ensure that each file is extracted to its designated location.

Explanation:

  • -o path/to/directory: Specifies the path to the directory where you want to extract the archive. Replace “path/to/directory” with the actual directory path.

Example output:

When executing the unar -o path/to/directory archive command, the archive file will be extracted to the specified directory. The extracted files will be placed in the “path/to/directory” location.

Use case 3: Force overwrite if files to be unpacked already exist

Code:

unar -f archive

Motivation:

Sometimes, when extracting an archive, you may encounter a situation where some of the files to be unpacked already exist in the destination location. Using the -f option allows you to forcefully overwrite those files, ensuring that you have the latest version of the files from the archive.

Explanation:

  • -f: Forces overwrite if files already exist in the destination directory.

Example output:

When executing the unar -f archive command, the archive will be unpacked, and any files that already exist in the destination directory will be overwritten with the corresponding files from the archive.

Use case 4: Force rename if files to be unpacked already exist

Code:

unar -r archive

Motivation:

There may be situations where you want to keep the existing files in the destination directory and avoid overwriting them. The -r option allows you to force rename the files being extracted from the archive to avoid conflicts with the existing files.

Explanation:

  • -r: Forces rename if files already exist in the destination directory.

Example output:

When executing the unar -r archive command, the archive will be unpacked, and any files that already exist in the destination directory will be renamed by appending a number to the filename to avoid conflicts.

Use case 5: Force skip if files to be unpacked already exist

Code:

unar -s archive

Motivation:

If you want to skip the extraction of files from the archive that already exist in the destination directory, the -s option allows you to achieve that. This can be useful when you only want to extract new or updated files from the archive.

Explanation:

  • -s: Forces skip if files already exist in the destination directory.

Example output:

When executing the unar -s archive command, the archive will be unpacked, and any files that already exist in the destination directory will be skipped and not extracted from the archive.

Conclusion:

The unar command is a powerful tool for extracting files from archive formats. By using different options, you can control the behavior of the extraction process, including overwriting, renaming, or skipping files that already exist in the destination directory. This flexibility makes unar a versatile tool for managing and unpacking various types of archives.

Related Posts

How to use the command "aws-s3-mv" (with examples)

How to use the command "aws-s3-mv" (with examples)

The aws s3 mv command is used to move local files or S3 objects to another location either locally or in an S3 bucket.

Read More
How to use the command `launchctl` (with examples)

How to use the command `launchctl` (with examples)

launchctl is a command-line utility that allows control over Apple’s launchd manager for launch daemons (system-wide services) and launch agents (per-user programs).

Read More
Proxmox Virtual Environment: qm rollback (with examples)

Proxmox Virtual Environment: qm rollback (with examples)

Use Case 1: Rollback the state of a specific VM to a specified snapshot.

Read More