Midnight CommanderAt the beginning of chapter 4 in TLCL there is a discussion of GUI-based file managers versus the traditional command line tools for file manipulation such as In this adventure we will look at Midnight Commander, a character-based directory browser and file manager that bridges the two worlds of the familiar graphical file manager and the common command line tools. The design of Midnight Commander is based on a common concept in file managers: dual directory panes where the listings of two directories are shown at the same time. The idea is that files are moved or copied from the directory shown in one pane to the directory shown in the other. Midnight Commander can do this, and much, much more. FeaturesMidnight Commander is quite powerful and boasts an extensive set of features:
AvailabilityMidnight Commander is part of the GNU project. It is installed by default in some Linux distributions, and is almost always available in every distribution's software repositories as the package "mc". InvocationTo start Midnight Commander, enter the command Screen Layout![]() Midnight Commander screen layout
Using the Keyboard and MouseBeing a character-based application with a lot of features means Midnight Commander has a lot of keyboard commands, some of which it shares with other applications; others are unique. This makes Midnight Commander a bit challenging to learn. Fortunately, Midnight Commander also supports mouse input on most terminal emulators (and on the console if the Another issue when using the keyboard with Midnight Commander is interference from the window manager and the terminal emulator itself. Many of the function keys and Alt-key combinations that Midnight Commander uses are intercepted for other purposes by the terminal and window manager. To work around this problem, Midnight Commander allows the Navigation and BrowsingBefore we start performing file operations, it's important to learn how to use the directory panels and navigate the file system. As we can see, there are two directory panels, the left panel and the right panel. At any one time, one of the panels is active and is called the current panel. The other panel is conveniently called the other panel in the Midnight Commander documentation. The current panel can be identified by the highlighted bar in the directory listing, which can be moved up and down with the arrow keys, Select a directory and press Pressing the Changing the Listing FormatThe directory listing can be displayed in several different formats. Pressing There is also an "information mode." This will display detailed file system information in the other panel about the selected item in the current panel. To invoke this mode, type ![]() Directory panel in information mode Setting the Directory on the Other PanelIt is often useful to select a directory in the current panel and have its contents listed on the other panel; for example, when moving files from a parent directory into a subdirectory. To do this, select a directory and type The Directory HotlistMidnight Commander can store a list of frequently visited directories. This "hotlist" can displayed by pressing ![]() Directory hotlist To add a directory to the hotlist while browsing, select a directory and type Directory HistoryEach directory panel maintains a list of directories that it has displayed. To access this list, type Using The MouseWe can perform many Midnight Commander operations using the mouse. A directory panel item can be selected by clicking on it and a directory can be opened by double clicking. Likewise, the function key labels and menu bar items can be activated by clicking on them. What is not so apparent is that the directory history can be accessed and traversed. At the top of each directory panel there are small arrows (circled in the image below). Clicking on them will show the directory history (the up arrow) and move forward and backward through the history list (the right and left arrows). There is also an arrow to the extreme lower right edge of the command line which reveals the command line history. ![]() Directory and command line history mouse controls Viewing and Editing FilesAn activity often performed while directory browsing is examining the content of files. Midnight Commander provides a capable file viewer which can be accessed by selecting a file and pressing the ![]() File viewer As we can see, when the file viewer is active, the function key labels at the bottom of the screen change to reveal viewer features. Files can be searched and the viewer can quickly go to any position in the file. Most importantly, files can be viewed in either ASCII (regular text) or hexadecimal, for those cases when we need a really detailed view. ![]() File viewer in hexadecimal mode It is also possible to put the other panel into "quick view" mode to view the the currently selected file. This is especially nice if we are browsing a directory full of text files and want to rapidly view the files, as each time a new file is selected in the current panel, it's instantly displayed in the other. To start quick view mode, type ![]() Quick view mode Once in quick view mode, we can press EditingSince we are already viewing files, we will probably want to start editing them too. Midnight Commander accommodates us with the ![]() mcedit Tagging FilesWe have already seen how to select a file in the current directory panel by simply moving the highlight, but operating on a single file is not of much use. After all, we can perform those kinds of operations more easily by entering commands directly on the command line. However, we often want to operate on multiple files. This can be accomplished through tagging. When a file is tagged, it is marked for some later operation such as copying. This is why we choose to use a file manager like Midnight Commander. When one or more files are tagged, file operations (such as copying) are performed on the tagged files and selection has no effect. Tagging Individual FilesTo tag an individual file or directory, select it and press the Tagging Groups of FilesTo tag a group of files or directories according to a selection criteria, such as a wildcard pattern, press the ![]() File tagging dialog This dialog stores a history of patterns. To traverse it, use Ctrl up and down arrows. It is also possible to un-tag a group of files. Pressing the We Need a PlaygroundTo explore the basic file manipulation features of Midnight Commander, we need a "playground" like we had in chapter 4 of TLCL. Creating DirectoriesThe first step in creating a playground is creating a directory called, aptly enough, ![]() Create Directory dialog Type "playground" into the dialog and press Now let's put some files into our playground. Press
![]() The playground Copying and Moving FilesOkay, here is where things start to get weird. Select ![]() Copy dialog To see Midnight Commander's default behavior, just press That was straightforward, but what if we want to copy ![]() Renaming a file during copy Again, this is pretty straightforward. But let's say we tagged a group of files and wanted to copy and rename them as they are copied (or moved). How would we do that? Midnight Commander provides a way of doing it, but it's a little strange. The secret is the source mask in the copy dialog. At first glance, it appears that the source mask is simply a file selection wildcard, but first appearances can be deceiving. The mask does filter files as we would expect, but only in a limited way. Unlike the range of wildcards available in the shell, the wildcards in the source mask are limited to "?" (for matching single characters) and "*" (for matching multiple characters). What's more, the wildcards have a special property. It works like this: let's say we had a file name with an embedded space such as "ugly file" and we want to copy (or move) it to ![]() Using grouping The "?" wildcard behaves the same way. If we make the source mask "???? ????" (which again matches the file Midnight Commander can also perform case conversion on file names. To do this, we include some additional escape sequences in the to mask:
So if we wanted to change the name Creating LinksMidnight Commander can create both hard and symbolic links. They are created using these 3 keyboard commands which cause a dialog to appear where the details of the link can be specified:
The two symbolic link commands are basically the same. They differ only in the fact that the paths suggested in the Symbolic Link dialog are absolute or relative. We'll demonstrate creating a symbolic link by creating a link to ![]() Symbolic link dialog Setting File Modes and OwnershipFile modes (i.e., permissions) can be set on the selected or tagged files by typing ![]() Chmod dialog To demonstrate changing file modes, we will make Deleting FilesPressing the We're done with our playground for now, so it's time to clean up. We will enter ![]() Delete confirmation dialog Power FeaturesBeyond basic file manipulation, Midnight Commander offers a number of additional features, some of which are very interesting. Virtual File SystemsMidnight Commander can treat some types of archive files and remote hosts as though they are local file systems. Using the For example, we can look at the contents of tar files. To try this out, let's create a compressed tar file containing the files in the
Once this command completes (there will be some "permission denied" errors but these don't matter for our purposes), the file Virtual file systems can also treat remote file systems as local directories. In most versions of Midnight Commander, both FTP and FISH (FIles transferred over SHell) protocols are supported and, in some versions, SMB/CIFS as well. As an example, let's look at the software library FTP site at Georgia Tech, a popular repository for Linux software. Its name is ftp.gtlib.gatech.edu. To connect with
Since we don't have write permission on this site, we cannot modify any any files there, but we can copy files from the remote server to our local file system. The FISH protocol is similar. This protocol can be used to communicate with any Unix-like system that runs a secure shell (SSH) server. If we have write permissions on the remote server, we can operate on the remote system's files as if they were local. This is extremely handy for performing remote administration. The
Finding FilesMidnight Commander has a useful file search feature. When invoked by pressing ![]() Find dialog On this dialog we can specify: where the search is to begin, a colon-separated list of directories we would like to skip during our search, any restriction on the names of the files to be searched, and the content of the files themselves. This feature is well-suited to searching large trees of source code or configuration files for specific patterns of text. For example, let's look for every file in ![]() Search for files containing "bashrc" Once the search is completed, we will see a list of files which we can view and/or edit. ![]() Search results PanelizingThere is a button at the bottom of the search results dialog labeled "Panelize." If we click it, the search results become the contents of the current panel. From here, we can act on the files just as we can with any others. In fact, we can create a panelized list from any command line program that produces a list of path names. For example, the ![]() External panelize dialog On this dialog we see a predefined list of panelized commands. Midnight Commander allows us to store commands for repeated use. Let's try it by creating a panelized command that searches the system for every file whose name has the extension
After typing the command we can either press Sub-shellsWe may, at any time, move from the Midnight Commander to a full shell session and back again by pressing The User MenuSo far we have avoided discussion of the mysterious When we press the The neat thing about this scheme is that each directory can have its own set of user menu commands, so that we can create commands appropriate to the contents of the current directory. For example, if we have a "Pictures" directory, we can create commands for processing images; if we have a directory full of HTML files, we can create commands for managing a web site, and so on. So, after we press ![]() The User Menu Editing the User MenuThe default user menu contains several example entries. These are by no means set in stone. We are encouraged to edit the menu and create our own entries. The menu file is ordinary text and it can be edited with any text editor, but Midnight Commander provides a menu editing feature found in the "Command" pulldown menu. The entry is called "Edit menu file." If we select this entry, Midnight Commander offers us a choice of "Local" and "User." The Local entry allows us to edit the Menu File FormatSome parts of the user menu file format are pretty simple; other parts, not so much. We'll start with the simple parts first. A menu file consists of one or more entries. Each entry contains:
Here is an example user menu entry that creates an HTML template in the current directory:
Notice the absence of the This command will reveal which program is actually providing the
MacrosWith that bit of silliness out of the way, let's look at how we can get a user menu entry to act on currently selected or tagged files. First, it helps to understand a little about how Midnight Commander executes user menu commands. It's done by writing the commands to a file (essentially a shell script) and then launching
Let's say we wanted to create a user menu entry that would resize a JPEG image using the ever-handy
Using the We could include some extra code to ensure that ConditionalsMidnight Commander supports two types of conditionals that affect the behavior of a menu entry. The first, called an addition conditional determines if a menu entry is displayed. The second, called default conditional sets the default entry on a menu. A conditional is added to a menu entry just before the first line. A conditional starts with either a Let's look at sub-conditions. They consist of one of the following:
pattern is either a shell pattern (i.e., wildcards) or a regular expression according to the global setting configured in the Options/Configuration dialog. This setting can be overridden by adding type is one or more of the following:
While this seems really complicated, it's not really that bad. To change our image resizing entry to only appear when the currently selected file has the extension
The conditional begins with The default menu file contains many more examples of conditionals. It's worth a look. Summing UpEven though it takes a little time to learn, Midnight Commander offers a lot of features and facilities that make file management easier when using the command line. This is particularly true when operating on a remote system where a graphical user interface may not be available. The user menu feature is especially good for specialized file management tasks. With a little configuration, Midnight Commander can become a powerful tool in our command line arsenal. Further Reading
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
© 2000-2019, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved. Linux® is a registered trademark of Linus Torvalds. |