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 enable `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 confirm it 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 of `lesspipe` will be piped to `less`.
- `%s`: A placeholder representing the path to the file `less` is trying to open.
- `LESS_ADVANCED_PREPROCESSOR=1`: Used in some `lesspipe` versions 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 Contents of a gzip Compressed File
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 with `less` using the `strings` command.
Tips & Considerations
Tips and considerations for effectively using `lesspipe`.
Custom lesspipe Scripts
The default `lesspipe` script is a shell script, which means you can modify it as needed or create your own `lesspipe` script and specify it in the `LESSOPEN` variable. For example, you can add additional processing logic for specific file types.
- Copy and modify the existing `lesspipe` script
- Add logic for new file types
- Specify the path to your custom script in the `LESSOPEN` variable
Performance Considerations
When opening very large compressed files or archives with `lesspipe`, it may take time to extract and convert the file contents. This can result in a slight delay before `less` starts.
Security Precautions
`lesspipe` executes external commands to process files. Therefore, using an untrusted `lesspipe` script or having malicious files processed through `lesspipe` can lead to security issues. Always use `lesspipe` from a trusted source and be cautious when configuring the `LESSOPEN` variable.
Check Supported File Formats
By examining the `lesspipe` script installed on your system, you can determine which file formats it supports and which external tools it uses. You can typically view the script's content with a command like `cat /usr/bin/lesspipe` (or the appropriate path).