How to Use the Command 'nnn' (with Examples)
The command nnn
is a lightweight and fast terminal-based file manager and disk usage analyzer. It offers advanced features such as browsing, managing files, directories, and performing disk usage analysis, all within the terminal environment. Perfect for those who prefer keyboard-driven navigation and shell scripting for file management tasks, nnn
can replace many complex graphical user interface applications with its simplicity and speed.
Use case 1: Open the Current Directory
Code:
nnn
Motivation:
Opening the current directory with nnn
allows users to manage their files in a simple and efficient manner directly from the terminal. This is especially beneficial for system administrators or developers who spend considerable time working within a command-line interface, providing them with a more visual representation of files and directories than standard ls
or ls -l
commands.
Explanation:
- The command is simply
nnn
with no additional options, which defaults to opening the current working directory. Without extra arguments or options,nnn
provides a standard interactive file manager view.
Example Output:
Upon execution, it launches a straightforward user interface within the terminal window where files and directories are displayed in rows. Users can then navigate these using arrow keys or enter directories by pressing the Enter key.
Use case 2: Start in Detailed Mode
Code:
nnn -d
Motivation:
Running nnn
in detailed mode provides more comprehensive information about each file and directory at a glance. This mode is particularly useful for users who need detailed file attributes such as size, modification date, and more, displayed immediately as they navigate through the file system, thereby saving them from having to execute additional commands to gather such information.
Explanation:
-d
: This option stands for ‘detailed mode’, which displays more information about files, including permissions, size, modification time, etc.
Example Output:
In detailed mode, each file and directory is listed with additional attributes like file size, permission bits, and date of last modification, all arranged neatly next to the filenames.
Use case 3: Show Hidden Files
Code:
nnn -H
Motivation:
Sometimes, files or directories beginning with a dot (e.g., .git, .bashrc) are hidden from regular views. By explicitly asking nnn
to show these, users can explore or edit configuration files or any hidden resources, which is particularly crucial for developers or IT specialists who often work with secret or configuration files.
Explanation:
-H
: The-H
flag instructsnnn
to reveal hidden files upon initialization, offering visibility into files that are concealed by default.
Example Output:
When executed, the interface will now include hidden files and directories along with regular files, allowing users access to all available data in the directory.
Use case 4: Open an Existing Bookmark
Code:
nnn -b bookmark_name
Motivation:
The use of bookmarks greatly enhances productivity by allowing users to quickly jump to frequently accessed directories without manually typing paths each time. This feature is especially useful for those who work with multiple projects or frequently accessed data sources.
Explanation:
-b bookmark_name
: This argument is used to open a directory path that has been previously bookmarked. The bookmark name should be defined in theNNN_BMS
environment variable, mapping the bookmark name to a specific directory path.
Example Output:
Upon execution, nnn
will open the bookmarked directory immediately, streamlining the navigation process for the user and reducing time otherwise spent on locating the path manually.
Use case 5: Sort Files
Code:
nnn -T a|d|e|r|s|t|v
Motivation:
Sorting files based on different criteria can dramatically enhance one’s efficiency in locating files or managing size constraints. Whether sorting by size, time, or extension, having an organized display helps in quick assessment and better file management.
Explanation:
-T
: This option triggers the sort feature.a|d|e|r|s|t|v
: These specify the criteria for sorting:a
: Apparent disk usaged
: Disk usagee
: Extensionr
: Reverse order of the current sort options
: Sizet
: Timev
: Version
Example Output:
Files will be rearranged according to the specified sort option. For example, using -T s
will sort files by size, displaying them from largest to smallest.
Use case 6: Open a File with a Specific Application
Code:
nnn -o
Motivation:
There are times when a file needs to be opened with a specific application rather than the default. This flexibility caters to varied workflow needs like opening a text file in a different editor or launching a media file in a specific player, marrying ease with speed in file management.
Explanation:
-o
: This argument enables the user to select a file and specify a particular program to open it with. Once a file is selected, pressingo
allows the user to type the desired application for the task.
Example Output:
After selecting a file and confirming the program to use, the selected file opens in the specified application, allowing users to immediately access and work with their data in their preferred tools.
Conclusion:
The nnn
command offers a powerful yet user-friendly interface for managing files and analyzing disk usage from the terminal. From viewing hidden files, sorting by various criteria, and starting in detailed mode to leveraging bookmarks and opening files with specific applications, nnn
efficiently fulfills diverse file management needs. These examples illustrate the flexibility and depth of functionality that nnn
provides, making it an indispensable tool in any terminal user’s arsenal.