File Management with Emacs

Xah Lee, 2006

Emacs is a excellent tool for file management, such as viewing directories, creating files, copying files, moving files, creating directory, deleting files or directory. Once you become familiar with it, you almost never go back to shell or your operating system's file viewer for these tasks.

emacs dired

above: A Screenshot of emacs's feature for file management.

Copy, Delete, Rename a File

To start viewing directory, type “Ctrl+x d”, then type the dir you want. (Or, use the graphical menu “File‣Open Directory...”.) You can now use arrow keys to move around. To open a file, just move cursor to it and hit enter. To view a subdir, just move cursor to it and enter. To close the current dir, type “q”. To copy a file, move cursor to it and type “C”. To rename a file, type “R”. To delete a file, type “D”. Typing “Z” will compress (gzip) the file the cursor is on. (These are under the graphical menu “Operate”)

the Operate menu in Emacs's dired mode

above: Emacs's dired mode's Operate menu.

Operate On A Set Of Files

Sometimes you want to copy or delete a set of files. Instead of using the command on one file at a time, you can simply mark the files you want, then execute the command once for all.

To mark a file, type “m”. To unmark a file, press “u”. Type “U” to unmark all marked files. To mark all files, first type “U” to unmark all, then type “t” for toggle. You can also mark files by a regular expression, by typing “% m”, then type the pattern. For example, to mark all files ending in “.html”, type “%”, “m”, “\.html”, “RET”.

General Info

The command bound to “Ctrl+x d” is called “dired”, short for directory edit. Here's a list of the most commonly used keystrokes within dired:

keystroke    purpose
-------------------------------
+            create a dir

R            rename file
C            copy file
D            delete file
Z            compress/decompress the file

M            change a file's mode
O            change a file's owner

m            mark a file
u            unmark a file
U            unmark all
t            toggle marks

g            update dir listing

“dired” is often used in combination with “Alt+!” (shell-command). For example, suppose you downloaded a program and is going thru the install process of unzip, untar, configure, make, sudo make install. You can either run these command inside a emacs shell with “Alt+x shell”, or you can just execute them one at a time with “Alt+!”. (which method to use is a matter of preference)

Once you are in dired mode, you will have 3 new graphical menus: “Operate”, “Mark”, “Regex”. These menus lists the commands specific to dired. They are very useful as a cheatsheet. So, if you forgot what's the key to press to do something, look up in these menus.

Reference: (info "(emacs)Dired").

Note: emacs 22 by default does not let you do copy or delete on a directory. If you want to do that, put the following in your emacs init file:

(setq dired-recursive-copies 'always)
(setq dired-recursive-deletes 'top)

“'always” means don't ask. “'top” means ask for each.


Related essays:


Page created: 2007.
© 2007 by Xah Lee.
Xah Signet