Dired, the Directory Editor
Dired makes an Emacs buffer containing a listing of a directory, and optionally some of its subdirectories as well. You can use the normal Emacs commands to move around in this buffer, and special Dired commands to operate on the 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, Wdired). 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. Dired-X. 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. Directories.
Entering Dired
To 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 @key{RET}
C-x d ~/foo/*/*.el @key{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 @key{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 (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 (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. 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 =. Dired does not handle files that have names with embedded newline characters well. If you have many such files, you may consider adding -b to dired-listing-switches. This will quote all special characters and allow Dired to handle them better. (You can also use the C-u C-x d command to add -b temporarily.) 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. ls in Lisp, 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. Typing q (quit-window) buries the Dired buffer, and deletes its window if the window was created just for that buffer.
Deleting Files with Dired
One of the most frequent uses of Dired is to first flag files for deletion, then delete the files that were flagged.
-
d - Flag this file for deletion (
dired-flag-file-deletion). -
u - Remove the deletion flag (
dired-unmark). -
DEL - Move point to previous line and remove the deletion flag on that line (
dired-unmark-backward). -
x - 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. Misc File Ops. An alternative way of deleting files is to mark them with m and delete with D, see Operating on Files.
Flagging Many Files at Once
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 (Auto Save). -
~ - Flag all backup files (files whose names end with
~) for deletion (Backup). -
.(Period) - Flag excess numeric backup files for deletion. The oldest and newest few backup files of any one file are exempt; the middle ones are flagged.
-
% & - Flag for deletion all files with certain kinds of names which suggest you could easily create those files again.
-
% d regexp RET - 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 #. Auto Save. ~ (dired-flag-backup-files) flags all files whose names say they are backup files—that is, files whose names end in ~. Backup. . (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. Hiding Subdirectories.
Visiting Files in Dired
There are several Dired commands for visiting or examining the files listed in the Dired buffer. All of them apply to the current line's file; if that file is really a directory, these commands invoke Dired on that subdirectory (making a separate Dired buffer).
-
f - Visit the file described on the current line, like typing
C-x C-fand supplying that file name (dired-find-file). Visiting. -
RET,e - Equivalent to
f. -
o - Like
f, but uses another window to display the file's buffer (dired-find-file-other-window). The Dired buffer remains visible in the first window. This is like usingC-x 4 C-fto visit the file. Windows. -
C-o - Visit the file described on the current line, and display the buffer in another window, but do not select that window (
dired-display-file). -
mouse-1,mouse-2 - Visit the file whose name you clicked on (
dired-mouse-find-file-other-window). This uses another window to display the file, like theocommand. -
v - 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; View Mode. -
^ - Visit the parent directory of the current directory (
dired-up-directory). This is equivalent to moving to the line for..and typingfthere.
@defopt dired-kill-when-opening-new-dired-buffer 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. @end defopt
Dired Marks vs. Flags
Instead of flagging a file with D, you can mark the file with some other character (usually *). Most Dired commands to operate on files 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. (Dired Deletion, for commands to flag and unflag files.)
-
m,* m - 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 (Subdirectories in Dired), this command marks all the files in that subdirectory. -
* N - 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. -
* s - Mark all the files in the current subdirectory, aside from
.and..(dired-mark-subdir-files). -
u,* u - 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). -
DEL,* DEL - 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). -
* !,U - Remove all marks from all the files in this Dired buffer (
dired-unmark-all-marks). -
* ? markchar,M-DEL - Remove all marks that use the character markchar (
dired-unmark-all-files). If invoked withM-DEL, the command prompts for markchar. That markchar is a single character—do not useRETto terminate it. See the description of the* ccommand 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 answerymeaning yes,nmeaning no, or!to remove the marks from the remaining files without asking about them. -
* C-n,M-@} - Move down to the next marked file (
dired-next-marked-file). A file is "marked" if it has any kind of mark. -
* C-p,M-@{ - Move up to the previous marked file (
dired-prev-marked-file). -
t,* t - 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. -
* c old-markchar new-markchar - 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*orD. The arguments are single characters—do not useRETto 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 =Dflags on all the files that have no marks, while unflagging all those that already haveDflags: * c D t * cSPCD * c tSPCThis assumes that no files were already marked witht. -
% m regexp RET,* % regexp RET - 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 withD. 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 (Hiding Subdirectories). -
% g regexp RET - 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, anddired-always-read-filesystemisnil(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. Reverting. 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 setdired-always-read-filesystemto non-nil. -
C-/,C-x u,C-_ - Undo changes in the Dired buffer, such as adding or removing marks (
dired-undo). This command does not revert the actual file operations It just undoes changes in the buffer itself. In some cases, using this after commands that operate on files can cause trouble. For example, after renaming one or more files,dired-undorestores the original names in the Dired buffer, which gets the Dired buffer out of sync with the actual contents of the directory.
Operating on Files
This section describes the basic Dired commands to operate on one file or several files. All of these commands are capital letters; all of them use the minibuffer, either to read an argument or to ask for confirmation, before they act. All of them let you specify the files to manipulate in these ways:
- If you give the command a numeric prefix argument n, it operates on the next n files, starting with the current file. (If n is negative, the command operates on the -n files preceding the current line.)
- Otherwise, if some files are marked with
*, the command operates on all those files. - Otherwise, the command operates on the current file only.
Certain other Dired commands, such as ! and the % commands, use the same conventions to decide which files to work on. 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.
-
C new RET - Copy the specified files (
dired-do-copy). The argument new is the directory to copy into, or (if copying a single file) the new name. This is like the shell commandcp. The optiondired-create-destination-dirscontrols whether Dired should create non-existent directories in the destination while copying/renaming files. The default valuenilmeans Dired never creates such missing directories; the valuealways, means Dired automatically creates them; the valueaskmeans Dired asks you for confirmation before creating them. Ifdired-copy-preserve-timeis non-nil, then copying with this command preserves the modification time of the old file in the copy, likecp -p. The variabledired-recursive-copiescontrols whether to copy directories recursively (likecp -r). The default istop, which means to ask before recursively copying a directory. The variabledired-copy-dereferencecontrols whether to copy symbolic links as links or after dereferencing (likecp -L). The default isnil, which means that the symbolic links are copied by creating new ones. -
D - Delete the specified files (
dired-do-delete). This is like the shell commandrm. 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. -
R new RET - 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 commandmv). The optiondired-create-destination-dirscontrols whether Dired should create non-existent directories in 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 variabledired-vc-rename-fileis non-nil, files are renamed using the commands of the underlying VCS, viavc-rename-file(VC Delete/Rename). -
H new RET - Make hard links to the specified files (
dired-do-hardlink). This is like the shell commandln. The argument new is the directory to make the links in, or (if making just one link) the name to give the link. -
S new RET - Make symbolic links to the specified files (
dired-do-symlink). This is likeln -s. The argument new is the directory to make the links in, or (if making just one link) the name to give the link. -
M modespec RET - Change the mode (also called permission bits) of the specified files (
dired-do-chmod). modespec can be in octal or symbolic notation, like arguments handled by thechmodprogram. 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. -
G newgroup RET - Change the group of the specified files to newgroup (
dired-do-chgrp). -
O newowner RET - Change the owner of the specified files to newowner (
dired-do-chown). (On most systems, only the superuser can do this.) The variabledired-chown-programspecifies the name of the program to use to do the work. (This variable is necessary because different systems putchownin different places). -
T timestamp RET - 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 commandtouch. -
P command RET - Print the specified files (
dired-do-print). You must specify the command to print them with, but the minibuffer starts out with a suitable guess made using the variableslpr-commandandlpr-switches(the same variables thatlpr-bufferuses; Printing). -
Z - 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 thegzipprogram if it is available, otherwise it usescompress. On a directory name, this command produces a compressed archive depending on thedired-compress-directory-default-suffixuser option. The default is a.tar.gzarchive containing all of the directory's files, by running thetarcommand with output piped togzip. To allow decompression of compressed directories, typingZon a.tar.gzor.tgzarchive file unpacks all the files in the archive into a directory whose name is the archive name with the extension removed. -
c - Compress the specified files (
dired-do-compress-to) into a single archive anywhere on the file system. The default archive is controlled by thedired-compress-directory-default-suffixuser option. Also seedired-compress-files-alist. -
:d - Decrypt the specified files (
epa-dired-do-decrypt). Dired integration. -
:v - Verify digital signatures on the specified files (
epa-dired-do-verify). Dired integration. -
:s - Digitally sign the specified files (
epa-dired-do-sign). Dired integration. -
:e - Encrypt the specified files (
epa-dired-do-encrypt). Dired integration. -
L - Load the specified Emacs Lisp files (
dired-do-load). Lisp Libraries. -
B - Byte compile the specified Emacs Lisp files (
dired-do-byte-compile). Byte Compilation. -
A regexp RET - Search all the specified files for the regular expression regexp (
dired-do-find-regexp). This command is a variant ofxref-find-references(Identifier Search), it displays the*xref*buffer, where you can navigate between matches and display them as needed using the commands described in Xref Commands. 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 matchgrep-find-ignored-filesand subdirectories whose names matchgrep-find-ignored-directories. -
Q regexp RET to RET - Perform
query-replace-regexpon each of the specified files, replacing matches for regexp with the string to (dired-do-find-regexp-and-replace). This command is a variant ofxref-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 (Xref Commands). In particular, if you exit the query replace loop, you can userin that buffer to replace more matches. Identifier Search. Like withdired-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 matchgrep-find-ignored-filesand subdirectories whose names matchgrep-find-ignored-directories.
Shell Commands in Dired
The Dired command ! (dired-do-shell-command) reads a shell command string in the minibuffer, and runs that shell command on one or more files. The files that the shell command operates on are determined in the usual way for Dired commands (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:
- If you use
*surrounded by whitespace in the command string, then the command runs just once, with the list of file names substituted for the*. The order of file names is the order of appearance in the Dired buffer. Thus,! tar cf foo.tar * RETrunstaron the entire list of file names, putting them into one tar filefoo.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. - Otherwise, if the command string contains
?surrounded by whitespace or`?`, Emacs runs the shell command once for each file, substituting the current file name for?and`?`each time. You can use both?and`?`more than once in the command; the same file name replaces each occurrence. If you mix them with*the command signals an error. - If the command string contains neither
*nor?nor`?`, Emacs runs the shell command once for each file, adding the file name at the end. For example,! uudecode RETrunsuudecodeon 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 (Dired Updating). Single Shell, for information about running shell commands outside Dired.
Transforming File Names in Dired
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 (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 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.
-
% u - Rename each of the selected files to an upper-case name (
dired-upcase). If the old file names areFooandbar, the new names areFOOandBAR. -
% l - Rename each of the selected files to a lower-case name (
dired-downcase). If the old file names areFooandbar, the new names arefooandbar. -
% R from RET to RET,% C from RET to RET,% H from RET to RET,% S from RET to RET - These four commands rename, copy, make hard links and make soft links, in each case computing the new name by regular-expression substitution from the name of the old file.
The four regular-expression substitution commands effectively perform 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 (Regexp 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 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.
File Comparison with Dired
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 (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; Setting Mark). Otherwise, if the file at point has a backup file (Backup), that is the default. You could also compare files using ediff-files, see Major Entry Points.
Subdirectories in Dired
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):
-
i - 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 (Setting Mark). You can also use ^ to return to the parent directory in the same Dired buffer (Dired Visiting). 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 (Dired Updating). You can also hide and show inserted subdirectories (Hiding Subdirectories).
Subdirectory Switches in Dired
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 (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.
Moving Over Subdirectories
When a Dired buffer lists subdirectories, you can use the page motion commands C-x [ and C-x ] to move by entire directories (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.
-
C-M-n - Go to next subdirectory header line, regardless of level (
dired-next-subdir). -
C-M-p - Go to previous subdirectory header line, regardless of level (
dired-prev-subdir). -
C-M-u - Go up to the parent directory's header line (
dired-tree-up). -
C-M-d - Go down in the directory tree, to the first subdirectory's header line (
dired-tree-down). -
< - 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).
Hiding Subdirectories
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. -
M-$ - Hide all subdirectories in this Dired buffer, leaving only their header lines (
dired-hide-all). Or, if any subdirectory is currently hidden, make all subdirectories visible again. You can use this command to get an overview in very deep directory trees or to move quickly to subdirectories far away.
Ordinary Dired commands never consider files inside a hidden subdirectory. For example, the commands to operate on marked files ignore files in hidden directories even if they are marked. Thus you can use hiding to temporarily exclude subdirectories from operations without having to remove the Dired marks on files in those subdirectories. Subdirectories in Dired, for how to insert a subdirectory listing, and see Dired Updating, for how to delete it.
Updating the Dired Buffer
This section describes commands to update the Dired buffer to reflect outside (non-Dired) changes in the directories and files, and to delete part of the Dired buffer.
-
g - Update the entire contents of the Dired buffer (
revert-buffer). -
l - Update the specified files (
dired-do-redisplay). You specify the files forlin the same way as for file operations. -
k - Delete the specified file lines—not the files, just the lines (
dired-do-kill-lines). -
s - Toggle between alphabetical order and date/time order (
dired-sort-toggle-or-edit). -
C-u s switches RET - Refresh the Dired buffer using switches as
dired-listing-switches.
Type g (revert-buffer) to update the contents of the Dired buffer, based on changes in the files and directories listed. This preserves all marks except for those on files that have vanished. Hidden subdirectories are updated but remain hidden. To update only some of the files, type l (dired-do-redisplay). 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 (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.
Dired and find
You can select a set of files for display in a Dired buffer more flexibly by using the find utility to choose the files. To search for files with names matching a wildcard pattern use M-x find-name-dired. It reads arguments directory and pattern, and chooses all the files in directory or its subdirectories whose individual names match pattern. The files thus chosen are displayed in a Dired buffer, in which the ordinary Dired commands are available. If you want to test the contents of files, rather than their names, use M-x find-grep-dired. This command reads two minibuffer arguments, directory and regexp; it chooses all the files in directory or its subdirectories that contain a match for regexp. It works by running the programs find and grep. See also M-x grep-find, in Grep Searching. 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 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.
Editing the Dired Buffer
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.
Viewing Image Thumbnails in Dired
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 (Image Mode) 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. This command asks for confirmation if the number of image files exceeds image-dired-show-all-from-dir-max-files. With point in the thumbnail buffer, you can type RET (image-dired-display-thumbnail-original-image) to display a sized version of it in another window. This sizes the image to fit the window. Use the arrow keys to move around in the buffer. For easy browsing, use SPC (image-dired-display-next-thumbnail-original) to advance and display the next image. Typing DEL (image-dired-display-previous-thumbnail-original) backs up to the previous thumbnail and displays that instead. To view the image in its original size, either provide a prefix argument (C-u) before pressing RET, or 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. You can also delete the thumbnail image from the thumbnail buffer with C-d (image-dired-delete-char). 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-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). 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.
Other Dired Features
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. Repeat Isearch, 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 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. If the directory you are visiting is under version control (Version Control), then the normal VC diff and log commands will operate on the selected files. 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 currently not supported.