Friday, November 15, 2013

Midnight Commander

Midnight Commander is a console file manager and directory browser. It is a friendly system for many tasks in the terminal window, and the quickest way to copy, move or delete multiple files. MC can also do fast ftp and network file transfers. Other unique features are the ability to browse inside archives and undelete files.


Install and launch mc

Check that mc is installed in your distribution - it is no longer installed by default in some distros, such as openSUSE, but easy to do with a package manager.
    See Advanced Operations on installing from source.
   

Start Midnight Commander by typing mc in a terminal window. The main section will be the two directory panels, with a dropdown menu line above, a command line below, and below that a list of the present functions of the F (function) keys. Above the command line is a Hint line that shows random tips.
    Your mc may also open the F2 file operation menu on start-up, a little irritating, so cancel this by unticking "Auto Menus' when configuring Midnight Commander.
    To quit mc, use key F10. 



1. Installation

mc comes with most of the Linux distributions. Verify whether your system already has mc:
# rpm -qa mc
mc-4.6.2-3.pre1.fc9.i386
If you don’t have mc, install it using yum:
# yum install mc
Execute mc -V to see all compiled options on your midnight commander:
# mc -V
GNU Midnight Commander 4.6.1-pre1a
Virtual File System: tarfs, extfs, cpiofs, ftpfs, fish, undelfs
With builtin Editor
Using system-installed S-Lang library with terminfo database
With subshell support as default
With support for background operations
With mouse support on xterm and Linux console
With support for X11 events
With internationalization support
With multiple codepages support
Note: You can also install mc from source code. Download mc source code from GNU FTP site.

2. Midnight Commander layout

Launch midnight commander user interface using one of the following methods:
# mc

# mc -a
[Note: If display lines are not drawn properly, use -a]

# mc -c
[Note: Option -c will display mc in color]
Midnight commander user interface has four sections as explained below.

The Basics: Navigation in the Directory Panels

Generally, you want to display different directories either side, so you can action files between them. Navigate around mc with the keyboard:
  • Tab key to change to the opposite directory panel
  • scroll through directories with up/down arrow keys
  • Home and End to jump to the top or bottom of a long directory
  • pg-up and pg-down to scroll one screen at a time
  • back/left arrow to change to parent directory (with lynx-like motion enabled)
To change up into a parent directory, arrow up to the top line and enter on  /.. (usual parent directory notation). To change down into a subdirectory, arrow down then Enter. 
    See Configuration 
on how to enable 'lynx-like motion'. Without needing to scroll to the top, the back arrrow will change you directly into the parent directory. 

Keyboard Shortcuts

The 'F' (function) keys are widely used in mc for file operations. Read the bar at the bottom for their current function, which may differ according to the context, eg. browsing a directory, using the file viewer, or the editor.
    In normal browsing mode:
F1 - help. More readable than the 2000-line man page, although difficult to browse.
F2 - user menu ( offers option to gzip files, etc.)
F3 - view (handy to check the contents of an rpm or tgz file, or read contents of files)
F4 - edit with internal editor, mcedit
F5 - copy
F6 - rename or move
F7 - create a directory
F8 - delete
F9 - pull-down - accesses the menu bar at the top.
F10 - quit. Closes mc, as well as mcedit and any unwanted open menu.

If you don't have F keys, use Esc - number sequence (1-0) instead.
F10 key in Gnome Terminal: opens the main terminal File menu instead, so click quit with mouse. 
Other basic keyboard usages:
  • Switch back and forth to the console with Ctrl–o while keeping mc running. (also called hiding panels or subshell support.)
  • In menus, arrow up or down and Enter, or use the 'hotkey' - the highlighted letter in each menu item. 
  • Toggle tick boxes on or off with the space bar, and 
  • select multiple files with the insert key
  • Mac users without an Insert Key: Use Ctrl-t instead.
  • Tab key also moves sequentially through fields of selection boxes
All shortcuts are noted in the menus. In mc's keyboard shortcut notation, 'C-x i' would mean press Ctrl and x simultaneously, release both then press i. M refers to the Alt key. A few common shortcuts:
  • Ctrl R - refresh or rescan directory view
  • Alt-shift ? - find file
  • Ctrl-x d - compare directories (release ctrl-x before pressing d)
  • Ctrl \ - open directory hotlist
  • Ctrl-x c (o,s,l) - chmod, chown, symlink, link
  • Alt c - quick cd (opens a box, quicker to type than arrowing through if you know its full path)
  • (Shift) + - select group. Can enter wildcards to highlight a certain type or name of file. 
  • (Shift) *  - reverse selection. Changes highlighting to all unselected files. Handy to use these two in combination if you are trying to quickly select all but a certain group. 
  • \ - unselect group. Opposite of +. 

Menus

Along the top is the drop-down menu bar. F9 highlights the Left / Right menu, then arrow sideways across. (See Configuration to set drop-down menus, so you will not have to Enter to display that menu.)
    To select a menu item, arrow down and Enter, or use the hotkey - the highlighted letter.

    Browse through the menus and see what features they offer. A brief overview:
  •  Left / Right menus: refer to actions on the respective side panel. 
    • Sort Order option allows you to choose how to display directory contents - by name, size, date and case sensitive/insensitive, etc.
    • Listing Mode allows you to change how your directory is displayed. Brief: shows contents in two columns on that pane. Full: gives a standard view. Long: shows permissions, ownership and long file names in full, but removes the other pane to make space. You can also cycle through these views by using Alt - t.
    • In Listing Mode, tick "User Mini Status" which will handily display the permissions of the presently highlighted file or directory within the separate line at the bottom of the panel. 
  •  File menu: gives you a number of options to link, change ownership and permissions.
  • Command menu: has several handy operations:
    • 'Directory Hotlist' to bookmark your favourite directories, including remote machines and FTP sites, then quickly access the list with Ctrl-\ . 
    • 'Undelete files' to recover deleted files on Linux/Unix. It works on ext2 and ext3 partitions. See Advanced Operations.
    • Other useful features are showing directory sizes and comparing two directories.
  • Options: for layout, confirmation options, and Configuration to set up your Midnight Commander. 

Configure Midnight Commander

Select Options menu, Configuration. To mark or unmark the boxes, use the Space bar or mouse click. (The top left section, 'Panel Options' is a separate listing in the main Options drop-down menu in version 4.7.3. Regardless of location, Panel Options is where to select 'show hidden files' and 'lynx-like motion'.)
    Mark 'drop-down menus' - otherwise F9 will only highlight the menu name, and you will still have to enter on it to display the menu. 
    Unmark 'show hidden files' unless you want to see them, or they clutter up your directory. Also untick 'mix all files' and 'fast dir reload'.
    
In 'Pause after run', tick 'always' to enable you to read output or error messages in the terminal after executing a command.
    In the right hand menu 'Other Options' mark all - except 'auto menus' and 'safe delete'. 'Auto menus' will irritatingly open the F2 menu when launching mc, and 'safe delete' defaults to No when you press F8, so it's only if you want to be twice as careful before confirmation.
    'Lynx-like motion' is 
one of the handiest options, and enables you to enter sub- and parent directories with the forward and back arrow keys, from wherever you are in the directory. Otherwise you would have to scroll back up to the top and enter to return.
    Remember to save when you are done. 

Sometimes, too much fiddling with the configuration messed up my mc – and I couldn't restore it as I didn't know what I had done. No problem – just delete the .mc directory in your home directory, relaunch mc and reconfigure from scratch. The profile is specific to each user.

File and Directory Operations

Files and directories can be moved, copied, deleted and the contents viewed easily through the F keys - always refer to the options list underneath. If you are moving or copying files, it will first assume you are actioning to the opposite directory, giving you the option to change the destination.    
Select FilesIn the directory panels, select single files simply by arrowing to them, and select multiple files by using Insert key to highlight – and Insert again on any file to unselect. 
    You can filter for groups of files: the + key will bring up a selection box into which you can enter wildcards. For instance, when I had moved a bunch of cartoon files by mistake into my Songs directory, I selected *jpg and it automatically highlighted them all. Then F6, which moved them across to the Cartoons directory on the other side.
     
    
File OperationsView contents or edit files using the F keys, and Enter or double-click on file to execute or open it with an external program. Many of these external programs (Imagemagick, xpdf etc) may not be installed by default in your distro, so the programs to open images, pdfs and word processor docs etc. can be edited by the truly confident via Command menu, Edit Extension File.     There are two 'file' menus. The top one - F9 - is for accessing operations such as changing permissions or ownership, linking and symlinking. The 'file user' menu from the bottom bar - F2 - deals mostly with zipping and extracting files and subdirectories, and also for opening the man page.

Advanced Directory Operations
  • Find files in large directories with mc's search function by using shortcut Alt - ? (Alt+shift ?)
  • Compare the contents of two directories by opening them in panels side by side, then Command menu, Compare directories, or shortcut Ctrl-x d. This will highlight all files that are different in the two sides.
  • To view or hide hidden (dot) files, enter the configuration menu under Options and tick or untick.
  • The delete function, F8, will delete non-empty directories, after confirmation. Very handy.
The command line
Although I don't use the command line much in mc, it should execute whatever you have typed into it. If you wish to su, it will return you to the terminal to type in your password. Then relaunch mc as root; if you ctl-o, it will take you back as user.    While anything remains typed into the command line, the sideways arrow keys move through the text and  won't work to navigate in the panels. If your arrow keys suddenly don't work, check and clear the command line.
   To scroll back and forth through command history, 
use alt - p for previous and alt - n for next command instead of the the up and down arrow keys (as in the terminal window), since mc uses them for navigating.  

Using the mouse in mc – For click-click fans

The mouse works in mc, but the keyboard is handy to know when the battery goes flat in your cordless mouse... From the man page:
The Midnight Commander comes with mouse support. When you left click on a file in the directory panels, that file is selected; if you click with the right button, the file is marked (or unmarked, depending on the previous state).
    Double-clicking on a file will try to execute the command if it is an executable program; and if the extension file has a program specified for the file's extension, the specified program is executed.
    Also, it is possible to execute the commands assigned to the function key labels by clicking on them.
If a mouse button is clicked on the top frame line of the directory panel, it is scrolled one pageful backward. Correspondingly, a click on the bottom frame line will cause a scroll of one pageful forward. This frame line method works also in the Help Viewer and the Directory Tree.
If you are running the Commander with the mouse support, you can bypass the Commander and get the default mouse behavior (cutting and pasting text) by holding down the Shift key.  
If your mouse does not work in mc, check that gpm mouse server is installed and running. 

Accessing Contents of Archives

All sorts of archives - RPM, deb, tgz, iso, rar, cpio etc. are accessible with mc. The individual files can be viewed and extracted without needing to unpack or install - even password-protected archives.
    Certain types of archive files need other packages installed to look inside: eg. to access .deb on an rpm-based system, install package deb which includes dpkg, etc. And to access iso files, I needed package cdrkit-isotools 
(may be installed by default on your distro, but not on PCLinuxOS).

To see an overall view, F3 directly onto the file. 
In this screen, the F keys have  different functions again. To quit this screen, use F10 (or click on quit at the bottom, if F10 instead activates a menu from your Gnome terminal).
To access the contents, Enter onto the file, then drill down into the contents, where you can now read the text files with F3 or copy out individual files. This is very handy for rpms, where you can copy out a needed library, for instance, in a package  you can't install due to conflict or dependency issues. 
Zip archives can be similarly viewed and files browsed and copied before unzipping (unzip .zip by command line only - the F2 unzip function only works with .gz or .bz2 extensions. This may be enabled in future versions).
    The F2 file user menu also gives options to tar or zip files in a directory.

Install RPM and tgz

RPM: If not already root, su and relaunch mc. Enter on the rpm and select install or upgrade. I only figured out how to install them singly, though, so for multiple rpm's use the command line.
    If you have configured mc to always pause after run, the terminal will show you the output. Otherwise, it flashes back to mc after running and you won't know you have an error message unless you ctl–o to check in the terminal window.
TGZ: To extract a tarball, F2 for a file user menu, then x to extract. You can cd to the extracted directory, then ./configure, make and make install from the command line, as per the INSTALL file instructions (which you read with F3), although there remains the problem of having to su to root halfway when you start as user. 

Editing files with mcedit

MC's integral editor is easy to use, and even if you're not in mc, you can start it directly from the command line: mcedit <filename>. 
    In mc, open a file to edit with F4. Once finished, F2 to save and F10 to quit (or click quit at the bottom). If it won't save your file, that means you forgot to edit it as root and now you're going to have to su and do it all over again! (Remember to relaunch mc once you are root.)
    Note the F keys have slightly different uses in the editor - refer to the function bar at the bottom. 
    Mouse highlighting to copy and paste works similarly to the terminal, but use the shift key at the same time: shift, highlight, then shift and paste with the middle button. Or, paste other items on your clipboard with Shift-Insert.
    A shortcut to jump to the top or bottom of a long file while editing is Ctl-home or Ctl-end. 

Using mc to FTP

MC can ftp via the command line, the Left / Right menu, or to a site you have 'bookmarked' in your directory hotlist. 
    If you access the site regularly, add it to your directory hotlist for fastest access. Go to Command menu - Directory Hotlist - 
add by either typing it in, or if you are connected in a panel already, simply Add Current. Access the list with Ctl - \ .
To disconnect ftp, type cd in the command line and it will return you to your home directory.
Command line: For anonymous sites, the format is cd ftp://ftp.yoursite.com
To connect to a site with a password (non-anonymous),
cd ftp://username:password@ftp.yoursite.com
   (This may return an error message if you did not specify a directory, but will connect you anyway.)
    Or type in the full path: 
cd ftp://username:password@ftp.yoursite.com/public_html 
Left / Right dropdown menu: will connect on that respective side. Arrow down to FTP link, or use hotkey P. A dialog box will request the FTP address in the same format:
Anonymous: ftp.yoursite.com
Non-anonymous: username:password@ftp.yoursite.com/public_html
For all methods, the username alone can be entered, eg. username@ftp.yoursite.com and and a box will request the password. If the command line is used, this is more secure, as the password may be read in plain language when scrolling back through the command line history. 
What if you have @ in the username? Originally, support for this was missing. Try enclosing the username in double quotation marks "user@name":password@ftp.yoursite.com
    If, however, the username itself includes ftp, such as "ftp@www.yoursite.com" then mc will not connect. Either use another ftp client, or this workaround: 
  • In your home directory, create a .netrc file: touch .netrc && chmod 600 .netrc
  • open it for editing: mcedit .netrc 
  • add the following line to the file, with your own details in place of italics:
  • machine ftp.yoursite.com login ftp@www.yoursite.com password password
  • F2 to save and F10 to quit, or simply F10 and enter 'yes' when asked if you want to save on exit.
Now connect to the site using the same format as an anonymous connection, and it will log you in instantly.

Change ftp configuration settings under Options menu, Virtual FS. This is where you can change the anonymous password, use of .netrc file, passive mode and proxy. 

Networking with Samba

It would appear that the latest versions of mc don't have samba support, but it is planned to be included again in the future. If your version supports samba:
    In the same way as with ftp, select Left / Right menu and SMB link (or hotkey B), and a box will open for the machine name, into which I entered the IP.AD.DR.ES. Or, command-line format: cd smb://IP.AD.DR.ES/
    I'm not a samba expert, so for more commands consult the man page. 

Advanced operations

Installing from Source: First, put on your patient hat! Read the INSTALL and README files carefully, especially for the list of libraries that must be pre-installed, to avoid unnecessary gnashing of teeth. If you install binary (rpm) versions of the libraries, note that many also require the -devel packages.
    The INSTALL file also gives a list of configure options - many common ones are on by default, but samba and undelete must be selected on.
    And, when I installed version 4.7.1, the binary landed up in a strange place well out of the normal path.

CD burning: A cd burning patch, MC-Burn, is available from Friesoft, but only as a tgz.

Undeleting files

MC can recover deleted files on Linux/Unix. Works on ext2 and ext3 filesystems. 
  • su to root and relaunch mc
  • it is recommended to unmount the affected partition: umount /dev/sda2 (your partition)
  • access Command menu, Undelete files and enter your partition in the box
  • after a few moments search, it will show a directory containing the inode numbers (the file contents, missing the header or filename)
  • Sort and arrange by time, size etc. through Left / Right menu, Sort Order
  • F3 to view the contents of a file
  • Copy required files over to a new directory on your mounted filesystem
  • if unmounted, remount partition: mount /dev/sda2 
  • rename and move your recovered files back. 
More help: For a detailed guide by the developers of Midnight Commander, see here.
Official FAQ here.




Mc from Hai Thai Viet

Hotkeys

Ctrl and Shift mean the same keyboard keys, Meta is metakey, on PC it's Alt or single Esc press.
F3Begin of text selection. Second press - end of text selection
Shift+F3Begin of block text selection
F5Copy text selection
F6Move text selection
F8Delete text selection
Meta+itoggle 'Return does autoindent' option.
Meta+lGo to line by number.
Meta+qInsert literal.See table at bottom of this page.
Meta+tSort of text in selection.
Meta+uExecute external command and insert output into cursor position
Ctrl+fSave selection into external file
Ctrl+kDelete chars to end of line
Ctrl+nCreate new file
Ctrl+sToggle syntax highlighting
Ctrl+tSelect text charset
Ctrl+uUndo
Ctrl+xGo to end of next word
Ctrl+yDelete line
Ctrl+zGo to start of previous word
Shift+F5Insert external file content into cursor position
Meta+EnterCodejump dialog
Meta+-Move back into codejump (go to from function definition to function call)
Meta++Move forvard into codejump (go to from function call to function definition)
Meta+nToggle number of lines
tabIf 'persistent blocks' option is on, then move selected text to right
M-tabIf 'persistent blocks' option is on, then move selected text to left
Shift+ArrowsText selection
Meta+ArrowsBlock text selection
Meta+Shift+-Toggle show visible tabs and trailing spaces

Hotkey for panel

Ctrl and Shift mean the same keyboard keys, Meta is metakey, on PC it's Alt or single press of Esc key. Fn (Where N > 10) mean Shift-F(n-10), i.e. F19 is Shift-F9.
F3View file.
F4Edit file.
F13View raw file without extension specific.
F14Create new file.
F19Activate last used menu element.
F20Quiet exit, without confirmation.
InsertSelect 'current object'1
+select a group of files. (regular expression can be used)
\Unselect a group of files. This is the opposite of the Plus key.
Meta+EnterInsert 'current object'1 to command line.
Meta+.Show/hidde hidden files and directories (that starts with '.').
Meta+,Change panel split view mode to vertical/horizontal.
Meta+a
Ctrl+x,p
Insert in command line path of active panel.
Meta+cDisplays change directory dialog for active panel.
Meta+hDisplays command history.
Meta+iMake the current directory of the current panel also the current directory of the other panel. Put the other panel to the listing mode if needed. If the current panel is panelized, the other panel doesn't become panelized.
Meta+n
Meta+p
Use these keys to browse through the command history. Meta-p takes you to the last entry, Meta-n takes you to the next one.
Meta+oIf the currently selected file is a directory, load that directory on the other panel and moves the selection to the next file.
Meta+g
Meta+r
Meta+j
Used to select the top/middle/bottom file in a panel repectively
Meta+tChange panel view ('Full','Brief','Long')
Meta+Shift+?Find file.
Meta+Shift+A
Ctrl+x,Ctrl+p
Insert to command line inactive panel path.
Meta+Shift+HDisplay directory history.
Ctrl+\Display directory hotlist.
Ctrl+lRepaint all information in midnight commander.
Ctrl+oShow/hida panell.
Ctrl+rReread content of current directory.
Ctrl+sQuick search.
Ctrl+SpaceShow directory size.
Ctrl+x,aShow active VFS list.
Ctrl+x,cChmod (Change file Mode) for file or directory.
Ctrl+x,iSet the other panel display mode to information.
Ctrl+x,jShow background jobs.
Ctrl+x,lCreate hardlink for active file or directory.
Ctrl+x,oChown (Change Owner) for file or directory.
Ctrl+x,qSet the other panel display mode to quick view.
Ctrl+x,sCreate symlink for active file or directory.
Ctrl+x,tInsert selected names to command line.
Ctrl+x,Ctrl+sEdit symlink.
deprecated or future hot-keys
Shift+F1Change directory to mount point for left panell (should work in further release).
Shift+F2Change directory to mount point for right panell (should work in further release).