Efficient File Compression in Ranger File Manager
Table of Contents 📑
Terminal file managers like ranger offer powerful ways to manage your files efficiently. This guide shows you how to enhance ranger with custom commands for compressing files into various archive formats directly from the file manager interface. This is part of a series of guides on extending ranger’s functionality.
Changelog
Date | Change |
---|---|
2025-07-18 | Initial Version: Created guide for compression workflow in ranger |
1. Prerequisites
Before we begin, make sure you have the following tools installed:
- ranger: The terminal file manager
- atool: A script for managing file archives of various types
- Archive utilities: While atool is a wrapper, you’ll need the actual compression tools installed:
- zip, unzip: For .zip archives
- tar: For .tar, .tar.gz, .tar.bz2 archives
- p7zip-full (Debian/Ubuntu) or p7zip (Arch/Fedora): For .7z archives
- rar, unrar: For .rar archives
Installation on Various Distributions
# For Debian/Ubuntu
sudo apt update && sudo apt install ranger atool
# For Arch Linux
sudo pacman -S ranger atool
# For Fedora
sudo dnf install ranger atool
The atool
package provides commands like apack
(for creating archives) and aunpack
(for extracting archives), which we’ll use in our custom ranger commands.
2. Creating the Custom compress Command
The integration requires adding a custom command to ranger that will use apack
to compress selected files into an archive.
2.1. Create or Edit commands.py
First, navigate to your ranger configuration directory and create or edit the commands.py
file:
mkdir -p ~/.config/ranger
touch ~/.config/ranger/commands.py
If you don’t have a commands.py
file yet, you can generate a template with:
ranger --copy-config=commands
2.2. Add the compress Command
Open the commands.py
file in your favorite text editor and add the following code:
import os
from ranger.core.loader import CommandLoader
class compress(Command):
def execute(self):
""" Compress marked files to current directory """
cwd = self.fm.thisdir
marked_files = cwd.get_selection()
if not marked_files:
return
def refresh(_):
cwd = self.fm.get_directory(original_path)
cwd.load_content()
original_path = cwd.path
parts = self.line.split()
au_flags = parts[1:]
descr = "Compressing to: " + os.path.basename(parts[1])
obj = CommandLoader(args=['apack'] + au_flags + \
[os.path.relpath(f.path, cwd.path) for f in marked_files], descr=descr, read=True)
obj.signal_bind('after', refresh)
self.fm.loader.add(obj)
def tab(self, tabnum):
""" Complete with current folder name """
extension = ['.zip', '.tar.gz', '.rar', '.7z']
return ['compress ' + os.path.basename(self.fm.thisdir.path) + ext for ext in extension]
This command will:
- Take the marked files in ranger
- Use
apack
to compress them into an archive - Refresh the directory view after compression
- Provide tab completion for common archive formats
3. Adding an Extract Command (Optional)
For a complete compression workflow, you might also want to add an extraction command. Add this to your commands.py
file:
class extract_here(Command):
def execute(self):
""" Extract selected files to current directory """
cwd = self.fm.thisdir
marked_files = cwd.get_selection()
if not marked_files:
return
def refresh(_):
cwd = self.fm.get_directory(original_path)
cwd.load_content()
original_path = cwd.path
au_flags = ['-X', cwd.path] # Extract to current directory
au_flags += self.line.split()[1:]
au_flags += ['-e']
self.fm.copy_buffer.clear()
self.fm.cut_buffer = False
if len(marked_files) == 1:
descr = "extracting: " + os.path.basename(marked_files[0].path)
else:
descr = "extracting {} files".format(len(marked_files))
obj = CommandLoader(args=['aunpack'] + au_flags + \
[f.path for f in marked_files], descr=descr, read=True)
obj.signal_bind('after', refresh)
self.fm.loader.add(obj)
4. Creating Keyboard Shortcuts
Now that we have our custom commands, let’s create keyboard shortcuts to invoke them easily.
4.1. Edit rc.conf
Open your ranger configuration file:
vim ~/.config/ranger/rc.conf
If you don’t have this file yet, you can generate it with:
ranger --copy-config=rc
4.2. Add the Keyboard Mappings
Add the following lines to map the commands to keyboard shortcuts:
# Compression and extraction
map cc console compress%space
map cx extract_here
With these shortcuts:
cc
will start the compress command and wait for you to specify the archive namecx
will extract the selected archive in the current directory
Shortcut | Description |
---|---|
cc | Compress selected files into an archive |
cx | Extract selected archive in the current directory |
5. Advanced Configuration
5.1. Customize Archive Formats
You can modify the list of supported archive extensions in the tab
method of the compress
command:
def tab(self, tabnum):
""" Complete with current folder name """
extension = ['.zip', '.tar.gz', '.tar.bz2', '.tar.xz', '.rar', '.7z']
return ['compress ' + os.path.basename(self.fm.thisdir.path) + ext for ext in extension]
Add or remove archive formats based on your needs and installed utilities.
5.2. Add Compression Options
You can enhance the compress
command to support additional options like compression level:
def tab(self, tabnum):
""" Complete with current folder name and compression options """
extension = ['.zip', '.tar.gz', '.rar', '.7z']
base_name = os.path.basename(self.fm.thisdir.path)
# Basic archive names
options = ['compress ' + base_name + ext for ext in extension]
# Add options with compression levels for zip
if tabnum == 1:
options += ['compress ' + base_name + '.zip -mx=9'] # Maximum compression
options += ['compress ' + base_name + '.zip -mx=1'] # Fastest compression
return options
5.3. Exclude Certain Files
You might want to exclude certain files from compression (like temporary files or already compressed files). You can modify the execute
method to filter the marked files:
marked_files = [f for f in cwd.get_selection() if not f.path.endswith(('.zip', '.rar', '.7z'))]
6. Usage
Once everything is set up, you can use your new compression workflow:
6.1. Creating Archives
- Open ranger in your terminal
- Navigate to the directory containing files you want to compress
- Select files using the
space
key (or select a single file with the cursor) - Press
cc
to start the compress command - Type the archive name or press
Tab
to auto-complete with suggested formats - Press
Enter
to create the archive
6.2. Extracting Archives
- Navigate to an archive file or select it
- Press
cx
to extract its contents to the current directory
7. Practical Examples
7.1. Backup Workflow
Create quick backups of important directories:
- Navigate to a project folder
- Press
cc
thenTab
to get suggestions - Select
.tar.gz
format for good compression - Add a date to the filename:
project-backup-2025-07-18.tar.gz
7.2. Sharing Files
Compress files for sharing:
- Select multiple files with
space
- Press
cc
and create a.zip
file (most compatible format) - The archive is ready to be emailed or transferred
7.3. Working with Different Archive Types
The workflow supports various archive formats:
.zip
- Good compatibility across platforms.tar.gz
- Better compression, common on Linux.7z
- Best compression ratio.rar
- Good for split archives
8. Troubleshooting
8.1. Command Not Found
If you get a “Command not found” error when trying to use the shortcut:
- Make sure you’ve saved the
commands.py
file correctly - Restart ranger to load the new command
- Check that
atool
is installed and in your PATH
8.2. Compression Fails
If compression fails:
- Check if you have the appropriate compression tools installed
- Verify that you have write permissions in the current directory
- Make sure you have enough disk space
Try running apack
directly to see if it works outside of ranger:
apack archive.zip file1 file2 file3
8.3. File Not Found Errors
If you get “file not found” errors:
- Make sure you’re not trying to compress files with special characters in their names
- Check if the paths are correct
- Try using relative paths instead of absolute paths
Summary
With this compression workflow integration, you’ve enhanced ranger’s capabilities for working with archives:
✅ Quick compression of files with just a few keystrokes
✅ Support for multiple archive formats
✅ Tab completion for common archive types
✅ Easy extraction of archives
✅ Seamless integration with your terminal workflow
This setup is particularly valuable for system administrators, developers, and anyone who frequently needs to create or extract archives. It combines the file management power of ranger with the versatility of atool for handling various archive formats.
Related Ranger Guides
Enhance your ranger experience with these additional tutorials:
- Ranger and fzf Integration - Add powerful fuzzy search capabilities
- Image Metadata Viewing with exiftool - View detailed metadata for images and media files
- Advanced Media Preview Configuration - Customize file previews for various formats
- Ranger and sxiv Integration - Create a seamless image viewing workflow