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 listed files. Dired works with both local and remote directories.
The Dired buffer is normally read-only, and inserting text in it is not allowed (however, the Wdired mode allows that, see Editing the Dired Buffer). Ordinary printing characters such as d and x are redefined 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. You first mark certain files in order to operate on all of them with one command.
The Dired-X package provides various extra features for Dired mode. See Dired-X in Dired Extra User’s Manual.
You can also view a list of files in a directory with C-x C-d
(list-directory). Unlike Dired, this command does not allow
you to operate on the listed files. See File Directories.
findTo invoke Dired, type C-x d (dired). This reads a
directory’s name using the minibuffer, and opens a Dired buffer
listing the files in that directory. You can also supply a wildcard
file name pattern as the minibuffer argument, in which case the Dired
buffer lists all files matching that pattern. A wildcard may appear
in the directory part as well.
For instance,
C-x d ~/foo/*.el RET C-x d ~/foo/*/*.el RET
The former lists all the files with extension ‘.el’ in directory ‘foo’. The latter lists the files with extension ‘.el’ in all the subdirectories of ‘foo’.
On Posix systems, when the system shell supports globstar, a recursive globbing feature, and that support is enabled, you can use recursive globbing in Dired:
C-x d ~/foo/**/*.el RET
This command produces a directory listing with all the files with extension ‘.el’, descending recursively in all the subdirectories of ‘foo’. Note that there are small differences in the implementation of globstar between different shells. Check your shell manual to know the expected behavior.
If the shell supports globstar, but that support is disabled by
default, you can still let Dired use this feature by customizing
dired-maybe-use-globstar to a non-nil value; then Dired
will enable globstar for those shells for which it knows how (see
dired-enable-globstar-in-shell for the list of those shells).
The usual history and completion commands can be used in the minibuffer; in particular, M-n puts the name of the visited file (if any) in the minibuffer (see Minibuffer History).
You can also invoke Dired by giving C-x C-f (find-file)
a directory’s name.
You can ask Emacs to invoke Dired on the default-directory
(see default-directory) of any buffer, by typing
C-x C-j (dired-jump). If the buffer visits a file, this
command will move point to that file’s line in the Dired buffer it
shows; otherwise, point will end up on the first file in the directory
listing. As an exception, if you type C-x C-j in a Dired
buffer, Emacs displays the directory listing of the parent directory
and places point on the line that corresponds to the directory where
you invoked dired-jump. Typing C-x 4 C-j
(dired-jump-other-window) has the same effect, but displays the
Dired buffer in a new window (see How display-buffer works).
The variable dired-listing-switches specifies the options to
give to ls for listing the directory; this string
must contain ‘-l’. If you use a prefix argument with the
dired command, you can specify the ls switches with the
minibuffer before you enter the directory specification. No matter
how they are specified, the ls switches can include short
options (that is, single characters) requiring no arguments, and long
options (starting with ‘--’) whose arguments are specified with
‘=’.
You can declare dired-listing-switches as a connection-local
variable in order to adjust its value to match what a remote system
expects (see Per-Connection Local Variables).
When a file name contains a newline character, Dired displays it by
default as a literal newline, so the display of this file name occupies
more than one line in the Dired buffer. If you invoke a Dired operation
on such a file listing, in many cases it will fail and signal an error.
For this reason, when Dired displays a file name containing a literal
newline, Emacs recognizes this and automatically pops up a buffer with
an informative warning. For such file names, Dired offers an
alternative display, using the ls switch ‘-b’, in which
newline characters are represented by ‘\n’ and the Dired listing of
the file occupies one line as usual, so you can execute all applicable
Dired operations on it.24
Emacs provides two different ways to make Dired use the ‘-b’ switch:
dired-listing-switches before invoking
dired. Since this variable is a user option, you can alter its
value persistently either by using the Customization interface
(see Saving Customizations) or by using the setopt macro in
your initialization file (see Examining and Setting Variables).25 You can
also add ‘-b’ just for the next dired invocation by typing
C-u C-x d.
dired-auto-toggle-b-switch, then
when you visit a directory containing a file whose name has a newline,
Emacs will automatically add the ‘-b’ switch and redisplay the
directory in Dired to show ‘\n’ in the file name. If you edit the
file name and remove the ‘\n’ character, then on completing the
edit Emacs automatically removes the ‘-b’ switch and redisplays the
Dired buffer, so that file names with tab or space characters now show
literal spaces without a backslash. If you enable or disable
dired-auto-toggle-b-switch after visiting a directory containing
a file name with a newline, Emacs will add or remove the ‘-b’
switch as appropriate and automatically redisplay the Dired buffer.
Dired displays in the mode line an indication of what were the
switches used to invoke ls. By default, Dired will try to
determine whether the switches indicate sorting by name or date, and
will say so in the mode line. If the dired-switches-in-mode-line
variable is as-is, the switches will be shown verbatim. If
this variable’s value is an integer, the switch display will be
truncated to that length. This variable can also be a function, which
will be called with dired-actual-switches as the only
parameter, and should return a string to display in the mode line.
If your ls program supports the ‘--dired’ option,
Dired automatically passes it that option; this causes ls to
emit special escape sequences for certain unusual file names, without
which Dired will not be able to parse those names. The first time you
run Dired in an Emacs session, it checks whether ls supports
the ‘--dired’ option by calling it once with that option. If the
exit code is 0, Dired will subsequently use the ‘--dired’ option;
otherwise it will not. You can inhibit this check by customizing the
variable dired-use-ls-dired. The value unspecified (the
default) means to perform the check; any other non-nil value
means to use the ‘--dired’ option; and nil means not to
use the ‘--dired’ option.
On MS-Windows and MS-DOS systems, and also on some remote systems,
Emacs emulates ls. See Emulation of ls on MS-Windows, for options and
peculiarities of this emulation.
To display the Dired buffer in another window, use C-x 4 d
(dired-other-window). C-x 5 d
(dired-other-frame) displays the Dired buffer in a separate
frame. See How display-buffer works.
Typing q (quit-window) buries the Dired buffer, and
deletes its window if the window was created just for that buffer.
One of the most frequent uses of Dired is to first flag files for deletion, then delete the files that were flagged.
Flag this file for deletion (dired-flag-file-deletion).
Remove the deletion flag (dired-unmark).
Move point to previous line and remove the deletion flag on that line
(dired-unmark-backward).
Delete files flagged for deletion (dired-do-flagged-delete).
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 prefix argument serves as a
repeat count; a negative count means to flag preceding files.
If the region is active, the d command flags all files in the region for deletion; in this case, the command does not move point, and ignores any prefix argument.
The reason for flagging files for deletion, rather than deleting
files immediately, is 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. A numeric prefix argument to
either command serves as a repeat count, with a negative count meaning
to unflag in the opposite direction. If the region is active, these
commands instead unflag all files in the region, without moving point.
To delete flagged files, type x
(dired-do-flagged-delete). This command 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 Dired
buffer, with somewhat fewer lines, 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.
You can delete empty directories just like other files, but normally
Dired cannot delete directories that are nonempty. However, if the
variable dired-recursive-deletes is non-nil, then Dired
is allowed to delete nonempty directories including all their
contents. That can be somewhat risky. If the value of the variable
is always, Dired will delete nonempty directories recursively,
which is even more risky.
Even if you have set dired-recursive-deletes to nil, you
might want sometimes to delete directories recursively without being
asked for confirmation for all of them. For example, you may want
that when you have marked many directories for deletion and you are
very sure that all of them can safely be deleted. For every nonempty
directory you are asked for confirmation to delete, if you answer
all, then all the remaining directories will be deleted without
any further questions.
If you change the variable delete-by-moving-to-trash to
t, the above deletion commands will move the affected files or
directories into the operating system’s Trash, instead of deleting
them outright. See Miscellaneous File Operations.
An alternative way of deleting files is to mark them with m and delete with D, see Operating on Files.
The #, ~, ., % &, and % d commands flag many files for deletion, based on their file names:
Flag all auto-save files (files whose names start and end with ‘#’) for deletion (see Auto-Saving: Protection Against Disasters).
Flag all backup files (files whose names end with ‘~’) for deletion (see Backup Files).
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.
Flag for deletion all files with certain kinds of names which suggest you could easily create those files again.
Flag for deletion all files whose names match the regular expression regexp.
# (dired-flag-auto-save-files) flags all files whose
names look like auto-save files—that is, files whose names begin and
end with ‘#’. See Auto-Saving: Protection Against Disasters.
~ (dired-flag-backup-files) flags all files whose names
say they are backup files—that is, files whose names end in
‘~’. See Backup Files.
. (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, the number of newest versions kept
for each file is given by the variable dired-kept-versions
(not kept-new-versions; that applies only when saving).
The number of oldest versions to keep is given by the variable
kept-old-versions.
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.
% & (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.
% d 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 certain subdirectories
from marking by hiding them while you use % d. See 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).
Visit the file described on the current line, like typing C-x C-f
and supplying that file name (dired-find-file). See Visiting Files.
Equivalent to f.
Like f, but uses another window (see How display-buffer works) 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 Multiple Windows.
Visit the file described on the current line, and display the buffer in
another window, but do not select that window (dired-display-file).
Visit the file whose name you clicked on
(dired-mouse-find-file-other-window). This uses another window
to display the file, like the o command.
View the file described on the current line, with View mode
(dired-view-file). View mode provides convenient commands to
navigate the buffer but forbids changing it; See View Mode.
Open the file described on the current line, with superuser, or root,
permissions (tramp-dired-find-file-with-sudo). If called with a
C-u prefix argument, it prompts for another Tramp method to use
other than the default, sudo. See Tramp in The Tramp Manual.
Visit the parent directory of the current directory
(dired-up-directory). This is equivalent to moving to the line
for .. and typing f there.
When visiting a new sub-directory in Dired, Emacs will (by default)
open a new buffer to display this new directory, and leave the old
Dired buffer as is. If this user option is non-nil, the old
Dired buffer will be killed after selecting the new directory. This
means that if you’re traversing a directory structure in Dired, you
won’t end up with more than a single Dired buffer.
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 only command that operates on flagged files is x, which deletes them.
Here are some commands for marking with ‘*’, for unmarking, and for operating on marks. (See Deleting Files with Dired, for commands to flag and unflag files.)
Mark the current file with ‘*’ (dired-mark). If the
region is active, mark all files in the region instead; otherwise, if
a numeric argument n is supplied, mark the next n files
instead, starting with the current file (if n is negative, mark
the previous −n files). If invoked on a subdirectory
header line (see Subdirectories in Dired), this command marks all
the files in that subdirectory.
Report what the number and size of the marked files are
(dired-number-of-marked-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 directories, except for
. and .. (dired-mark-directories). With a numeric
argument, unmark all those files.
Mark all the files in the current subdirectory, aside from .
and .. (dired-mark-subdir-files).
Remove any mark on this line (dired-unmark). If the region is
active, unmark all files in the region instead; otherwise, if a
numeric argument n is supplied, unmark the next n files
instead, starting with the current file (if n is negative,
unmark the previous −n files).
Move point to previous line and remove any mark on that line
(dired-unmark-backward). If the region is active, unmark all
files in the region instead; otherwise, if a numeric argument n
is supplied, unmark the n preceding files instead, starting with
the current file (if n is negative, unmark the next
−n files).
Remove all marks from all the files in this Dired buffer
(dired-unmark-all-marks).
Remove all marks that use the character markchar
(dired-unmark-all-files). If invoked with M-DEL,
the command prompts for markchar. That markchar 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.
Move down to the next marked file (dired-next-marked-file).
A file is “marked” if it has any kind of mark.
Move up to the previous marked file (dired-prev-marked-file).
Toggle all marks (dired-toggle-marks): files marked with ‘*’
become unmarked, and unmarked files are marked with ‘*’. Files
marked in any other way are not affected.
Replace all marks that use the character old-markchar with marks
that use the character new-markchar (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’.
Mark (with ‘*’) all files whose names match the regular expression
regexp (dired-mark-files-regexp). This command is like
% d, except that it marks files with ‘*’ instead of flagging
with ‘D’.
Only the non-directory part of the file name is used in matching. Use ‘^’ and ‘$’ to anchor matches. You can exclude subdirectories by temporarily hiding them (see Hiding Subdirectories).
Mark (with ‘*’) all files whose contents contain a match for
the regular expression regexp
(dired-mark-files-containing-regexp). This command is like
% m, except that it searches the file contents instead of the file
name. Note that if a file is visited in an Emacs buffer,
and dired-always-read-filesystem is nil (the default), this
command will look in the buffer without revisiting the file, so the results
might be inconsistent with the file on disk if its contents have changed
since it was last visited. If you don’t want this, you may wish to
revert the files you have visited in your buffers, or to turn on
Auto-Revert mode in those buffers, before invoking this command.
See Reverting a Buffer. If you prefer that this command should always
revisit the file, without you having to revert the file or enable
Auto-Revert mode, you might want to set
dired-always-read-filesystem to non-nil.
Undo changes in the Dired buffer, such as adding or removing
marks (dired-undo). This command does not revert the
actual file operations, nor recover lost files! It just undoes
changes in the buffer itself. Like with undo, a numeric prefix
argument specifies a repeat count.
In some cases, using this after commands that operate on files can
cause trouble. For example, after renaming one or more files,
dired-undo restores the original names in the Dired buffer,
which gets the Dired buffer out of sync with the actual contents of
the directory.
Enter a “click to select” mode (dired-click-to-select-mode),
where using the mouse button mouse-2 on a file name will cause its
mark to be toggled. This mode is useful when performing file management
using a touch screen device.
It is enabled when a “hold” gesture (see Using Emacs on Touchscreens) is detected over a file name, and is automatically disabled once a Dired command that operates on the marked files finishes.
This section describes the basic Dired commands to operate on one file or several files. Many 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 let you specify the files to manipulate in these ways:
Certain other Dired commands, such as ! and the ‘%’ commands, use the same conventions to decide which files to work on.
In addition to Dired commands described here, you can also invoke Version Control (VC) commands on one or more files shown in a Dired buffer. See Version Control.
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 option dired-dwim-target
is non-nil, and if there is another Dired buffer displayed in
some window, that other buffer’s directory is suggested instead.
You can customize dired-dwim-target to prefer either the next
window with a Dired buffer, or the most recently used window with
a Dired buffer, or to use any other function. When the value is
a function, it will be called with no arguments and is expected to
return a list of directories which will be used as defaults
(i.e., default target and “future history”).
Here are the file-manipulating Dired commands that operate on files.
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. This is like the shell command cp.
The option dired-create-destination-dirs controls whether Dired
should create non-existent directories in the destination while
copying/renaming files. The default value nil means Dired
never creates such missing directories; the value always,
means Dired automatically creates them; the value ask
means Dired asks you for confirmation before creating them.
If the option dired-create-destination-dirs-on-trailing-dirsep
is non-nil in addition to dired-create-destination-dirs,
a trailing directory separator at the destination directory is treated
specially. In that case, when copying to ‘test/’ and no
directory ‘test’ exists already, it will be created and the
specified source files or directories are copied into the newly
created directory.
If dired-copy-preserve-time is non-nil, then copying
with this command preserves the modification time of the old file in
the copy, like ‘cp -p’.
The variable dired-recursive-copies controls whether to copy
directories recursively (like ‘cp -r’). The default is
top, which means to ask before recursively copying a directory.
The variable dired-copy-dereference controls whether to copy
symbolic links as links or after dereferencing (like ‘cp -L’).
The default is nil, which means that the symbolic links are
copied by creating new ones.
The dired-keep-marker-copy user option controls how this
command handles file marking. The default is to mark all new copies
of files with a ‘C’ mark.
Delete the specified files (dired-do-delete). This is like the
shell command rm.
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.
“Open” the specified files using an external program (dired-do-open).
The program is selected according to the system conventions, as
determined by the variable shell-command-guess-open.
Rename the specified files (dired-do-rename). If you rename a
single file, the argument new is the new name of the file. If
you rename several files, the argument new is the directory into
which to move the files (this is like the shell command mv).
The option dired-create-destination-dirs controls whether Dired
should create non-existent directories in new.
The option dired-create-destination-dirs-on-trailing-dirsep,
when set in addition to dired-create-destination-dirs, controls
whether a trailing directory separator at the destination is treated
specially. In that case, when renaming a directory ‘old’ to
‘new/’ and no directory ‘new’ exists already, it will be
created and ‘old’ is moved into the newly created directory.
Otherwise, ‘old’ is renamed to ‘new’.
Dired automatically changes the visited file name of buffers associated with renamed files so that they refer to the new names.
If the value of the variable dired-vc-rename-file is non-nil,
files are renamed using the commands of the underlying VCS, via
vc-rename-file (see Deleting and Renaming Version-Controlled Files).
Make hard links to the specified files (dired-do-hardlink).
This is like the shell command ln. The argument new is
the directory to make the links in, or (if making just one link) the
name to give the link.
Make symbolic links to the specified files (dired-do-symlink).
This is like ‘ln -s’. The argument new is the directory to
make the links in, or (if making just one link) the name to give the
link.
Make relative symbolic links to the specified files
(dired-do-relsymlink). The argument new is the directory
to make the links in, or (if making just one link) the name to give
the link. This is like dired-do-symlink but creates relative
symbolic links. For example:
foo -> ../bar/foo
It does not create absolute ones like:
foo -> /path/that/may/change/any/day/bar/foo
Change the mode (also called permission bits) of the specified
files (dired-do-chmod). modespec can be in octal or
symbolic notation, like arguments handled by the chmod
program. This command does not follow symbolic links, so it reports
an error if you try to change the mode of a symbolic link on a
platform where such modes are immutable.
Change the group of the specified files to newgroup
(dired-do-chgrp).
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. (This variable is necessary because
different systems put chown in different places).
Touch the specified files (dired-do-touch). This means
updating their modification times to timestamp, which defaults
to the present time. This is like the shell command touch.
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-buffer uses;
see Printing Hard Copies).
Compress the specified files (dired-do-compress). If the file
appears to be a compressed file already, uncompress it instead. Each
marked file is compressed into its own archive; this uses the
gzip program if it is available, otherwise it uses
compress.
On a directory name, this command produces a compressed archive
depending on the dired-compress-directory-default-suffix user
option. The default is a .tar.gz archive containing all of the
directory’s files, by running the tar command with output
piped to gzip. To allow decompression of compressed
directories, typing Z on a .tar.gz or .tgz archive
file unpacks all the files in the archive into a directory whose name
is the archive name with the extension removed.
Compress the specified files (dired-do-compress-to) into a
single archive anywhere on the file system. The default archive is
controlled by the dired-compress-directory-default-suffix user
option. Also see dired-compress-files-alist.
If the directory you are visiting is under version control
(see Version Control), then the normal VC commands will operate on
the selected files. For example, C-x v v invokes
dired-vc-next-action, which does the same as
vc-next-action in a buffer visiting a file under version control
(see Basic Editing under Version Control). Similarly, C-x v = shows the diffs
between the marked files and their committed versions, and C-x v l
shows the VC change history for the marked files.
Decrypt the specified files (epa-dired-do-decrypt).
See Dired integration in EasyPG Assistant User’s Manual.
Verify digital signatures on the specified files (epa-dired-do-verify).
See Dired integration in EasyPG Assistant User’s Manual.
Digitally sign the specified files (epa-dired-do-sign).
See Dired integration in EasyPG Assistant User’s Manual.
Encrypt the specified files (epa-dired-do-encrypt).
See Dired integration in EasyPG Assistant User’s Manual.
Load the specified Emacs Lisp files (dired-do-load).
See Libraries of Lisp Code for Emacs.
Byte compile the specified Emacs Lisp files
(dired-do-byte-compile). See Byte
Compilation in The Emacs Lisp Reference Manual.
Run Info on this file (assumed to be a file in Info format).
Run man on this file (assumed to be a file in nroff format).
Search all the specified files for the regular expression regexp
(dired-do-find-regexp).
This command is a variant of xref-find-references
(see Searching and Replacing with Identifiers), it displays the *xref* buffer,
where you can navigate between matches and display them as needed
using the commands described in Commands Available in the *xref* Buffer.
If any of the marked files are directories, then this command searches
all of the files in those directories, and any of their
subdirectories, recursively, except files whose names match
grep-find-ignored-files and subdirectories whose names match
grep-find-ignored-directories.
Perform query-replace-regexp on each of the specified files,
replacing matches for regexp with the string
to (dired-do-find-regexp-and-replace).
This command is a variant of xref-query-replace-in-results. It
presents an *xref* buffer that lists all the matches of regexp,
and you can use the special commands in that buffer (see Commands Available in the *xref* Buffer). In particular, if you exit the query replace loop, you
can use r in that buffer to replace more matches.
See Searching and Replacing with Identifiers.
Like with dired-do-find-regexp, if any of the marked files are
directories, this command performs replacements in all of the files in
those directories, and in any of their subdirectories, recursively,
except for files whose names match grep-find-ignored-files and
subdirectories whose names match grep-find-ignored-directories.
The Dired command ! (dired-do-shell-command) reads a
shell command string in the minibuffer, and runs that shell command on
one or more files. The files that the shell command operates on are
determined in the usual way for Dired commands (see Operating on Files). The command X is a synonym for !.
The command & (dired-do-async-shell-command) does the
same, except that it runs the shell command asynchronously. (You can
also do this with !, by appending a ‘&’ character to the
end of the shell command.) When the command operates on more than one
file, it runs multiple parallel copies of the specified shell command,
one for each file. As an exception, if the specified shell command
ends in ‘;’ or ‘;&’, the shell command is run in the
background on each file sequentially; Emacs waits for each invoked
shell command to terminate before running the next one.
For both ! and &, the working directory for the shell command is the top-level directory of the Dired buffer.
If you tell ! or & to operate on more than one file, the shell command string determines how those files are passed to the shell command:
Thus, ! tar cf foo.tar * RET runs tar on the entire
list of file names, putting them into one tar file foo.tar.
If you want to use ‘*’ as a shell wildcard with whitespace around
it, write ‘*""’. In the shell, this is equivalent to ‘*’;
but since the ‘*’ is not surrounded by whitespace, Dired does not
treat it specially. Emacs will prompt for confirmation if you do
this, unless dired-confirm-shell-command is nil.
uudecode on
each file.
To iterate over the file names in a more complicated fashion, you might prefer to use an explicit shell loop. For example, here is how to uuencode each file, making the output file name by appending ‘.uu’ to the input file name:
for file in * ; do uuencode "$file" "$file" >"$file".uu; done
The same example with ‘`?`’ notation:
uuencode ? ? > `?`.uu
The ! and & commands do not attempt to update the Dired buffer to show new or modified files, because they don’t know what files will be changed. Use the g command to update the Dired buffer (see Updating the Dired Buffer).
See Single Shell Commands, for information about running shell commands outside Dired.
Based upon the name of a file, Dired tries to guess what shell command you might want to apply to it. For example, if you have point on a file named foo.tar and you press !, Dired will guess that you want to run ‘tar xvf’, and suggest that as the default shell command.
You can type M-n to get the default into the minibuffer for editing. If there are several commands for a given file, type M-n several times to see each matching command in order.
Dired only tries to guess a command for a single file, never for a list of marked files.
This variable specifies the predefined rules for guessing shell
commands suitable for certain files. Set this to nil to turn
guessing off. The elements of dired-guess-shell-alist-user
(defined by the user) will override these rules.
This variable is like dired-guess-shell-alist-default but
contains external viewers and players for various media formats.
Setting this to nil turns guessing off. The variables
dired-guess-shell-alist-user and
dired-guess-shell-alist-default will override these rules.
If non-nil, this variable specifies the user-defined alist of
file regexps and their suggested commands. These rules take
precedence over the predefined rules in the variables
dired-guess-shell-alist-default and
dired-guess-shell-alist-optional when
dired-do-shell-command is run). The default is nil.
Each element of the alist looks like
(regexp command...)
where each command can either be a string or a Lisp expression that evaluates to a string. If several commands are given, all of them will temporarily be pushed onto the history.
A ‘*’ in the shell command stands for the file name that matched regexp. When Emacs invokes the command, it replaces each instance of ‘*’ with the matched file name.
To add rules for ‘.foo’ and ‘.bar’ file extensions, add this to your Init file:
(setq dired-guess-shell-alist-user
(list
(list "\\.foo$" "foo-command") ; fixed rule
;; possibly more rules...
(list "\\.bar$" ; rule with condition test
'(if condition
"bar-command-1"
"bar-command-2"))))
This will override any predefined rules for the same extensions.
You can find more user options with M-x customize-group RET dired-guess RET.
This section describes Dired commands which alter file names in a systematic way. Each command operates on some or all of the marked files, using a new name made by transforming the existing name.
Like the basic Dired file-manipulation commands (see 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 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 filter the selected names by typing y or n when the command prompts for confirmation.
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.
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.
These five commands rename, copy, make hard links, make soft links, and make relative 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 a search-and-replace on the selected file names. They read
two arguments: a regular expression from, and a substitution
pattern to; they match each old file name against
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 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 file name.)
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. (A non-zero argument specifies the number of files to operate on.)
You may want to select the set of files to operate on using the same regexp from that you will use to operate on them. To do this, mark those files with % m from RET, then use the same regular expression in the command to operate on the files. To make this more convenient, the % commands to operate on files use the last regular expression specified in any % command as a default.
The = (dired-diff) command compares the current file
(the file at point) with another file (read using the minibuffer)
using the diff program. The file specified with the
minibuffer is the first argument of diff, and file at point
is the second argument. The output of the diff program is
shown in a buffer using Diff mode (see Comparing Files).
If the region is active, the default for the file read using the minibuffer is the file at the mark (i.e., the ordinary Emacs mark, not a Dired mark; see Setting the Mark). Otherwise, if the file at point has a backup file (see Backup Files), that is the default.
You could also compare files using ediff-files, see
Major Entry Points in Ediff User’s Manual.
A Dired buffer usually displays just one directory, 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.
More often, you will want to show only specific subdirectories. You
can do this with i (dired-maybe-insert-subdir):
Insert the contents of a subdirectory later in the buffer.
If you use this 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 returns to your previous position in the Dired buffer (see Setting the Mark). You can also use ‘^’ to return to the parent directory in the same Dired buffer (see Visiting Files in Dired).
Use the l command (dired-do-redisplay) to update the
subdirectory’s contents, and use C-u k on the subdirectory
header line to remove the subdirectory listing (see Updating the Dired Buffer). You can also hide and show inserted subdirectories
(see Hiding Subdirectories).
You can insert subdirectories with specified ls switches in
Dired buffers using C-u i. You can change the ls
switches of an already inserted subdirectory at point using C-u l.
Dired preserves the switches if you revert the buffer. Deleting a subdirectory forgets about its switches.
Using dired-undo
(see Dired Marks vs. Flags)
to reinsert or delete
subdirectories that were inserted with explicit switches can bypass
Dired’s machinery for remembering (or forgetting) switches. Deleting
a subdirectory using dired-undo does not forget its switches.
When later reinserted using i, it will be reinserted using its
old switches. Using dired-undo to reinsert a subdirectory that
was deleted using the regular Dired commands (not dired-undo)
will originally insert it with its old switches. Reverting the
buffer, however, will relist it using the buffer’s default switches.
If any of this yields problems, you can easily correct the situation
using C-u i or C-u l.
Dired does not remember the R switch. Inserting a subdirectory
with switches that include the R switch is equivalent to
inserting each of its subdirectories using all remaining switches.
For instance, updating or killing a subdirectory that was inserted
with the R switch will not update or kill its subdirectories.
The buffer’s default switches do not affect subdirectories that were inserted using explicitly specified switches. In particular, commands such as s that change the buffer’s switches do not affect such subdirectories. (They do, however, affect subdirectories without explicitly assigned switches.)
You can make Dired forget about all subdirectory switches and relist all subdirectories with the buffer’s default switches using M-x dired-reset-subdir-switches. This also reverts 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 (see 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.
Go to next subdirectory header line, regardless of level
(dired-next-subdir).
Go to previous subdirectory header line, regardless of level
(dired-prev-subdir).
Go up to the parent directory’s header line (dired-tree-up).
Go down in the directory tree, to the first subdirectory’s header line
(dired-tree-down).
Move up to the previous directory-file line (dired-prev-dirline).
These lines are the ones that describe a directory as a file in its
parent directory.
Move down to the next directory-file line (dired-next-dirline).
Prompt for a directory and move to its directory-file line
(dired-goto-subdir).
Hiding a subdirectory means to make it invisible, except for its header line.
Hide or show the subdirectory that point is in, and move point to the
next subdirectory (dired-hide-subdir). This is a toggle. A
numeric argument serves as a repeat count.
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 Dired marks on files in those subdirectories.
See Subdirectories in Dired, for how to insert a subdirectory listing, and see Updating the Dired Buffer, for how to delete it.
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.
Update the entire contents of the Dired buffer (revert-buffer).
Update the specified files (dired-do-redisplay). You specify the
files for l in the same way as for file operations.
Delete the specified file lines—not the files, just the lines
(dired-do-kill-lines).
Toggle between alphabetical order and date/time order
(dired-sort-toggle-or-edit).
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). 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.
If you use C-x d or some other Dired command to visit a
directory that is already being shown in a Dired buffer, Dired
switches to that buffer but does not update it. If the buffer is not
up-to-date, Dired displays a warning telling you to type g to
update it. You can also tell Emacs to revert each Dired buffer
automatically when you revisit it, by setting the variable
dired-auto-revert-buffer to a non-nil value.
To delete file lines from the buffer—without actually
deleting 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. However, it does not operate on
the current file, since otherwise mistyping k could be annoying.
If you use k to kill the line for a directory file which you had inserted in the Dired buffer as a subdirectory (see Subdirectories in Dired), it removes the subdirectory listing as well. Typing C-u k on the header line for a subdirectory also removes the subdirectory line 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.
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 Searching with Grep 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 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.
The format of listing produced by these commands is controlled by
the variable find-ls-option. This is a pair of options; the
first specifying how to call find to produce the file listing,
and the second telling Dired to parse the output.
The command M-x locate provides a similar interface to the
locate program. M-x locate-with-filter is similar, but
keeps only files whose names match a given regular expression.
These buffers don’t work entirely like ordinary Dired buffers: file operations work, but do not always automatically update the buffer. Reverting the buffer with g deletes all inserted subdirectories, and erases all flags and marks.
Wdired is a special mode that allows you to perform file operations
by editing the Dired buffer directly (the “W” in “Wdired” stands
for “writable”). To enter Wdired mode, type C-x C-q
(dired-toggle-read-only) while in a Dired buffer.
Alternatively, use the ‘Immediate / Edit File Names’ menu item.
While in Wdired mode, you can rename files by editing the file names
displayed in the Dired buffer. All the ordinary Emacs editing
commands, including rectangle operations and query-replace, are
available for this. Once you are done editing, type C-c C-c
(wdired-finish-edit). This applies your changes and switches
back to ordinary Dired mode.
Apart from simply renaming files, you can move a file to another directory by typing in the new file name (either absolute or relative). To mark a file for deletion, delete the entire file name. To change the target of a symbolic link, edit the link target name which appears next to the link name.
If you edit the file names to create a new subdirectory, Wdired will
automatically create these new directories. To inhibit this behavior,
set wdired-create-parent-directories to nil.
The rest of the text in the buffer, such as the file sizes and
modification dates, is marked read-only, so you can’t edit it.
However, if you set wdired-allow-to-change-permissions to
t, you can edit the file permissions. For example, you can
change ‘-rw-r--r--’ to ‘-rw-rw-rw-’ to make a file
world-writable. These changes also take effect when you type C-c
C-c.
Image-Dired is a facility for browsing image files. It provides viewing the images either as thumbnails or in full size, either inside Emacs or through an external viewer. This is different from Image mode (see Viewing Image Files) for visiting an image file in the Emacs buffer.
To enter Image-Dired, mark the image files you want to look at in
the Dired buffer, using m as usual. Then type C-t d
(image-dired-display-thumbs). This creates and switches to a
buffer containing Image-Dired, corresponding to the marked files.
You can also enter Image-Dired directly by typing M-x image-dired. This prompts for a directory; specify one that has image files. This creates thumbnails for all the images in that directory, and displays them all in the thumbnail buffer. The thumbnails are generated in the background and are loaded as they become available.
Image-Dired supports three methods of generating and storing
thumbnails, controlled by the value of the option
image-dired-thumbnail-storage:
image-dired ¶This method stores thumbnails as JPEG images in a single directory
specified by the variable image-dired-dir. This is the default.
The names of the thumbnail files are in this case constructed according
to the value of image-dired-thumb-naming.
standardstandard-largestandard-x-largestandard-xx-largeThese methods, mandated by the
Thumbnail Managing Standard, store thumbnails as PNG images
under the thumbnails subdirectory of the directory specified by
the environment variable XDG_CACHE_HOME (which defaults to
~/.cache).
per-directoryThis method stores the thumbnails of each directory in a .image-dired subdirectory of that directory, as jpeg images.
You can control the size of the thumbnail images by customizing the
variable image-dired-thumb-size. The default is 128 pixels.
This variable is in effect only for the first and the third method of
thumbnail storage described above; the Thumbnail Managing Standard uses
fixed sizes determined by the method: standard uses 128 pixels,
standard-large 256 pixels, etc. If you change the size after
some thumbnails were already created, you need to erase the thumbnail
files from their directory to have the new size take effect.
With point in the thumbnail buffer, you can type RET
(image-dired-display-this) to display the image in another
window. Use the standard Emacs movement key bindings or the arrow
keys to move around in the thumbnail buffer. For easy browsing, use
SPC (image-dired-display-next) to advance and display the
next image. Typing DEL (image-dired-display-previous)
backs up to the previous thumbnail and displays that instead.
Type C-RET
(image-dired-thumbnail-display-external) to display the image
in an external viewer. You must first configure
image-dired-external-viewer.
You can delete images through Image-Dired also. Type d
(image-dired-flag-thumb-original-file) to flag the image file
for deletion in the Dired buffer. Alternatively, you can remove an
image’s thumbnail from the thumbnail buffer without flagging the image
for deletion, by typing C-d (image-dired-delete-char).
You could also use Image-Dired for “inline” operations (i.e.,
right into the Dired buffer). Type C-t C-t, and the thumbnails
of the selected images in Dired will appear in front of their names
(image-dired-dired-toggle-marked-thumbs). C-t i and
C-t x will display the image under the point in Emacs or with
the external viewer, respectively.
More advanced features include image tags, which are metadata
used to categorize image files. The tags are stored in a plain text
file configured by image-dired-tags-db-file.
To tag image files, mark them in the Dired buffer (you can also mark
files in Dired from the thumbnail buffer by typing m) and type
C-t t (image-dired-tag-files). This reads the tag name
in the minibuffer. To mark files having a certain tag, type C-t f
(image-dired-mark-tagged-files). After marking image files
with a certain tag, you can use C-t d to view them.
You can also tag a file directly from the thumbnail buffer by typing
t t, and you can remove a tag by typing t r. There is
also a special tag called “comment” for each file (it is not a tag
in the exact same sense as the other tags, it is handled slightly
differently). That is used to enter a comment or description about
the image. You comment a file from the thumbnail buffer by typing
c. You will be prompted for a comment. Type C-t c to add
a comment from Dired (image-dired-dired-comment-files).
C-t e will bring a buffer to edit comment and tags
(image-dired-dired-edit-comment-and-tags).
If you have the exiftool program installed, you can set the
EXIF ‘ImageDescription’ tag of an image file by invoking
the image-dired-thumbnail-set-image-description command with
point at the thumbnail of the image file. This command prompts for the
description of the image, and adds the EXIF tag to it.
Files that are marked in Dired will also be marked in Image-Dired if
image-dired-thumb-visible-marks is non-nil (which is the
default).
Image-Dired also provides simple image manipulation. In the
thumbnail buffer, type L to rotate the original image 90 degrees
anti clockwise, and R to rotate it 90 degrees clockwise. This
rotation is lossless, and uses an external utility called
jpegtran, which you need to install first.
By default, Dired displays the available space on the directory’s
disk on the first line of that directory’s listing, following the
directory name. You can control this display by customizing the
variable dired-free-space. Its default value is first,
which produces the available space after the directory name. If you
customize it to the value separate instead, Dired will display
the disk space information on a separate line, following the line with
the directory name, and will include in that line the space used by
the files in the current directory as well as the available disk
space. If you set this to nil, the available disk space
information will not be displayed at all.
Dired fontifies the items in the Dired buffer. If the
default-directory of that buffer is remote, this might be
expensive for symbolic links, because their file-truename needs
to be accessed. To avoid slowdown in Dired due to this, set user option
dired-check-symlinks to nil for remote directories which
suffer from a slow connection. It can be declared as connection-local
variable to match how a remote system is connectable (see Per-Connection Local Variables).
The command + (dired-create-directory) reads a
directory’s name, and creates that directory. It signals an error if
the directory already exists.
The command (dired-create-empty-file) reads a
file name, and creates that file. It signals an error if
the file already exists.
The command M-s a C-s (dired-do-isearch) begins a
multi-file incremental search on the marked files. If a search
fails at the end of a file, typing C-s advances to the next
marked file and repeats the search; at the end of the last marked
file, the search wraps around to the first marked file. The command
M-s a M-C-s (dired-do-isearch-regexp) does the same with
a regular expression search. See Repeating Incremental Search, for information
about search repetition.
The command w (dired-copy-filename-as-kill) puts the
names of the marked (or next n) files into the kill ring, as if
you had killed them with C-w. The names are separated by a
space.
With a zero prefix argument, this uses the absolute file name of each marked file. With prefix value 1, it uses names relative to the project root. With just C-u as the prefix argument, it uses file names relative to the Dired buffer’s default directory. (This can still contain slashes if in a subdirectory.) As a special case, if point is on a directory header line, w gives you the absolute name of that directory. Any prefix argument or marked files are ignored in this case.
The main purpose of this command is so that you can yank the file names into arguments for other Emacs commands. It also displays what it added to the kill ring, so you can use it to display the list of currently marked files in the echo area.
If you have an HTML file in the file listing, it can be useful to
view that file with a browser. The W
(browse-url-of-dired-file) command will use the standard
configured browser to view that file.
The command ( (dired-hide-details-mode) toggles whether
details, such as ownership or file permissions, are visible in the
current Dired buffer. By default, it also hides the targets of
symbolic links, and all lines other than the header line and
file/directory listings. To change this, customize the options
dired-hide-details-hide-symlink-targets and
dired-hide-details-hide-information-lines, respectively.
The command M-x dired-compare-directories is used to compare the current Dired buffer with another directory. It marks all the files that differ between the two directories. It puts these marks in all Dired buffers where these files are listed, which of course includes the current buffer.
The default comparison method (used if you type RET at the
prompt) is to compare just the file names—file names differ if
they do not appear in the other directory. You can specify
more stringent comparisons by entering a Lisp expression, which can
refer to the variables size1 and size2, the respective
file sizes; mtime1 and mtime2, the last modification
times in seconds, as floating point numbers; and fa1 and
fa2, the respective file attribute lists (as returned by the
function file-attributes). This expression is evaluated for
each pair of like-named files, and files differ if the expression’s
value is non-nil.
For instance, the sequence M-x dired-compare-directories RET (> mtime1 mtime2) RET marks files newer in this directory than in the other, and marks files older in the other directory than in this one. It also marks files with no counterpart, in both directories, as always.
On the X Window System, Emacs supports the drag and drop protocol.
You can drag a file object from another program, and drop it onto a
Dired buffer; this either moves, copies, or creates a link to the file
in that directory. Precisely which action is taken is determined by
the originating program. Dragging files out of a Dired buffer is also
supported, by enabling the user option dired-mouse-drag-files,
the mouse can be used to drag files onto other programs. When set to
link, it will make the other program (typically a file manager)
create a symbolic link to the file; when set to move, it will
make the other program move the file to a new location, and setting it
to any other non-nil value will make the other program open or
create a copy of the file. The keyboard modifiers pressed during the
drag-and-drop operation can also control what action the other program
takes towards the file.
Note that with the ‘-b’ switch Dired displays tab characters in file names as ‘\t’ and escapes other control characters and also spaces in file names with ‘\’.
If
dired-listing-switches contains ‘-b’ when you invoke dired
on a directory containing a file name with a newline, the newline is
displayed as ‘\n’, so Emacs will not pop up a warning.