YouTip LogoYouTip

Linux Comm Lha

Image 1: Linux Command Encyclopaedia Linux Command Manual

\n\n

Linux lha Command

\n\n

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

\n\n

lha is a command-line tool used in Linux systems to handle .lzh and .lha compressed files. It was originally developed to process the LHA compression format popular in Japan and is still used in certain specific scenarios.

\n\n

Main Features

\n
    \n
  • Supports compressing and decompressing .lzh and .lha format files
  • \n
  • Compatible with old DOS/Windows compressed files
  • \n
  • Lightweight, suitable for resource-limited environments
  • \n
  • Preserves original file attributes (such as timestamps)
  • \n
\n\n
\n\n

Installing the lha Tool

\n\n

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

\n\n

Ubuntu/Debian Systems

\n\n
sudo apt-get update\nsudo apt-get install lha
\n\n

CentOS/RHEL Systems

\n\n
sudo yum install lha
\n\n

Verify Installation

\n\n
lha --version
\n\n
\n\n

Basic Command Syntax

\n\n

The basic syntax format of the lha command is:

\n\n
lha  command archive [files/directories...]
\n\n

Common Command Parameters

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
CommandDescription
aAdd files to an archive
eExtract files (without preserving paths)
xExtract files (preserving full paths)
lList archive contents
tTest archive integrity
dDelete files from an archive
\n\n

Common Option Parameters

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
OptionDescription
-vDisplay detailed operation information
-qQuiet mode (no output displayed)
-fForce operation
-xAllow extended attributes
-pPreserve file permissions
\n\n
\n\n

Usage Examples

\n\n

1. Creating an Archive

\n\n

Compress the project directory into project.lzh:

\n\n
lha a project.lzh project/
\n\n

Add multiple files and directories:

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

2. Listing Archive Contents

\n\n

View the file list in archive.lzh:

\n\n
lha l archive.lzh
\n\n

Example output:

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

3. Extracting Files

\n\n

Extract to the current directory (without preserving paths):

\n\n
lha e archive.lzh
\n\n

Extract while preserving the full directory structure:

\n\n
lha x archive.lzh
\n\n

Extract a specific file:

\n\n
lha e archive.lzh file1.txt
\n\n

4. Testing Archive Integrity

\n\n
lha t archive.lzh
\n\n

5. Deleting Files from an Archive

\n\n

Delete file1.txt from archive.lzh:

\n\n
lha d archive.lzh file1.txt
\n\n
\n\n

Advanced Usage

\n\n

1. Split Compression

\n\n

Create a split archive (1MB per volume):

\n\n
lha -v-s1024k a archive.lzh large_dir/
\n\n

2. Setting Compression Level

\n\n

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

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

3. Excluding Specific Files

\n\n

Use -x to exclude files:

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

Frequently Asked Questions

\n\n

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

\n
    \n
  • lha is a tool specifically for handling .lzh/.lha formats.
  • \n
  • Its compression ratio is usually not as good as gzip/bzip2.
  • \n
  • Its main advantage is compatibility with old Japanese system files.
  • \n
\n\n

Q2: How to extract a password-protected .lzh file?

\n

lha itself does not support password protection. You need to use other tools like unar:

\n\n
unar -p password protected.lzh
\n\n

Q3: Why isn't my lha command working?

\n

Possible reasons:

\n
    \n
  1. The lha tool is not installed (see the installation section).
  2. \n
  3. File permission issues (try using sudo).
  4. \n
  5. The archive file is corrupted (use lha t to test).
  6. \n
\n\n
\n\n

Best Practice Recommendations

\n\n
    \n
  1. Compatibility Considerations: Only use lha when you need to handle old system files. For new projects, it is recommended to use more universal formats like zip or tar.gz.
  2. \n
  3. Use in Scripts: Add the -q option to avoid output interference:\n
    lha -q a backup.lzh /path/to/files
    \n
  4. \n
  5. Combine with find: Compress specific types of files:\n
    find . -name"*.txt"-exec lha a textfiles.lzh {} +
    \n
  6. \n
  7. Backup Scenarios: Preserve file attributes:\n
    lha -p a backup.lzh important_dir/
    \n
  8. \n
\n\n
\n\n

Alternative Tools

\n\n

Although lha has its specific uses, modern Linux systems more commonly use these tools:

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ToolFormatCharacteristics
tar.tarArchiving only, no compression
gzip.gzHigh compression ratio
bzip2.bz2Higher compression ratio, slower speed
xz.xzExtremely high compression ratio
zip.zipCross-platform compatibility
7z.7zSupports multiple compression algorithms
\n\n

Image 2: Linux Command Encyclopaedia Linux Command Manual

← Os MakedevLinux Comm Gzip β†’