Linux lha command
The Linux lha command is used to compress or decompress files.
lha is a command-line tool in Linux for handling .lzh and .lha compressed files. It was originally developed to handle the LHA compression format popular in Japan, and is still used in certain specific scenarios.
Main Features
- Supports compression and decompression
.lzhand.lhaformat files - Compatible with old DOS/Windows compressed files
- Lightweight, suitable for environments with limited resources
- Preserves original file attributes (such as timestamps)
Installing the lha tool
On most Linux distributions, lha is not pre-installed and must 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 the installation
Example
lha
--version
Basic command syntax
The basic syntax of the lha command is:
Example
lha
[
Option
]
command archive-file
[
file
/
directory...
]
Common command parameters
| Command | Description |
|---|---|
| a | Add files to an 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 the archive |
Commonly used options
| Option | Description |
|---|---|
| -v | Display detailed operation information |
| -q | Quiet mode (no output) |
| -f | Force the operation |
| -x | Allow extended attributes |
| -p | Preserve file permissions |
Usage examples
1. Create an archive
Compress the directory project into project.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
Check archive.lzh the list of files 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 the current directory (without preserving paths):
Example
lha e archive.lzh
Extract and preserve the 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 an archive
Delete archive.lzh The file1.txt:
Example
lha d archive.lzh file1.txt
Advanced usage
1. Multi-volume compression
Create a multi-volume archive (1MB per volume):
Example
lha
-v
-s1024k
a archive.lzh large_dir
/
2. Set the 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 -x Exclude files:
Example
lha a -x
.tmp archive.lzh dir
/
Frequently Asked Questions
Q1: What is the difference between lha and zip/gzip?
- lha is a tool specifically for handling
.lzh/.lhaformat files - compression ratio is usually not as good as gzip/bzip2
- its 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 as unar:
Example
unar
-p
password protected.lzh
Q3: Why doesn't my lha command work?
Possible reasons:
- The lha tool is not installed (see the installation section)
- File permission issues (try using sudo)
- The archive is corrupted (use
lha tto test)
Best Practice Recommendations
-
Compatibility considerations: Use lha only when you need to handle old system files; for new projects, more general tools such as zip or tar.gz are recommended
-
Use in scripts: Add the
-qoption to avoid output interference:
Example
lha
-q
a backup.lzh
/
path
/
to
/
files
- Use together with find: compress specific types of files
Example
find
.
-name
"*.txt"
-exec
lha a textfiles.lzh
{
}
- 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 in modern Linux systems:
| Tool | Format | Feature |
|---|---|---|
| tar | .tar | Archive only, no compression |
| gzip | .gz | High compression ratio |
| bzip2 | .bz2 | Higher compression ratio, slower speed |
| xz | .xz | Very high compression ratio |
| zip | .zip | Cross-platform compatible |
| 7z | .7z | Supports multiple compression algorithms |
Linux Command Reference