Go to the previous, next section.

Dired, the Directory Editor

Dired makes an Emacs buffer containing a listing of a directory, and optionally some of its subdirectories as well. You can use the normal Emacs commands to move around in this buffer, and special Dired commands to operate on the files listed.

Entering Dired

To invoke Dired, do C-x d or M-x dired. The command reads a directory name or wildcard file name pattern as a minibuffer argument to specify which files to list. Where dired differs from list-directory is in putting the buffer into Dired mode so that the special commands of Dired are available.

The variable dired-listing-switches specifies the options to give to ls for listing directory; this string must contain `-l'. If you use a numeric prefix argument with the dired command, you can specify the ls switches with the minibuffer after you finish entering the directory specification.

To display the Dired buffer in another window rather than in the selected window, use C-x 4 d (dired-other-window) instead of C-x d. C-x 5 d (dired-other-frame) uses a separate frame to display the Dired buffer.

Commands in the Dired Buffer

The Dired buffer is "read-only", and inserting text in it is not useful, so ordinary printing characters such as d and x are used for special Dired commands. Some Dired commands mark or flag the current file (that is, the file on the current line); other commands operate on the marked files or on the flagged files.

All the usual Emacs cursor motion commands are available in Dired buffers. Some special purpose cursor motion commands are also provided. The keys C-n and C-p are redefined to put the cursor at the beginning of the file name on the line, rather than at the beginning of the line.

For extra convenience, SPC and n in Dired are equivalent to C-n. p is equivalent to C-p. (Moving by lines is so common in Dired that it deserves to be easy to type.) DEL (move up and unflag) is often useful simply for moving up.

Deleting Files with Dired

The primary use of Dired is to flag files for deletion and then delete the files previously flagged.

d
Flag this file for deletion.
u
Remove deletion flag on this line.
DEL
Move point to previous line and remove the deletion flag on that line.
x
Delete the files that are flagged for deletion.

You can flag a file for deletion by moving to the line describing the file and typing d. The deletion flag is visible as a `D' at the beginning of the line. This command moves point to the next line, so that repeated d commands flag successive files. A numeric argument serves as a repeat count.

The files are flagged for deletion rather than deleted immediately to reduce the danger of deleting a file accidentally. Until you direct Dired to expunge the flagged files, you can remove deletion flags using the commands u and DEL. u works just like d, but removes flags rather than making flags. DEL moves upward, removing flags; it is like u with numeric argument automatically negated.

To delete the flagged files, type x (dired-expunge). This command first displays a list of all the file names flagged for deletion, and requests confirmation with yes. If you confirm, Dired deletes the flagged files, then deletes their lines from the text of the Dired buffer. The shortened Dired buffer remains selected.

If you answer no or quit with C-g when asked to confirm, you return immediately to Dired, with the deletion flags still present in the buffer, and no files actually deleted.

Flagging Many Files

#
Flag all auto-save files (files whose names start and end with `#') for deletion (see section Auto-Saving: Protection Against Disasters).

~
Flag all backup files (files whose names end with `~') for deletion (see section Backup Files).

. (Period)
Flag excess numeric backup files for deletion. The oldest and newest few backup files of any one file are exempt; the middle ones are flagged.

% d regexp RET
Flag for deletion all files whose names match the regular expression regexp (dired-flag-files-regexp). `%d' is like % m except that it flags files with `D' instead of marking with `*'. See section Dired Marks vs. Flags.

The #, ~ and . commands flag many files for deletion, based on their file names. These commands are useful precisely because they do not actually delete any files; you can remove the deletion flags from any flagged files that you really wish to keep.

# flags for deletion all files whose names look like auto-save files (see section Auto-Saving: Protection Against Disasters)---that is, files whose names begin and end with `#'. ~ flags for deletion all files whose names say they are backup files (see section Backup Files)---that is, whose names end in `~'.

. (Period) flags just some of the backup files for deletion: all but the oldest few and newest few backups of any one file. Normally dired-kept-versions (not kept-new-versions; that applies only when saving) specifies the number of newest versions of each file to keep, and kept-old-versions specifies the number of oldest versions to keep.

Period with a positive numeric argument, as in C-u 3 ., specifies the number of newest versions to keep, overriding dired-kept-versions. A negative numeric argument overrides kept-old-versions, using minus the value of the argument to specify the number of oldest versions of each file to keep.

The % d command flags all files whose names match a specified regular expression (dired-flag-files-regexp). Only the non-directory part of the file name is used in matching. You can use `^' and `$' to anchor matches. You can exclude subdirectories by hiding them (see section Hiding Subdirectories).

Visiting Files in Dired

There are several Dired commands for visiting or examining the files listed in the Dired buffer. All of them apply to the current line's file; if that file is really a directory, these commands invoke Dired on that subdirectory (making a separate Dired buffer).

f
Visit the file described on the current line, like typing C-x C-f and supplying that file name (dired-find-file). See section Visiting Files.

o
Like f, but uses another window to display the file's buffer (dired-find-file-other-window). The Dired buffer remains visible in the first window. This is like using C-x 4 C-f to visit the file. See section Multiple Windows.

C-o
Visit the file described on the current line, and display the buffer in another window, but do not select that window (dired-display-file).

Mouse-2
Visit the file named by the line you click on (dired-mouse-find-file-other-window). This uses another window to display the file, like the o command.

v
View the file described on the current line, using M-x view-file (dired-view-file).

Viewing a file is like visiting it, but is slanted toward moving around in the file conveniently and does not allow changing the file. See section Miscellaneous File Operations.

Dired Marks vs. Flags

Instead of flagging a file with `D', you can mark the file with some other character (usually `*'). Most Dired commands to operate on files, aside from "expunge" (x), look for files marked with `*'.

Here are some commands for marking with `*' (and also for unmarking). (See section Deleting Files with Dired, for commands to flag and unflag files.)

m
Mark the current file with `*' (dired-mark). With a numeric argument n, mark the next n files starting with the current file. (If n is negative, mark the previous -n files.)

*
Mark all executable files with `*' (dired-mark-executables). With a numeric argument, unmark all those files.

@
Mark all symbolic links with `*' (dired-mark-symlinks). With a numeric argument, unmark all those files.

/
Mark with `*' all files which are actually directories, except for `.' and `..' (dired-mark-directories). With a numeric argument, unmark all those files.

M-DEL markchar
Remove all marks that use the character markchar (dired-unmark-all-files). If you specify RET as markchar, this command removes all marks, no matter what the marker character is.

With a numeric argument, this command queries about each marked file, asking whether to remove its mark. You can answer y meaning yes, n meaning no, ! to remove the marks from the remaining files without asking about them.

c old new
Replace all marks that use the character old with marks that use the character new (dired-change-marks). This command is the primary way to create or use marks other than `*' or `D'. The arguments are single characters--do not use RET to terminate them.

You can use almost any character as a mark character by means of this command, to distinguish various classes of files. If old is a space (` '), then the command operates on all unmarked files; if new is a space, then the command unmarks the files it acts on.

To illustrate the power of this command, here is how to put `*' marks on all the files that are unmarked, while unmarking all those that have `*' marks:

c * t  c SPC *  c t SPC

% m regexp RET
Mark (with `*') all files whose names match the regular expression regexp (dired-mark-files-regexp).

Only the non-directory part of the file name is used in matching. Use `^' and `$' to anchor matches. Exclude subdirectories by hiding them (see section Hiding Subdirectories).

Operating on Files

This section describes the basic Dired commands to operate on one file or several files. All of these commands are capital letters; all of them use the minibuffer, either to read an argument or to ask for confirmation, before they act. All use the following convention to decide which files to manipulate:

Here are the file-manipulating commands that operate on files in this way. (Some other Dired commands, such as ! and the `%' commands, use the same conventions to decide which files to work on.)

C new RET
Copy the specified files (dired-do-copy). The argument new is the directory to copy into, or (if copying a single file) the new name.

If dired-copy-preserve-time is non-nil, then copying with this command sets the modification time of the new file to be the same as that of the old file.

R new RET
Rename the specified files (dired-do-rename). The argument new is the directory to rename into, or (if renaming a single file) the new name.

Dired automatically changes the visited file name of buffers associated with renamed files so that they refer to the new names.

H new RET
Make hard links to the specified files (dired-do-hardlink). The argument new is the directory to make the links in, or (if making just one link) the name to give the link.

S new RET
Make symbolic links to the specified files (dired-do-symlink). The argument new is the directory to make the links in, or (if making just one link) the name to give the link.

M modespec RET
Change the mode (also called "permission bits") of the specified files (dired-do-chmod). This uses the chmod program, so modespec can be any argument that chmod can handle.

G newgroup RET
Change the group of the specified files to newgroup (dired-do-chgrp).

O newowner RET
Change the owner of the specified files to newowner (dired-do-chown). (On most systems, only the superuser can do this.)

The variable dired-chown-program specifies the name of the program to use to do the work (different systems put chown in different places).

P command RET
Print the specified files (dired-do-print). You must specify the command to print them with, but the minibuffer starts out with a suitable guess made using the variables lpr-command and lpr-switches (the same variables that lpr-file uses; see section Hardcopy Output).

Z
Compress or uncompress the specified files (dired-do-compress). If the file appears to be a compressed file, it is uncompressed; otherwise, it is compressed.

L
Load the specified Emacs Lisp files (dired-do-load). See section Libraries of Lisp Code for Emacs.

B
Byte compile the specified Emacs Lisp files (dired-do-byte-compile). See section `Byte Compilation' in The Emacs Lisp Reference Manual.

Shell Commands in Dired

The dired command ! (dired-do-shell-command) reads a shell command string in the minibuffer and runs that shell command on all the specified files. There are two ways of applying a shell command to multiple files:

What if you want to run the shell command once for each file but with the file name inserted in the middle? Or if you want to use the file names in a more complicated fashion? Use a shell loop. For example, this shell command would run uuencode on each of the specified files, writing the output into a corresponding `.uu' file:

for file in *; uuencode $file $file >$file.uu; done

The working directory for the shell command is the top level directory of the Dired buffer.

The ! command does not attempt to update the Dired buffer to show new or modified files, because it doesn't really understand shell commands, and does not know what files the shell command changed. Use the g command to update the Dired buffer (see section Updating the Dired Buffer).

Transforming File Names in Dired

Here are commands that alter file names in a systematic way:

% u
Rename each of the selected files to an upper case name (dired-upcase). If the old file names are `Foo' and `bar', the new names are `FOO' and `BAR'.

% l
Rename each of the selected files to a lower case name (dired-downcase). If the old file names are `Foo' and `bar', the new names are `foo' and `bar'.

% R from RET to RET
% C from RET to RET
% H from RET to RET
% S from RET to RET
These four commands rename, copy, make hard links and make soft links, in each case computing the new name by regular expression substitution from the name of the old file.

The four regular expression substitution commands effectively perform query-replace-regexp on the selected file names in the Dired buffer. They read two arguments: a regular expression from, and a substitution pattern to.

The commands match each "old" file name against the regular expression from, and then replace the matching part with to. You can use `\&' and `\digit' in to to refer to all or part of what the pattern matched in the old file name, as in query-replace (see section Query Replace). If the regular expression matches more than once in a file name, only the first match is replaced.

For example, % R ^.*$ RET x-\& RET renames each selected file by prepending `x-' to its name. The inverse of this, removing `x-' from the front of each file name, is also possible: one method is % R ^x-\(.*\)$ RET \1 RET; another is % R ^x- RET RET. (Use `^' and `$' to anchor matches that should span the whole filename.)

Normally, the replacement process does not consider the files' directory names; it operates on the file name within the directory. If you specify a numeric argument of zero, then replacement affects the entire absolute file name including directory name.

Often you will want to apply the command to all files matching the same regexp that you use in the command. To do this, mark those files with % m regexp RET, then use the same regular expression in the command to operate on the files. To make this easier, the `%' commands to operate on files use the last regular expression specified in any % command as a default.

File Comparison with Dired

Here are two Dired commands that compare specified files using diff.

=
Compare the current file (the file at point) with another file (the file at the mark) using the diff program (dired-diff). The file at the mark is the first argument of diff, and the file at point is the second argument.

M-=
Compare the current file with its latest backup file (dired-backup-diff). If the current file is itself a backup, compare it with the file it is a backup of; this way, you can compare a file with any backup version of your choice.

The backup file is the first file given to diff.

Subdirectories in Dired

A Dired buffer displays just one directory in the normal case; but you can optionally include its subdirectories as well.

The simplest way to include multiple directories in one Dired buffer is to specify the options `-lR' for running ls. (If you give a numeric argument when you run Dired, then you can specify these options in the minibuffer.) That produces a recursive directory listing showing all subdirectories at all levels.

But usually all the subdirectories are too many; usually you will prefer to include specific subdirectories only. You can do this with the i command:

i
Insert the contents of a subdirectory later in the buffer.

Use the i (dired-maybe-insert-subdir) command on a line that describes a file which is a directory. It inserts the contents of that directory into the same Dired buffer, and moves there. Inserted subdirectory contents follow the top-level directory of the Dired buffer, just as they do in `ls -lR' output.

If the subdirectory's contents are already present in the buffer, the i command just moves to it.

In either case, i sets the Emacs mark before moving, so C-u C-SPC takes you back to the old position in the buffer (the line describing that subdirectory).

Use the l command (dired-do-redisplay) to update the subdirectory's contents. Use k to delete the subdirectory. See section Updating the Dired Buffer.

Moving Over Subdirectories

When a Dired buffer lists subdirectories, you can use the page motion commands C-x [ and C-x ] to move by entire directories.

The following commands move across, up and down in the tree of directories within one Dired buffer. They move to directory header lines, which are the lines that give a directory's name, at the beginning of the directory's contents.

C-M-n
Go to next subdirectory header line, regardless of level (dired-next-subdir).

C-M-p
Go to previous subdirectory header line, regardless of level (dired-prev-subdir).

C-M-u
Go up to the parent directory's header line (dired-tree-up).

C-M-d
Go down in the directory tree, to the first subdirectory's header line (dired-tree-down).

Hiding Subdirectories

Hiding a subdirectory means to make it invisible, except for its header line, via selective display (see section Selective Display).

$
Hide or reveal the subdirectory that point is in, and move point to the next subdirectory (dired-hide-subdir). A numeric argument serves as a repeat count.

M-$
Hide all subdirectories in this Dired buffer, leaving only their header lines (dired-hide-all). Or, if any subdirectory is currently hidden, make all subdirectories visible again. You can use this command to get an overview in very deep directory trees or to move quickly to subdirectories far away.

Ordinary Dired commands never consider files inside a hidden subdirectory. For example, the commands to operate on marked files ignore files in hidden directories even if they are marked. Thus you can use hiding to temporarily exclude subdirectories from operations without having to remove the markers.

The subdirectory hiding commands toggle; that is, they hide what was visible, and show what was hidden.

Updating the Dired Buffer

This section describes commands to update the Dired buffer to reflect outside (non-Dired) changes in the directories and files, and to delete part of the Dired buffer.

g
Update the entire contents of the Dired buffer (revert-buffer).

l
Update the specified files (dired-do-redisplay).

k
Delete the specified file lines---not the files, just the lines (dired-do-kill-lines).

s
Toggle between sorting by file name and sorting by date/time (dired-sort-toggle-or-edit).

C-u s switches RET
Refresh the Dired buffer using switches as dired-listing-switches.

Type g (revert-buffer) to update the contents of the Dired buffer, based on changes in the files and directories listed. This preserves all marks except for those on files that have vanished. Hidden subdirectories are updated but remain hidden.

To update only some of the files, type l (dired-do-redisplay). This command applies to the next n files, or to the marked files if any, or to the current file. Updating them means reading their current status from the file system and changing the buffer to reflect it properly.

If you use l on a subdirectory header line, it updates the contents of the corresponding subdirectory.

To delete the specified file lines---not the files, just the lines--type k (dired-do-kill-lines). This command applies to the next n files, or to the marked files if any, or to the current file.

If you kill the line for a file that is a directory, the directory's contents are also deleted from the buffer. Typing C-u k on the header line for a subdirectory is another way to delete a subdirectory from the Dired buffer.

The g command brings back any individual lines that you have killed in this way, but not subdirectories--you must use i to reinsert each subdirectory.

The files in a Dired buffers are normally in listed alphabetical order by file names. Alternatively Dired can sort them by date/time. The Dired command s (dired-sort-toggle-or-edit) switches between these two sorting modes. The mode line in a Dired buffer indicates which way it is currently sorted--by name, or by date.

C-u s switches RET lets you specify a new value for dired-listing-switches.

Dired and find

You can select a set of files for display in a Dired buffer more flexibly by using the find utility to choose the files.

To search for files with names matching a wildcard pattern use M-x find-name-dired. It reads arguments directory and pattern, and chooses all the files in directory or its subdirectories whose individual names match pattern.

The files thus chosen are displayed in a Dired buffer in which the ordinary Dired commands are available.

If you want to test the contents of files, rather than their names, use M-x find-grep-dired. This command reads two minibuffer arguments, directory and regexp; it chooses all the files in directory or its subdirectories that contain a match for regexp. It works by running the programs find and grep.

The most general command in this series is M-x find-dired, which lets you specify any condition that find can test. It takes two minibuffer arguments, directory and find-args; it runs find in directory, passing find-args to tell find what condition to test. To use this command, you need to know how to use find.

Go to the previous, next section.