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.
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.
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.
The primary use of Dired is to flag files for deletion and then delete the files previously flagged.
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.
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).
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).
dired-find-file
). See section Visiting Files.
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.
dired-display-file
).
dired-mouse-find-file-other-window
). This uses another window
to display the file, like the o command.
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.
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.)
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.)
dired-mark-executables
). With a numeric argument, unmark all
those files.
dired-mark-symlinks
).
With a numeric argument, unmark all those files.
dired-mark-directories
). With a numeric
argument, unmark all those files.
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.
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
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).
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.)
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.
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.
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.
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.
dired-do-chmod
). This uses the chmod
program, so
modespec can be any argument that chmod
can handle.
dired-do-chgrp
).
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).
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).
dired-do-compress
).
If the file appears to be a compressed file, it is uncompressed;
otherwise, it is compressed.
dired-do-load
).
See section Libraries of Lisp Code for Emacs.
dired-do-byte-compile
). See section `Byte Compilation' in The Emacs Lisp Reference Manual.
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:
tar
on the entire
list of file names, putting them into one tar file `foo.tar'.
uudecode
on each
file.
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).
Here are commands that alter file names in a systematic way:
dired-upcase
). If the old file names are `Foo'
and `bar', the new names are `FOO' and `BAR'.
dired-downcase
). If the old file names are `Foo' and
`bar', the new names are `foo' and `bar'.
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.
Here are two Dired commands that compare specified files using
diff
.
diff
program (dired-diff
). The
file at the mark is the first argument of diff
, and the file at
point is the second argument.
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
.
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:
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.
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.
dired-next-subdir
).
dired-prev-subdir
).
dired-tree-up
).
dired-tree-down
).
Hiding a subdirectory means to make it invisible, except for its header line, via selective display (see section Selective Display).
dired-hide-subdir
). A numeric argument serves
as a repeat count.
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.
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.
revert-buffer
).
dired-do-redisplay
).
dired-do-kill-lines
).
dired-sort-toggle-or-edit
).
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
.
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
.