How to use the command '7z' (with examples)

How to use the command '7z' (with examples)

7z is a versatile file archiver utility known for its high compression ratio, making it a favored tool for managing, securing, and compressing files into archives. The command supports multiple formats, strong encryptions, and a range of operations for file compression and extraction. Here, we present various use cases illustrating the practical applications of 7z with examples to highlight its capabilities.

Use case 1: Add a file or directory to a new or existing archive

Code:

7z a path/to/archive.7z path/to/file_or_directory

Motivation: Compressing files or folders can significantly reduce their size, making them easier to store and share. When managing large collections of files, combining them into a single archive can improve organization. This command adds the specified file or directory to an existing archive or creates a new one if it doesn’t exist.

Explanation:

  • 7z: Initiates the 7z command line interface.
  • a: Stands for “add”, indicating the intention to add files or directories to an archive.
  • path/to/archive.7z: Specifies the path and name of the archive file. If the archive does not already exist, it will be created.
  • path/to/file_or_directory: Indicates the path to the file or directory that you wish to archive.

Example Output:

  Adding    path/to/file_or_directory
  Everything is Ok

Use case 2: Encrypt an existing archive (including filenames)

Code:

7z a path/to/encrypted.7z -ppassword -mhe=on path/to/archive.7z

Motivation: Data security is paramount, especially when transferring files over the internet or sharing sensitive information. Encrypting archives ensures that only individuals with the correct password can access the contents, including the file names.

Explanation:

  • 7z: Initiates the 7z command line interface.
  • a: Indicates the addition or modification of an archive.
  • path/to/encrypted.7z: Defines the path and name for the newly encrypted archive.
  • -ppassword: Sets the password for the encryption. Make sure to choose a strong password to protect your data.
  • -mhe=on: Enables header encryption, which encrypts the archive filenames as well as the contents.
  • path/to/archive.7z: Specifies the existing archive to be encrypted.

Example Output:

  Adding    path/to/archive.7z
  Everything is Ok

Use case 3: Extract an archive preserving the original directory structure

Code:

7z x path/to/archive.7z

Motivation: When extracting files from an archive, preserving the original directory structure can be crucial to maintaining the organizational hierarchy and ensuring that interrelated files remain properly sorted.

Explanation:

  • 7z: Initiates the 7z command line interface.
  • x: Stands for “extract”, which extracts the contents of the archive.
  • path/to/archive.7z: Specifies the archive file from which contents should be extracted while maintaining its directory structure.

Example Output:

  Extracting  file1
  Extracting  dir1/file2
  Extracting  dir1/dir2/file3
  Everything is Ok

Use case 4: Extract an archive to a specific directory

Code:

7z x path/to/archive.7z -opath/to/output

Motivation: Direct extraction to a specific directory provides flexibility in organizing extracted files, allowing users to directly manage where files are placed and potentially preventing clutter in the working directory.

Explanation:

  • 7z: Initiates the 7z command line interface.
  • x: Stands for “extract”, which extracts the contents of the archive.
  • path/to/archive.7z: Specifies the source archive file.
  • -opath/to/output: Determines the output directory for the extracted files.

Example Output:

  Extracting  file1
  Extracting  dir1/file2
  Extracting  dir1/dir2/file3
  Everything is Ok

Use case 5: Extract an archive to stdout

Code:

7z x path/to/archive.7z -so

Motivation: Extracting directly to the standard output (stdout) can be useful for piping data directly to another command line utility for further processing or viewing without creating intermediate files.

Explanation:

  • 7z: Initiates the 7z command line interface.
  • x: Stands for “extract”, which extracts the contents of the archive.
  • path/to/archive.7z: Indicates the archive file to be extracted.
  • -so: Directs the extracted data to standard output, allowing further manipulation or direct inspection.

Example Output:

(Contents directly printed to the terminal)

Use case 6: Archive using a specific archive type

Code:

7z a -t7z|bzip2|gzip|lzip|tar|zip path/to/archive path/to/file_or_directory

Motivation: Different formats have unique properties and compatibility. Using 7z to archive in a specific format allows users to select the best format for their needs, considering factors like compression rate, speed, and compatibility with other systems.

Explanation:

  • 7z: Initiates the 7z command line interface.
  • a: Stands for “add”, indicating the intent to add files to an archive.
  • -t7z|bzip2|gzip|lzip|tar|zip: Specifies the archive type. Replace “|” with the desired format to use.
  • path/to/archive: Defines the target archive’s path and name (extension automatically added).
  • path/to/file_or_directory: Specifies the file or directory to be added.

Example Output:

  Adding    path/to/file_or_directory
  Everything is Ok

Use case 7: List the contents of an archive

Code:

7z l path/to/archive.7z

Motivation: Before extracting an archive, it is often beneficial to list its contents to understand its structure, verify file existence, or guide an extraction strategy. This can prevent unnecessary extraction of unwanted files.

Explanation:

  • 7z: Initiates the 7z command line interface.
  • l: Stands for “list”, displaying the contents of the specified archive.
  • path/to/archive.7z: Indicates the archive to be inspected.

Example Output:

  Date      Time    Attr         Size   Compressed  Name
  2023-10-01 12:00  .....        1024         500  file1.txt
  2023-10-01 12:00  D....            0           0  dir1/
  2023-10-01 12:00  .....        2048        1024  dir1/file2.txt

Use case 8: Set the level of compression (higher means more compression, but slower)

Code:

7z a path/to/archive.7z -mx=0|1|3|5|7|9 path/to/file_or_directory

Motivation: Adjusting compression levels lets users balance between the time taken to compress files and the size of the resulting archive. Higher compression levels yield smaller files, beneficial for storage and transfer, but require more time and computational power.

Explanation:

  • 7z: Initiates the 7z command line interface.
  • a: Indicates the addition of files to an archive.
  • path/to/archive.7z: Path and name of the archive.
  • -mx=0|1|3|5|7|9: Sets compression level from 0 (store only) to 9 (maximum compression). Replace “|” with the desired level.
  • path/to/file_or_directory: Specifies the files/directories to archive.

Example Output:

  Adding    path/to/file_or_directory
  Everything is Ok

Conclusion

The 7z command is a highly flexible tool that caters to numerous file management operations tailored for personal and professional use. Whether for compressing files to save space, securing data through encryption, or efficiently managing archive contents, mastering these use cases will unlock the full potential of 7z in handling file archives.

Related Posts

Effective Use of 'redis-server' (with examples)

Effective Use of 'redis-server' (with examples)

Redis is a high-performance, open-source, in-memory data structure store, primarily used as a database, cache, and message broker.

Read More
Exploring Calendar Command Usage (with examples)

Exploring Calendar Command Usage (with examples)

The cal command is a simple yet powerful utility available in Unix and Unix-like operating systems, including Linux and macOS.

Read More
How to Use the Command 'psalm' (with examples)

How to Use the Command 'psalm' (with examples)

Psalm is a static analysis tool designed for identifying errors in PHP applications.

Read More