Linux lha Command
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 install lha
CentOS/RHEL Systems
Example
Verify Installation
Example
Basic Command Syntax
The basic syntax format of the lha command is:
Example
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
Add multiple files and directories:
Example
2. List Archive Contents
Viewarchive.lzhthe file list in:
Example
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
Extract and preserve full path structure:
Example
Extract specific files:
Example
4. Test Archive Integrity
Example
5. Delete Files from Archive
Deletearchive.lzhfromfile1.txt:
Example
Advanced Usage
1. Split Archive
Create a split archive (1MB per volume):
Example
2. Set Compression Level
Specify the compression level (0-5, default 2):
Example
lha -0 a archive.lzh files/ # Store only, no compression
3. Exclude Specific Files
Use-xto exclude files:
Example
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
Q3: Why doesn't my lha command work?
Possible reasons:
- lha tool not installed (see installation section)
- File permission issue (try using sudo)
- The archive is corrupted (use
lha tto test)
Best Practice Recommendations
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.
Usage in scripts: Add the
-qoption to avoid output interference:
Example
- Combine with find: compress specific types of files.
Example
- Backup scenarios: preserve file attributes
Example
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 Command Reference