Emacs Advent Calendar 15: File Management with Dired

Tue 2015-12-15

Emacs includes dired, a mode with file manager functionality. As this is quite a big topic, today's entry can only be considered an introduction. Read the Reference Manual for the full documentation.

Dired is started with C-x d or by visiting a directory with C-x f. Because ls is used for listing the directory contents you can specify its command line options by starting dired with a prefix argument (C-u C-x d); e.g. C-u C-x d -alh <RET> to get human readable file sizes. To reload the directory content, e.g. after you made some changes outside Emacs, press g.

Movement in a dired buffer uses the standard keybindings; in addition n and <SPC> move to the next line, p moves to the previous line.

To flag a file for deletion, press d on the correspondig line. To unflag a file, press u or <DEL>. When you are sure that the right files are flagged, press x to execute the deletion command (it will list the files and ask for confirmation before actually deleting anything).

There are commands to automatically flag files you might want to delete: ~ for backup files (ending in ~), # for autosave files (surrounded by #) and some more about which you can read in the reference manual.

Visiting files from dired is straightforward, just press <RET> (or f or e) on a file name. To open a file in another window press o or click it with Mouse-1. To move up one directory visit the .. folder or press ^.

While deletion operates on flagged files, all other commands act on files that are marked. To mark one file, press m. Unmarking works like unflagging with u or <DEL>, upper case U unmarks all files. Files can be marked according to various criteria (again, see the Manual), for example * / to select all subdirectories.

Key Action
C copy one or multiple files (like cp)
R rename file or move multiple files (like mv)
M change permissions
G change group of file
O change file ownership
P print file
Z (un-)compress file

You can run shell commands on the selected file with ! (synchronously) and & (asynchronously). See the Manual for how the command can be applied to multiple files.

There are many more features of dired left for you to explore in the manual as needed (systematic file renaming, diff, version control, image preview among others).

For full documentation see chapter 30 of the Emacs Reference Manual (Dired, the Directory Editor).

Tags:

This text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.