A modern file manager for the command line
Command Line – ranger
Ranger offers a wealth of commands – many with alternatives – and less reliance on a mouse.
Linux has all the necessary commands for manipulating files, everything from ls
and cp
to rm
and mv
. However, file managers have the advantage of centralizing all the commands and requiring less knowledge in all except the most advanced circumstances. In fact, file managers remain so useful that Midnight Commander (mc
) continues to be used at the command line 27 years after it was first released. A more recent command-line file manager is ranger [1], which uses many Vim keybindings and is written in Python, which allows for the easy creation of custom scripts.
Ranger is available in many distributions, and the latest version can be downloaded from GitHub [1]. It requires Python 3 and includes a long list of suggested packages for full functionality, such as w3m for graphics display, poppler-utils for PDF display, and python-pygments for code highlighting. It creates the directory ~/.config/ranger/rc.conf
with the configuration files in Table 1 (shown in Figure 1).
Table 1
Ranger Configuration Files
rifle.conf |
Configuration for |
commands.py |
Defines the default ranger console commands |
commands_full.py |
Reference file for custom commands |
rc.conf |
The main configuration file |
scope.sh |
Defines how to handle preview files |
The configuration files also function as documentation on how to customize; although, unless you have a knowledge of Python, these files may be of limited use. A few notes are given in the GitHub repository [2].
Upon startup, ranger first references the global configuration in /etc/ranger/rc.conf
by default and then ~/.config/ranger/rc.conf
. To avoid potential conflicts, you should add to the top of rc.conf
the line RANGER_LOAD_DEFAULT_RC =FALSE
so that ranger bypasses the global configuration, and make all your edits in ~/.config/ranger
(Figure 2). While making this change, you might also scan the local rc.conf
for other customizations, such as whether ranger displays hidden files or if deletions require confirmation. The other files can be ignored unless you want to customize.
Basic Navigation
If ranger is started as a bare command, it displays your home directory. However, you can also follow the command with a space-separated list of directories and files. A few options are available, but most are unnecessary, especially if you add a list of directories. For many, the only useful option may be --show-only-dirs
. Each directory or file specified is opened in a tab in the order in which it is listed in the command. The number of directories or files open is listed in the upper right corner. To switch to another tab, press Tab or gt to move to the next tab, or Shift+Tab or gT to move to the previous tab. The keybinding gc will close the current tab, and Ctrl+N or gn opens a new tab. In addition, you may want to enter the command:
<C>:set show hidden true<C>
or add it to ~/.config/ranger/rc.conf
to show hidden files as the permanent default.
Each tab has three panes (Figure 3). The left-hand pane shows the top level directories, and the middle one the second level subdirectories. The right-hand pane shows the third-level subdirectories and files, with statistics at the bottom. Types of files are color-coded – for instance, PDF files are white, JPEGs orange, and FLAC files pink. Basic navigation is with arrow keys, with more complex commands assigned to other keys and key combinations (see Table 2). Default key assignments are based on Vim or mc
, making ranger easy for experienced command line users to learn. An online summary of basic navigation displays when you press the g key.
Table 2
Basic ranger Navigation
Up arrow, Page Up |
Up a page |
Ctrl+U |
Up half page |
Down arrow |
Go down |
Ctrl+F, Page Down |
Down a page |
Ctrl+D |
Go down half page |
Left arrow |
Go left |
Right arrow |
Go right |
Home |
Move to top |
End |
Move to bottom |
cd |
Change the working directory to a specific directory |
gh |
Change the working directory to your home |
gm |
Change the working directory to /media |
gM |
Change the working directory to /mnt |
gu |
Change the working directory to /usr |
H |
Back in history |
L |
Forward in history |
q |
Quit |
Copying, Moving, and Deleting
To access basic file management, press y for a list of copy and paste commands and d for move or delete commands (Figure 4). Note that, like Vim, ranger uses the term "yank" instead of paste. Besides the basic commands, the lists also contain advanced options for the basic keystrokes (Table 3). Alternatively, you can press r with a file selected or the s key and use the standard shell commands instead.
Table 3
Basic File Manipulation
yy |
Copy |
yp |
Paste (yank) |
yd |
Paste directory (yank) |
dd |
Cut |
dD |
Delete (yank) |
ya, da |
Add selection to a list of files |
yr, dr |
Remove selection from a list of files |
yt, dt |
Toggle list of files on or off |
For actions involving more than one file, you can use copy or cut modes. Pressing a adds a selected file to the multiple files, while r removes the selected file. When you are finished creating a list of files, you can proceed with the operation by pressing t to toggle to the copy or cut mode.
To rename or move a selection, press wa. For renaming multiple files or directories, you can also use the :bulkrename
command.
Viewing and Searching
Although ranger is a command-line application, it can open other applications to view formats that include PDF, OpenDocument (LibreOffice), BitTorrent, HTML, and SVG. The supported formats and how they are handled are listed in the scope.sh
configuration file. You can use the examples in scope.sh
to add support for other formats. If a file is properly configured in scope.sh
, all you need to do is select the file and press the Enter key. Some files display in the right-hand pane (Figure 5), and others open in an application in a new window. If a file opens in the righthand pane, but you would like to view it in its native application, you can use the :travel
command. Should a file type not be supported, you can open a command line and run the rifle
command, which supports a wide variety of formats.
At times, you may just want to view file attributes with :linemode
. The command can be completed by filename
, permissions
, fileinfo
, mtime
(last time modified), or sizemtime
(the size and time of the last modification). You can modify these completions by using them with :meta
.
The :travel
command can also be used to quickly find directories. Just enter the name of the directory at the end of the command. Another alternative is to create a bookmark for a selected directory by pressing m and assigning a letter or number. A list of existing bookmarks displays as a crib, and you can create the bookmark by pressing m a second time. To jump to a bookmark, press ` followed by the bookmark's letter or number.
Within directories, you can use / or f to find a file. If the file is not found, you can search for it in the next file by pressing n, or in the previous directory by pressing N. Visual searches can be enhanced with :filter REGEX
to reduce the number of files displayed. You can also sort by file size with os or arrange sub-directories by size with du.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs