Linux lha Command

Linux 命令大全Linux Command Reference

The Linux lha command is used to compress or decompress files.

lha is a.lzhand.lhacommand-line tool used in Linux systems to process compressed files. It was originally developed for the LHA compression format popular in Japan, and is still used in certain specific scenarios today.

Main Features

  • Supports compression and decompression.lzhand.lhaformat files.
  • Compatible with old DOS/Windows compressed files
  • Lightweight, suitable for resource-constrained environments
  • Preserves original file attributes (such as timestamps)

Install lha Tool

On most Linux distributions, lha is not a pre-installed tool and needs to be installed manually:

Ubuntu/Debian Systems

Example

sudo apt-get update
sudo apt-get install lha

CentOS/RHEL Systems

Example

sudo yum install lha

Verify Installation

Example

lha --version

Basic Command Syntax

The basic syntax format of the lha command is:

Example

lha [options]command archive[file/directory...]

Common Command Parameters

Command Description
a Add files to archive
e Extract files (without preserving paths)
x Extract files (preserving full paths)
l List archive contents
t Test archive integrity
d Delete files from archive

Common Option Parameters

Option Description
-v Display detailed operation information
-q Quiet mode (no output)
-f Force operation
-x Allow extended attributes
-p Preserve file permissions

Usage Examples

1. Create Archive

Compress the directoryprojectintoproject.lzh:

Example

lha a project.lzh project/

Add multiple files and directories:

Example

lha a archive.lzh file1.txt file2.txt dir1/

2. List Archive Contents

Viewarchive.lzhthe file list in:

Example

lha l archive.lzh

Example output:

 PERMISSION  UID  GID      SIZE  RATIO     STAMP     NAME
---------- ----------- ------- ------ ------------ --------------------
-rw-r--r--  1000/1000     1024  65.3% May 15 10:00 file1.txt
-rw-r--r--  1000/1000     2048  70.1% May 15 10:05 file2.txt
drwxr-xr-x  1000/1000        0   0.0% May 15 09:55 dir1/

3. Extract Files

Extract to current directory (without preserving paths):

Example

lha e archive.lzh

Extract and preserve full path structure:

Example

lha x archive.lzh

Extract specific files:

Example

lha e archive.lzh file1.txt

4. Test Archive Integrity

Example

lha t archive.lzh

5. Delete Files from Archive

Deletearchive.lzhfromfile1.txt:

Example

lha d archive.lzh file1.txt

Advanced Usage

1. Split Archive

Create a split archive (1MB per volume):

Example

lha -v -s1024k a archive.lzh large_dir/

2. Set Compression Level

Specify the compression level (0-5, default 2):

Example

lha -2 a archive.lzh files/  # Higher compression ratio
lha -0 a archive.lzh files/  # Store only, no compression

3. Exclude Specific Files

Use-xto exclude files:

Example

lha a -x*.tmp archive.lzh dir/

FAQ

Q1: What is the difference between lha and zip/gzip?

  • lha is a.lzh/.lhatool specifically for the format.
  • The compression ratio is usually not as good as gzip/bzip2
  • The main advantage is compatibility with old Japanese system files

Q2: How to extract password-protected .lzh files?

lha itself does not support password protection; you need to use other tools such asunar:

Example

unar -p password protected.lzh

Q3: Why doesn't my lha command work?

Possible reasons:

  1. lha tool not installed (see installation section)
  2. File permission issue (try using sudo)
  3. The archive is corrupted (uselha tto test)

Best Practice Recommendations

  1. Compatibility considerations: Only use lha when you need to handle old system files; for new projects, it is recommended to use the more common zip or tar.gz.

  2. Usage in scripts: Add the-qoption to avoid output interference:

Example

lha -q a backup.lzh /path/to/files
  1. Combine with find: compress specific types of files.

Example

find . -name "*.txt" -exec lha a textfiles.lzh {} +
  1. Backup scenarios: preserve file attributes

Example

lha -p a backup.lzh important_dir/

Alternatives

Although lha has its specific uses, these tools are more commonly used on modern Linux systems:

Tool Format Features
tar .tar Package only, no compression
gzip .gz High compression ratio
bzip2 .bz2 Higher compression ratio, slower speed
xz .xz Extremely high compression ratio
zip .zip Cross-platform compatibility
7z .7z Support for multiple compression algorithms

Linux 命令大全Linux Command Reference

Other Extensions