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
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.