Skip to main content

Midnight Commander [mc]

Miguel de Icaza's Midnight Commander (mc) README describes mc as: a free Norton Commander Clone with many useful features... The Midnight Commander is a directory browsing tool which bears a certain remote resemblance to John Socha's Norton Commander for DOS.

image-1639342735087.png  Introduction

From https://midnight-commander.org/

GNU Midnight Commander is a visual file manager, licensed under GNU General Public License and therefore qualifies as Free Software. It's a feature rich full-screen text mode application that allows you to copy, move and delete files and whole directory trees, search for files and run commands in the subshell. Internal viewer and editor are included.

Midnight Commander is based on versatile text interfaces, such as Ncurses or S-Lang, which allows it to work on a regular console, inside an X Window terminal, over SSH connections and all kinds of remote shells.

image-1639342885227.png

Installation
apt-get install mc
Navigation
  • down arrow, Ctrl-n - move forward one line
  • up arrow, Ctrl-p - move backward one line
  • Page Down, Ctrl-v - move back one screen
  • Page Up, Alt-v - move backward one screen
  • Home, Alt-< - move to the top of the directory listing
  • End, Alt-> - move to the end of the directory listing
Tagging of multiple files

The simplest is to either click on the file or directory using the right mouse button or move the highlight bar to the file and hit [INSERT] or [CTRL+T] to tag any number of files for copy, deletion, moving, and so forth.

If the files you want can be specified by a shell pattern (such as *.tar.gz for all the gzipped tar files or foo_??.txt for foo_01.txt, foo_02.txt, foo_03.txt, etc., then you can use the following shortcuts:

"+" key

Select a group of files by pattern. This will cause a dialog box to appear in which you can enter the search pattern. Note that entering the "*" wildcard will select all the files in the directory.

"*" key

Select all the files in the directory. This has the same effect as using the + key and then entering "*"

"\" or "-" key

Unselect a group of files. A dialog box similar to that with the + key will appear; if you want to unselect all files then enter "*"
Using pathname expansion (also known as filename globbing) is a fast and powerful way to select a group of similar files.

Basic File Operations

The Midnight Commander provides all of the basic UNIX file system operations including copy, delete, move, rename, link, symbolic link, chown, view, and edit. One of the nice features of mc is that it defaults to asking for confirmation before taking destructive actions such as file deletion or overwriting.This feature alone can save you a good deal of agony.

copy: F5

Normally, to copy a file from one directory to another select or tag the file(s) to copy in the current directory panel and change to the target directory in the other directory panel. Hit F5 or click on the Copy function at the bottom. If you want to use a different directory than the one in the other panel or if you want to change the file name then you can use the to: entry box to do so.

Also, when you perform a copy (and move or delete) operation mc displays a dialog box with a progress meter indicating the progress on the current file as well as the overall progress if a set of files has been selected.

delete: F8

After selecting or tagging a file or set of files hit F8 to delete them. Note that you'll be asked for confirmation. Also, mc is able to recursively delete a directory, but this, too, requires confirmation. If you're having to clean out a directory then I'd strongly suggest your using mc. It makes selecting files very easy and helps prevent costly accidental deletions.

Note that at any time you can cancel an operation by hitting the Escape key twice.

move/rename: F6

These are very similar to the copy operation described above in that the destination directory defaults to the non-current directory in the case of the move operation. Note that renaming a file entails "moving" it to another filename. In this case, use the to: entry field in the dialog box to enter the new filename.

(hard) link: Ctrl-x l

To create a hard link to a file, select that file and enter Ctrl-x followed by the letter "l". A dialog box will prompt you for the filename for the newly created link.

symbolic link: Ctrl-x s

This is the same as that for a hard link except Ctrl-x is followed by the letter "s".

chown: Ctrl-x o

To change the owner or group of a file hit Ctrl-x followed by an "o". A dialog box is presented that will allow you to select owner and group from a list of valid values.

Note that an "Advanced Chown" facility is available under the "File" menu. 

mkdir: F7

While not a file operation per se, the MkDir command allows you to create a new directory in the currently active directory. A dialog box is presented that will prompt you for the new directory name.

File Viewing and Editing

The Midnight Commander comes with a very functional built in file viewer and text/hex editor. To view or edit a file hit the F3 or F4 key respectively. Of particular note is the fact that mc provides formatting support for several common file formats including manual pages, HTML, and mail file. That is, that rather than viewing the raw file, mc will format the file according to the file format.

For example, to view a manual page (even a gzipped page!) simply select the file and hit F3. If you have the mc source distribution handy, change to the doc directory and select the mc.1 file. Hit F3 to see:

You can do similar things with HTML or mail files. In the case of HTML files it is worth noting that "viewing" the file is probably not what you expect as mc will strip out the hypertext tags leaving just the text. If you want to view an HTML file it is best to select the file and hit the RETURN key. Doing so "opens" the file and automatically executes (by default) lynx if you are at a console or netscape if you are running under X.

The internal file view allows you to view files in one of two modes: ASCII or hex. When using the file viewer you'll notice that the function keys at the bottom change to a new set which are specific to the viewer. These include:

  • F2 toggles line wrapping
  • F3 exit the viewer
  • F4 toggle hex view mode
  • F5 goto line; a dialog box appears which will prompt you for a line number
  • F6 regular expression search
  • F7 normal search
  • F8 toggle raw or parsed mode
  • F9 toggle formated or unformatted mode: this affects attributes such as bold or underline which are displayed using various colors
  • F10 exit the viewer

In addition Ctrl-s and Ctrl-r can be used for normal or reverse searches. Once you've started a search, hit the letter n to find the next match. Ctrl-l will repaint the screen; Alt-r will toggle the display of a ruler.

In terms of moving around the viewer, mc has a rather egalitarian attitude and will accommodate almost any set of movement keystrokes that you've gotten used including those for emacs, less, and even some vi. Some of these are:

  • Home, Alt-< - move to head of file
  • End, G, Alt-> - move to end of file
  • up/down arrows - move forward or backward a single line
  • Ctrl-n, Ctrl-p - move forward or backward a single line
  • left/right arrows - scroll horizontally
  • Space, Page Down - move forward one page
  • b, Page Up - move back one page

One very handy feature is that, if you are in View mode and hit Ctrl-f then the viewer will move to the next file in the directory and display it. In this way you can easily move through a set of files, viewing one right after the other.

Using mc's Panels

We've already noted that the directory panels are where most of the action takes place in terms of file operations. Accordingly there are a number of keystroke (and mouse) shortcuts that quickly let you change the panel view. In addition to directory listings, the panels can be quickly changed to display directory trees, quick file views, or extensive file information. If you spend much time at all working with files these features are invaluable.

Basic Panel Operations

As previously noted, you can quickly switch from one panel to the other using the TAB key (or Ctrl-i). You can also swap panels using Ctrl-u; note that the currently active directory panel does not change. Use Ctrl-r to refresh the directory display.

To change the sort order of the files being displayed, use the (Left|Right)->Sort Order... menu item. This allows you to sort files by name, size, various time stamps, inode number, and so forth. You can also specify whether sorting should be case (in)sensitive or reversed. Sorting by size is very useful when trying to cull out files to recover disk space; sorting by date is useful when you are searching for a recently installed, created, or modified file in a directory with many files or are looking for ancient files that can safely be warehoused.

As with sorting, use the (Left|Right)->Filter... menu item to filter the directory listing using shell patterns. For instance, suppose that you wanted a listing containing only files with a .c extension. In the Filter dialog simply enter "*.c" and all other files are removed from the listing. This is very useful when you wish to work with only a subset of files in a directory in an uncluttered setting.

You can also cycle from two-panel to single-panel modes using Alt-t. This is particularly useful when you need to see the full directory information for a particular file. Note that you can also use the (Left|Right)->Listing Mode... menu item to customize what file information the panel lists. In addition, resize the panels using the Options->Layout... menu item. This allows you to split the panels either vertically or horizontally as well as set the number of columns for each panel using the ">" and "<" keys.

Panel Modes

Here's where the power of mc becomes apparent. The panels can be used in one of four modes which can be switched using the (Left|Right) menus:

Listing Mode...

This is the default directory listing mode. As noted above, you can customize what file information (name, permissions, time stamps, etc) is displayed

Quick View...

Provides a quick file viewer; it displays the file which has been selected in the other panel. This is very handy if you need a quick preview of a set of files. Note that the function key mappings at the bottom of the window display the usual viewer operations: all of the viewer features and keystrokes are available here. When previewing a set of files try increasing the size of the viewing panel using the Options->Layout... menu item.

Info

This works in a manner similar to Quick View: it displays a wealth of file or directory information.

Tree

As with Quick View and Info, the Tree view works with the other panel: it allows you to quickly navigate through the entire directory tree. By double clicking on a directory (or selecting it and hitting RETURN) its contents are displayed in the other panel
One final shortcut to be aware of is Alt-o which makes use of both panels: by selecting a directory in the active panel and hitting Alt-o, its directory listing is displayed in the other panel. Hitting Alt-o repeatedly lets you quickly preview through a series of directories.

VFS "Virtual File System"

VFS is a powerful abstraction that allows you to view archives as though they were a directory: all the basic file manipulation operations can then be applied. The VFS file system handles an extraordinary number of archive types including tar, gzipped or compressed tar, RedHat's rpm package archives, Debian's deb package archives, gzip, zip, zoo, cpio, rar, and lha.

To use it either select the file and hit RETURN or double click on the file. It's contents are then displayed as a directory listing. Navigation through the archive is the same as you'd use for a directory. This is a very useful feature when you need a single file or set of files from an archive. Note that if the archive is a compressed single file -- i.e., a gzip, zip, zoo, or lha compressed file -- then it is uncompressed and displayed.

FTP Client

The VFS also supports its own FTP capacity which allows you to transparently manipulate files via FTP as though they were local to your machine. To log into an FTP server use the (Left|Right)->FTP Link... menu item and enter the URL or simply enter cd ftp://"URL" at the command line. For example, to ftp to the Linux Incoming directory at sunsite you would enter:

$ cd ftp://sunsite.unc.edu/pub/Linux/Incoming/

The hintbar at the console or the title bar under X will display progress information (e.g., logging in, retrieving directory listings, and so forth). You can now view and copy files just as you would using ftp. On file transfer (use F5 to "Copy" the file to your local machine) a progress meter displays percent transfer completed, ETA (estimated time of arrival), transfer rate, and the now commonplace "stalled" flag. Use the Options->Virtual FS... menu item to customize the VFS features such as anonymous login name and so forth.

image-1639345040131.png

Note that mc also provides FTP service via a proxy server as well as network VFS. 

A number of popups are built into mc that considerably speed up various operations. These include:

Directory Hotlist: Ctrl-\

Use this to create a popup hotlist of frequently accessed directories. The hotlist keeps a list of "alias-directory path" pairs. For example, if you use /usr/local/projects/C++/personal/ to store your personal programming projects then you could create an entry using the "New Entry" option and alias it to something like "Personal C++ Projects".
If you use mc as an ftp client then you can use the directory hotlist to keep the URL's for your frequented sites! To edit (add, modify, or delete entries) the list type in Ctrl-\ and then use "New Entry" to create a new entry: enter the URL for the site, including the path to the directory that you're interested in and then fill in the alias. Now, anytime that you need to ftp just popup the hotlist and select the site!

Filename Search: Ctrl-s, Alt-s

At the bottom of each directory panel is a mini-status box which lists the currently selected file or directory name. By hitting Ctrl-s or Alt-s the status box becomes an entry box in which you type in a file or directory name to search for. As you type, mc does an incremental search and automatically selects the file which matches the search. This is very handy when searching for a particular file in a large directory.

Find File: Alt-?

Find File provides an exceptionally fast and useful means of searching for a particular file based on a filename pattern, its contents, or both! For example, suppose that you kept a directory with USENET postings that you wanted to save and you're now looking for information on mysql. Use Find File with the "*" pattern for the filename and "mysql" for its content. It will then grep through the files and create a list of files containing "mysql". Clicking on one of the files in the list selects that file.
Alternatively, if you were looking for all files with "announce" in the filename simply enter "*announce*" in Filename: (and leave the Content: entry box empty).

Active VFS Directories: Ctrl-x a

This pops up a dialog box listing all of the active VFS directories including those associated with FTP, archives, and so forth. When mc creates a VFS directory for an archive, network, or ftp connection, when you leave the directory the connection or resources are not immediately freed. So, for example, if you created an ftp connection to some host, you "close" it simply by changing back to a local directory. Until the connection times out (which you can set using the Options->Virtual FS...->Timeout for freeing VFS's menu item) you can reestablish the connection once again. This also works for archives (in this case, after the timeout, the memory resources are deallocated).

Background Jobs: Ctrl-x j

mc allows you to put copy and move operations into the background (select this option from the Copy or Move dialog box) which allows you to immediately return to mc. This is useful when you are copying/moving a large number of files or for ftp. Use the Background Jobs popup to quickly see what jobs are running and stop, restart, or kill them. Another very handy feature which mc provides is subshell support. The way this works is by hitting Ctrl-o which creates a non-login interactive shell. This works for bash, tcsh, and zsh shells. Use this shell just as you would any ordinary shell. To immediately switch back to mc hit Ctrl-o once again, which allows you to toggle back and forth easily.

If you are using the bash shell, keep in mind that non-login interactive shells only source your ~/.bashrc file (and not the ~/.bash_profile file) which means that if you have aliases or other customizations that you want to use then you should put these in ~/.bashrc. For example, if you use color-ls and find that file listings are not colorized, then you'll need to add alias ls='ls --color=tty' to your ~/.bashrc.

Sync the contents of two directories: Ctrl-x d.

One last feature I'd like to mention is mc's ability to help you sync the contents of two directories. This is particularly useful if you are keeping a backup set of files on another partition, a floppy, zip drive, etc. To use this list the "source" directory in one panel and the "target" directory in the other then hit Ctrl-x d. This will pop up a dialog box that allows you to select the type of directory comparison: Size simply compares files by size; Quick compares files by size and date; and Thorough does an exhaustive byte-by-byte comparison. After the comparison operation is complete (and after ensuring that the source directory is in the active directory panel) hit F5 (Copy) to copy files from your source directory to the target (backup) directory.

All-In-One-Command-Center - AIOCC

One way to quickly create the "all-in-one-command-center" is execute mc and then start a subshell. From here, you can execute your favorite editor (emacs, xemacs, vim, etc.) and hit Ctrl-z to stop its execution and put it in the background. This returns you to the shell. Now, if you need to run mc then hit Ctrl-o; if you need to use your editor, type in fg which will resume the stopped program; and if you need to run any other program then use the shell as normal. This is a powerful means of keeping productivity tools readily available.

Credit: Based on An Introduction to the Midnight Commander by by John M. Fisk <fiskjm[at]ctrvax.vanderbilt.edu>.