[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
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.
The Dired-X package provides various extra features for Dired mode. See section `Top' in Dired Extra Version 2 User's Manual.
AB.1 Entering Dired How to invoke Dired. AB.2 Navigation in the Dired Buffer Special motion commands in the Dired buffer. AB.3 Deleting Files with Dired Deleting files with Dired. AB.4 Flagging Many Files at Once Flagging files based on their names. AB.5 Visiting Files in Dired Other file operations through Dired. AB.6 Dired Marks vs. Flags Flagging for deletion vs marking. AB.7 Operating on Files How to copy, rename, print, compress, etc. either one file or several files. AB.8 Shell Commands in Dired Running a shell command on the marked files. AB.9 Transforming File Names in Dired Using patterns to rename multiple files. AB.10 File Comparison with Dired Running `diff' by way of Dired. AB.11 Subdirectories in Dired Adding subdirectories to the Dired buffer. AB.12 Moving Over Subdirectories Moving across subdirectories, and up and down. AB.13 Hiding Subdirectories Making subdirectories visible or invisible. AB.14 Updating the Dired Buffer Discarding lines for files of no interest. AB.15 Dired and find
Using `find' to choose the files for Dired.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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
before you enter 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.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
Some additional navigation commands are available when the Dired buffer includes several directories. See section AB.12 Moving Over Subdirectories.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
One of the most frequent uses of Dired is to first flag files for deletion, then delete the files that were flagged.
You can flag a file for deletion by moving to the line describing the
file and typing d (dired-flag-file-deletion
). 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 variable dired-recursive-deletes
controls whether the
delete command will delete non-empty directories (including their
contents). The default is to delete only empty directories.
The files are flagged for deletion rather than deleted immediately to
reduce the danger of deleting a file accidentally. Until you direct
Dired to delete the flagged files, you can remove deletion flags using
the commands u and DEL. u (dired-unmark
) works
just like d, but removes flags rather than making flags.
DEL (dired-unmark-backward
) moves upward, removing flags;
it is like u with argument -1.
To delete the flagged files, type x (dired-do-flagged-delete
).
(This is also known as expunging.)
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.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The #, ~, &, and . commands flag many files for deletion, based on their file names. These commands are useful precisely because they do not themselves delete any files; you can remove the deletion flags from any flagged files that you really wish to keep.
& (dired-flag-garbage-files
) flags files whose names
match the regular expression specified by the variable
dired-garbage-files-regexp
. By default, this matches certain
files produced by TeX, `.bak' files, and the `.orig' and
`.rej' files produced by patch
.
# (dired-flag-auto-save-files
) flags for deletion all
files whose names look like auto-save files (see section M.5 Auto-Saving: Protection Against Disasters)---that
is, files whose names begin and end with `#'.
~ (dired-flag-backup-files
) flags for deletion all files
whose names say they are backup files (see section M.3.1 Backup Files)---that is, files
whose names end in `~'.
. (period, dired-clean-directory
) 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 AB.13 Hiding Subdirectories).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 M.2 Visiting Files.
dired-find-alternate-file
).
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 O. 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.
dired-up-directory
). This is more convenient than moving to
the parent directory's line and typing f there.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Instead of flagging a file with `D', you can mark the file with some other character (usually `*'). Most Dired commands to operate on files use the files marked with `*', the exception being x which deletes the flagged files.
Here are some commands for marking with `*', or for unmarking or operating on marks. (See section AB.3 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-mark-subdir-files
).
dired-unmark
).
dired-unmark-backward
).
dired-unmark-all-marks
).
dired-unmark-all-files
). The argument is a single
character--do not use RET to terminate it. See the description
of the * c command below, which lets you replace one mark
character with another.
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, or ! to remove the marks from the remaining files without asking about them.
dired-next-marked-file
)
A file is "marked" if it has any kind of mark.
dired-prev-marked-file
)
dired-do-toggle
): files marked with `*'
become unmarked, and unmarked files are marked with `*'. Files
marked in any other way are not affected.
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-markchar is a space (` '), then the command operates on all unmarked files; if new-markchar is a space, then the command unmarks the files it acts on.
To illustrate the power of this command, here is how to put `D' flags on all the files that have no marks, while unflagging all those that already have `D' flags:
* c D t * c SPC D * c t SPC |
This assumes that no files were already marked with `t'.
dired-mark-files-regexp
). This command is like
% d, except that it marks files with `*' instead of flagging
with `D'. See section AB.4 Flagging Many Files at Once.
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 AB.13 Hiding Subdirectories).
dired-mark-files-containing-regexp
). This command is like
% m, except that it searches the file contents instead of the file
name.
dired-undo
). This command does not revert the
actual file operations, nor recover lost files! It just undoes
changes in the buffer itself. For example, if used after renaming one
or more files, dired-undo
restores the original names, which
will get the Dired buffer out of sync with the actual contents of the
directory.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 of them give you several ways to specify which files to manipulate:
Commands which ask for a destination directory, such as those which
copy and rename files or create links for them, try to guess the default
target directory for the operation. Normally, they suggest the Dired
buffer's default directory, but if the variable dired-dwim-target
is non-nil
, and if there is another Dired buffer displayed in the
next window, that other buffer's directory is suggested instead.
Here are the file-manipulating commands that operate on files in this way. (Some other Dired commands, such as ! and the `%' commands, also use these 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.
The variable dired-recursive-copies
controls whether
directories are copied recursively. The default is to not copy
recursively, which means that directories cannot be copied.
dired-do-delete
). Like the other
commands in this section, this command operates on the marked
files, or the next n files. By contrast, x
(dired-do-flagged-delete
) deletes all flagged 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.
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-buffer
uses;
see section AC.18 Hardcopy Output).
dired-do-compress
). If the file
appears to be a compressed file already, it is uncompressed instead.
dired-do-load
).
See section V.7 Libraries of Lisp Code for Emacs.
dired-do-byte-compile
). See section `Byte Compilation' in The Emacs Lisp Reference Manual.
dired-do-search
).
This command is a variant of tags-search
. The search stops at
the first match it finds; use M-, to resume the search and find
the next match. See section W.2.6 Searching and Replacing with Tags Tables.
query-replace-regexp
on each of the specified files,
replacing matches for regexp with the string
to (dired-do-query-replace-regexp
).
This command is a variant of tags-query-replace
. If you exit the
query replace loop, you can use M-, to resume the scan and replace
more matches. See section W.2.6 Searching and Replacing with Tags Tables.
One special file-operation command is +
(dired-create-directory
). This command reads a directory name and
creates the directory if it does not already exist.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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. X is a synonym for !. You can specify the
files to operate on in the usual ways for Dired commands
(see section AB.7 Operating on Files). There are two ways of applying a shell
command to multiple files:
Thus, ! tar cf foo.tar * RET runs tar
on the entire
list of file names, putting them into one tar file `foo.tar'.
For example, ! uudecode RET runs uudecode
on each
file.
What if you want to run the shell command once for each file, with the file name inserted in the middle? You can use `?' in the command instead of `*'. The current file name is substituted for `?'. You can use `?' more than once. For instance, here is how to uuencode each file, making the output file name by appending `.uu' to the input file name:
uuencode ? ? > ?.uu |
To use the file names in a more complicated fashion, you can use a shell loop. For example, this shell command is another way to uuencode each file:
for file in *; do 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 AB.14 Updating the Dired Buffer).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes Dired commands which alter file names in a systematic way.
Like the basic Dired file-manipulation commands (see section AB.7 Operating on Files), the commands described here operate either on the next n files, or on all files marked with `*', or on the current file. (To mark files, use the commands described in AB.6 Dired Marks vs. Flags.)
All of the commands described in this section work interactively: they ask you to confirm the operation for each candidate file. Thus, you can select more files than you actually need to operate on (e.g., with a regexp that matches many files), and then refine the selection by typing y or n when the command prompts for confirmation.
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 a search-and-replace 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
replace-regexp
(see section K.7.2 Regexp Replacement). 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. (Non-zero argument specifies the number of files to operate on.)
Often you will want to select the set of files to operate on using the same regexp that you will use to operate on them. 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.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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. Use C-SPC
(set-mark-command
) to set the mark at the first file's line
(see section H.1 Setting the Mark), since dired-diff
ignores the files marked
with the Dired's m command.
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
.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 C-u k on the subdirectory header
line to delete the subdirectory. See section AB.14 Updating the Dired Buffer.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When a Dired buffer lists subdirectories, you can use the page motion commands C-x [ and C-x ] to move by entire directories (see section T.4 Pages).
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
).
dired-prev-dirline
).
These lines are the ones that describe a directory as a file in its
parent directory.
dired-prev-dirline
).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Hiding a subdirectory means to make it invisible, except for its header line, via selective display (see section J.9 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.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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
). Like the Dired file-operating commands,
this command operates on the next n files (or previous
-n files), or on the marked files if any, or on the
current file. Updating the files means reading their current status,
then updating their lines in the buffer to indicate that status.
If you use l on a subdirectory header line, it updates the contents of the corresponding subdirectory.
To delete the specified file lines from the buffer--not
delete the files--type k (dired-do-kill-lines
). Like
the file-operating commands, this command operates on the next n
files, or on the marked files if any; but it does not operate on the
current file as a last resort.
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 a subdirectory.
The files in a Dired buffers are normally listed in 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
.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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
. See also M-x grep-find, in V.1 Running Compilations under Emacs.
Remember to write the regular expression for grep
, not for Emacs.
(An alternative method of showing files whose contents match a given
regexp is the % g regexp command, see AB.6 Dired Marks vs. Flags.)
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
.
M-x locate provides a similar interface to the locate
program. M-x locate-with-filter is similar, but keeps only lines
matching a given regular expression.
The format of listing produced by these commands is controlled by the
variable find-ls-option
, whose default value specifies using
options `-ld' for ls
. If your listings are corrupted, you
may need to change the value of this variable.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |