Overview
`lesspipe` is a utility that helps `less` handle various file formats beyond plain text files. When `less` opens a file, it executes the `lesspipe` script to convert the file's content to standard output, which `less` then receives and displays.
Key Features
- View contents of compressed files (gzip, bzip2, xz, etc.) directly
- View the list and contents of files within archive files (tar, zip, rar, etc.)
- Extract and view text from non-text files like PDFs and images (requires relevant tools)
- Maintain
less's powerful search and navigation capabilities
Installation and Setup
`lesspipe` is often provided as part of the `less` package on most Linux distributions or as a separate `lesspipe` package. The key is to set the `LESSOPEN` environment variable to make `less` use `lesspipe`.
Check Installation
It is pre-installed on most systems, but you can check for its existence with the following command.
which lesspipe
Install Package (if needed)
If lesspipe is not installed, you can install it with the following commands.
Debian/Ubuntu
sudo apt update && sudo apt install less
Fedora/RHEL
sudo dnf install less
Set LESSOPEN Environment Variable
To activate lesspipe, you need to add the LESSOPEN environment variable to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc). The exact path to lesspipe may vary by system, so check with which lesspipe.
Example LESSOPEN Configuration
export LESSOPEN="|/usr/bin/lesspipe %s"
export LESS_ADVANCED_PREPROCESSOR=1
source ~/.bashrc
A typical configuration is as follows. Apply changes immediately using the `source` command.
LESSOPEN Variable Explanation
|: Indicates that the output oflesspipeis piped toless.%s: A placeholder representing the path to the filelessis trying to open.LESS_ADVANCED_PREPROCESSOR=1: Used in somelesspipeversions to enable advanced preprocessor features.
Usage Examples
Once `lesspipe` is correctly configured, using the `less` command as usual will automatically handle various file formats.
View gzip Compressed File Contents
less my_log.gz
View the contents of a `.gz` file directly with `less` without decompressing it.
View List of Files in a tar Archive
less my_archive.tar.gz
Check the list of files inside a `.tar.gz` file with `less` without decompressing it.
View List of Files in a zip Archive
less my_documents.zip
Check the list of files inside a `.zip` file with `less` without decompressing it.
View Text Content of a PDF File
less document.pdf
If tools like `pdftotext` are installed, you can view the text content of a PDF file with `less`.
View Strings in a Binary File
less /bin/ls
For binary files, you can view extracted strings using the `strings` command with `less`.
Tips & Considerations
Tips and considerations for effectively using `lesspipe`.
Custom lesspipe Scripts
The default lesspipe script is a shell script, so you can modify it or create your own lesspipe script and specify it in the LESSOPEN variable if needed. For example, you can add additional processing logic for specific file types.
- Copy and modify the existing
lesspipescript - Add logic for new file types
- Specify the path to your custom script in the
LESSOPENvariable
Performance Considerations
Opening very large compressed files or archives with lesspipe may take time to extract and convert the file contents. In such cases, there might be a slight delay before less starts.
Security Warning
lesspipe executes external commands to process files. Therefore, using untrusted lesspipe scripts or having malicious files processed through lesspipe can lead to security issues. Always use lesspipe from trusted sources and be cautious when configuring the LESSOPEN variable.
Check Supported File Formats
By examining the contents of the lesspipe script installed on your system, you can determine which file formats it supports and which external tools it uses. Generally, you can view the script's content with a command like cat /usr/bin/lesspipe (or the relevant path).