1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985, 86, 87, 93, 94, 95, 97, 2000
3 @c Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node Dired, Calendar/Diary, Rmail, Top
6 @chapter Dired, the Directory Editor
8 @cindex file management
10 Dired makes an Emacs buffer containing a listing of a directory, and
11 optionally some of its subdirectories as well. You can use the normal
12 Emacs commands to move around in this buffer, and special Dired commands
13 to operate on the files listed.
15 The Dired-X package provides various extra features for Dired mode.
16 @xref{,Dired-X,,dired-x, Dired Extra Version 2 User's Manual}.
19 * Enter: Dired Enter. How to invoke Dired.
20 * Commands: Dired Commands. Commands in the Dired buffer.
21 * Deletion: Dired Deletion. Deleting files with Dired.
22 * Flagging Many Files:: Flagging files based on their names.
23 * Visit: Dired Visiting. Other file operations through Dired.
24 * Marks vs Flags:: Flagging for deletion vs marking.
25 * Operating on Files:: How to copy, rename, print, compress, etc.
26 either one file or several files.
27 * Shell Commands in Dired:: Running a shell command on the marked files.
28 * Transforming File Names:: Using patterns to rename multiple files.
29 * Comparison in Dired:: Running `diff' by way of Dired.
30 * Subdirectories in Dired:: Adding subdirectories to the Dired buffer.
31 * Subdirectory Motion:: Moving across subdirectories, and up and down.
32 * Hiding Subdirectories:: Making subdirectories visible or invisible.
33 * Updating: Dired Updating. Discarding lines for files of no interest.
34 * Find: Dired and Find. Using `find' to choose the files for Dired.
38 @section Entering Dired
42 @vindex dired-listing-switches
43 To invoke Dired, do @kbd{C-x d} or @kbd{M-x dired}. The command reads
44 a directory name or wildcard file name pattern as a minibuffer argument
45 to specify which files to list. Where @code{dired} differs from
46 @code{list-directory} is in putting the buffer into Dired mode so that
47 the special commands of Dired are available.
49 The variable @code{dired-listing-switches} specifies the options to
50 give to @code{ls} for listing directory; this string @emph{must} contain
51 @samp{-l}. If you use a numeric prefix argument with the @code{dired}
52 command, you can specify the @code{ls} switches with the minibuffer
53 before you enter the directory specification.
55 @findex dired-other-window
57 @findex dired-other-frame
59 To display the Dired buffer in another window rather than in the
60 selected window, use @kbd{C-x 4 d} (@code{dired-other-window}) instead
61 of @kbd{C-x d}. @kbd{C-x 5 d} (@code{dired-other-frame}) uses a
62 separate frame to display the Dired buffer.
65 @section Commands in the Dired Buffer
67 The Dired buffer is ``read-only,'' and inserting text in it is not
68 useful, so ordinary printing characters such as @kbd{d} and @kbd{x} are
69 used for special Dired commands. Some Dired commands @dfn{mark} or
70 @dfn{flag} the @dfn{current file} (that is, the file on the current
71 line); other commands operate on the marked files or on the flagged
74 @kindex C-n @r{(Dired)}
75 @kindex C-p @r{(Dired)}
76 All the usual Emacs cursor motion commands are available in Dired
77 buffers. Some special-purpose cursor motion commands are also
78 provided. The keys @kbd{C-n} and @kbd{C-p} are redefined to put the
79 cursor at the beginning of the file name on the line, rather than at the
80 beginning of the line.
82 @kindex SPC @r{(Dired)}
83 For extra convenience, @key{SPC} and @kbd{n} in Dired are equivalent
84 to @kbd{C-n}. @kbd{p} is equivalent to @kbd{C-p}. (Moving by lines is
85 so common in Dired that it deserves to be easy to type.) @key{DEL}
86 (move up and unflag) is often useful simply for moving up.
89 @section Deleting Files with Dired
90 @cindex flagging files (in Dired)
91 @cindex deleting files (in Dired)
93 The primary use of Dired is to @dfn{flag} files for deletion and then
94 delete the files previously flagged.
98 Flag this file for deletion.
100 Remove deletion flag on this line.
102 Move point to previous line and remove the deletion flag on that line.
104 Delete the files that are flagged for deletion.
107 @kindex d @r{(Dired)}
108 @findex dired-flag-file-deletion
109 You can flag a file for deletion by moving to the line describing the
110 file and typing @kbd{d} (@code{dired-flag-file-deletion}). The deletion flag is visible as a @samp{D} at
111 the beginning of the line. This command moves point to the next line,
112 so that repeated @kbd{d} commands flag successive files. A numeric
113 argument serves as a repeat count.
115 @vindex dired-recursive-deletes
116 The variable @code{dired-recursive-deletes} controls whether the
117 delete command will delete non-empty directories (including their
118 contents). The default is to delete only empty directories.
120 @kindex u @r{(Dired deletion)}
121 @kindex DEL @r{(Dired)}
122 The files are flagged for deletion rather than deleted immediately to
123 reduce the danger of deleting a file accidentally. Until you direct
124 Dired to expunge the flagged files, you can remove deletion flags using
125 the commands @kbd{u} and @key{DEL}. @kbd{u} (@code{dired-unmark}) works
126 just like @kbd{d}, but removes flags rather than making flags.
127 @key{DEL} (@code{dired-unmark-backward}) moves upward, removing flags;
128 it is like @kbd{u} with argument @minus{}1.
130 @kindex x @r{(Dired)}
131 @findex dired-expunge
132 @cindex expunging (Dired)
133 To delete the flagged files, type @kbd{x} (@code{dired-expunge}).
134 This command first displays a list of all the file names flagged for
135 deletion, and requests confirmation with @kbd{yes}. If you confirm,
136 Dired deletes the flagged files, then deletes their lines from the text
137 of the Dired buffer. The shortened Dired buffer remains selected.
139 If you answer @kbd{no} or quit with @kbd{C-g} when asked to confirm, you
140 return immediately to Dired, with the deletion flags still present in
141 the buffer, and no files actually deleted.
143 @node Flagging Many Files
144 @section Flagging Many Files at Once
145 @cindex flagging many files for deletion (in Dired)
149 Flag all auto-save files (files whose names start and end with @samp{#})
150 for deletion (@pxref{Auto Save}).
153 Flag all backup files (files whose names end with @samp{~}) for deletion
157 Flag for deletion all files with certain kinds of names, names that
158 suggest you could easily create the files again.
160 @item .@: @r{(Period)}
161 Flag excess numeric backup files for deletion. The oldest and newest
162 few backup files of any one file are exempt; the middle ones are
165 @item % d @var{regexp} @key{RET}
166 Flag for deletion all files whose names match the regular expression
170 The @kbd{#}, @kbd{~}, @kbd{&}, and @kbd{.} commands flag many files for
171 deletion, based on their file names. These commands are useful
172 precisely because they do not themselves delete any files; you can
173 remove the deletion flags from any flagged files that you really wish to
176 @kindex & @r{(Dired)}
177 @findex dired-flag-garbage-files
178 @vindex dired-garbage-files-regexp
179 @cindex deleting some backup files
180 @kbd{&} (@code{dired-flag-garbage-files}) flags files whose names
181 match the regular expression specified by the variable
182 @code{dired-garbage-files-regexp}. By default, this matches certain
183 files produced by @TeX{}, and the @samp{.orig} and @samp{.rej} files
184 produced by @code{patch}.
186 @kindex # @r{(Dired)}
187 @kindex ~ @r{(Dired)}
188 @findex dired-flag-auto-save-files
189 @findex dired-flag-backup-files
190 @cindex deleting auto-save files
191 @kbd{#} (@code{dired-flag-auto-save-files}) flags for deletion all
192 files whose names look like auto-save files (@pxref{Auto Save})---that
193 is, files whose names begin and end with @samp{#}. @kbd{~}
194 (@code{dired-flag-backup-files}) flags for deletion all files whose
195 names say they are backup files (@pxref{Backup})---that is, whose names
198 @kindex . @r{(Dired)}
199 @vindex dired-kept-versions
200 @findex dired-clean-directory
201 @kbd{.} (period, @code{dired-clean-directory}) flags just some of the
202 backup files for deletion: all but the oldest few and newest few backups
203 of any one file. Normally @code{dired-kept-versions} (@strong{not}
204 @code{kept-new-versions}; that applies only when saving) specifies the
205 number of newest versions of each file to keep, and
206 @code{kept-old-versions} specifies the number of oldest versions to
209 Period with a positive numeric argument, as in @kbd{C-u 3 .},
210 specifies the number of newest versions to keep, overriding
211 @code{dired-kept-versions}. A negative numeric argument overrides
212 @code{kept-old-versions}, using minus the value of the argument to
213 specify the number of oldest versions of each file to keep.
215 @findex dired-flag-files-regexp
216 @kindex % d @r{(Dired)}
217 The @kbd{% d} command flags all files whose names match a specified
218 regular expression (@code{dired-flag-files-regexp}). Only the
219 non-directory part of the file name is used in matching. You can use
220 @samp{^} and @samp{$} to anchor matches. You can exclude subdirectories
221 by hiding them (@pxref{Hiding Subdirectories}).
224 @section Visiting Files in Dired
226 There are several Dired commands for visiting or examining the files
227 listed in the Dired buffer. All of them apply to the current line's
228 file; if that file is really a directory, these commands invoke Dired on
229 that subdirectory (making a separate Dired buffer).
233 @kindex f @r{(Dired)}
234 @findex dired-find-file
235 Visit the file described on the current line, like typing @kbd{C-x C-f}
236 and supplying that file name (@code{dired-find-file}). @xref{Visiting}.
239 @kindex RET @r{(Dired)}
240 Equivalent to @kbd{f}.
243 @kindex a @r{(Dired)}
244 @findex dired-find-alternate-file
245 Like @kbd{f}, but replaces the contents of the Dired buffer with
246 that of an alternate file or directory.
249 @kindex o @r{(Dired)}
250 @findex dired-find-file-other-window
251 Like @kbd{f}, but uses another window to display the file's buffer
252 (@code{dired-find-file-other-window}). The Dired buffer remains visible
253 in the first window. This is like using @kbd{C-x 4 C-f} to visit the
254 file. @xref{Windows}.
257 @kindex C-o @r{(Dired)}
258 @findex dired-display-file
259 Visit the file described on the current line, and display the buffer in
260 another window, but do not select that window (@code{dired-display-file}).
263 @findex dired-mouse-find-file-other-window
264 Visit the file named by the line you click on
265 (@code{dired-mouse-find-file-other-window}). This uses another window
266 to display the file, like the @kbd{o} command.
269 @kindex v @r{(Dired)}
270 @findex dired-view-file
271 View the file described on the current line, using @kbd{M-x view-file}
272 (@code{dired-view-file}).
274 Viewing a file is like visiting it, but is slanted toward moving around
275 in the file conveniently and does not allow changing the file.
276 @xref{Misc File Ops,View File}.
280 @section Dired Marks vs. Flags
282 @cindex marking many files (in Dired)
283 Instead of flagging a file with @samp{D}, you can @dfn{mark} the file
284 with some other character (usually @samp{*}). Most Dired commands to
285 operate on files, aside from ``expunge'' (@kbd{x}), look for files
286 marked with @samp{*}.
288 Here are some commands for marking with @samp{*}, or for unmarking or
289 operating on marks. (@xref{Dired Deletion}, for commands to flag and
295 @kindex m @r{(Dired)}
296 @kindex * m @r{(Dired)}
298 Mark the current file with @samp{*} (@code{dired-mark}). With a numeric
299 argument @var{n}, mark the next @var{n} files starting with the current
300 file. (If @var{n} is negative, mark the previous @minus{}@var{n}
304 @kindex * * @r{(Dired)}
305 @findex dired-mark-executables
306 @cindex marking executable files (in Dired)
307 Mark all executable files with @samp{*}
308 (@code{dired-mark-executables}). With a numeric argument, unmark all
312 @kindex * @@ @r{(Dired)}
313 @findex dired-mark-symlinks
314 @cindex marking symlinks (in Dired)
315 Mark all symbolic links with @samp{*} (@code{dired-mark-symlinks}).
316 With a numeric argument, unmark all those files.
319 @kindex * / @r{(Dired)}
320 @findex dired-mark-directories
321 @cindex marking subdirectories (in Dired)
322 Mark with @samp{*} all files which are actually directories, except for
323 @file{.} and @file{..} (@code{dired-mark-directories}). With a numeric
324 argument, unmark all those files.
327 @kindex * s @r{(Dired)}
328 @findex dired-mark-subdir-files
329 Mark all the files in the current subdirectory, aside from @file{.}
330 and @file{..} (@code{dired-mark-subdir-files}).
334 @kindex u @r{(Dired)}
335 @kindex * u @r{(Dired)}
337 Remove any mark on this line (@code{dired-unmark}).
341 @kindex * DEL @r{(Dired)}
342 @findex dired-unmark-backward
343 @cindex unmarking files (in Dired)
344 Move point to previous line and remove any mark on that line
345 (@code{dired-unmark-backward}).
348 @kindex * ! @r{(Dired)}
349 @findex dired-unmark-all-files-no-query
350 Remove all marks from all the files in this Dired buffer
351 (@code{dired-unmark-all-files-no-query}).
353 @item * ? @var{markchar}
354 @kindex * ? @r{(Dired)}
355 @findex dired-unmark-all-files
356 Remove all marks that use the character @var{markchar}
357 (@code{dired-unmark-all-files}). The argument is a single
358 character---do not use @key{RET} to terminate it.
360 With a numeric argument, this command queries about each marked file,
361 asking whether to remove its mark. You can answer @kbd{y} meaning yes,
362 @kbd{n} meaning no, or @kbd{!} to remove the marks from the remaining
363 files without asking about them.
366 @findex dired-next-marked-file
367 @kindex * C-n @r{(Dired)}
368 Move down to the next marked file (@code{dired-next-marked-file})
369 A file is ``marked'' if it has any kind of mark.
372 @findex dired-prev-marked-file
373 @kindex * C-p @r{(Dired)}
374 Move up to the previous marked file (@code{dired-prev-marked-file})
377 @kindex * t @r{(Dired)}
378 @findex dired-do-toggle
379 @cindex toggling marks (in Dired)
380 Toggle all marks (@code{dired-do-toggle}): files marked with @samp{*}
381 become unmarked, and unmarked files are marked with @samp{*}. Files
382 marked in any other way are not affected.
384 @item * c @var{old} @var{new}
385 @kindex * c @r{(Dired)}
386 @findex dired-change-marks
387 Replace all marks that use the character @var{old} with marks that use
388 the character @var{new} (@code{dired-change-marks}). This command is
389 the primary way to create or use marks other than @samp{*} or @samp{D}.
390 The arguments are single characters---do not use @key{RET} to terminate
393 You can use almost any character as a mark character by means of this
394 command, to distinguish various classes of files. If @var{old} is a
395 space (@samp{ }), then the command operates on all unmarked files; if
396 @var{new} is a space, then the command unmarks the files it acts on.
398 To illustrate the power of this command, here is how to put @samp{D}
399 flags on all the files that have no marks, while unflagging all those
400 that already have @samp{D} flags:
403 * c D t * c SPC D * c t SPC
406 This assumes that no files are marked with @samp{t}.
408 @item % m @var{regexp} @key{RET}
409 @itemx * % @var{regexp} @key{RET}
410 @findex dired-mark-files-regexp
411 @kindex % m @r{(Dired)}
412 @kindex * % @r{(Dired)}
413 Mark (with @samp{*}) all files whose names match the regular expression
414 @var{regexp} (@code{dired-mark-files-regexp}). This command is like
415 @kbd{% d}, except that it marks files with @samp{*} instead of flagging
416 with @samp{D}. @xref{Flagging Many Files}.
418 Only the non-directory part of the file name is used in matching. Use
419 @samp{^} and @samp{$} to anchor matches. Exclude subdirectories by
420 hiding them (@pxref{Hiding Subdirectories}).
422 @item % g @var{regexp} @key{RET}
423 @findex dired-mark-files-containing-regexp
424 @kindex % g @r{(Dired)}
425 @cindex finding files containing regexp matches (in Dired)
426 Mark (with @samp{*}) all files whose @emph{contents} contain a match for
427 the regular expression @var{regexp}
428 (@code{dired-mark-files-containing-regexp}). This command is like
429 @kbd{% m}, except that it searches the file contents instead of the file
433 @kindex C-_ @r{(Dired)}
435 Undo changes in the Dired buffer, such as adding or removing
436 marks (@code{dired-undo}).
439 @node Operating on Files
440 @section Operating on Files
441 @cindex operating on files in Dired
443 This section describes the basic Dired commands to operate on one file
444 or several files. All of these commands are capital letters; all of
445 them use the minibuffer, either to read an argument or to ask for
446 confirmation, before they act. All of them give you several ways to
447 specify which files to manipulate:
451 If you give the command a numeric prefix argument @var{n}, it operates
452 on the next @var{n} files, starting with the current file. (If @var{n}
453 is negative, the command operates on the @minus{}@var{n} files preceding
457 Otherwise, if some files are marked with @samp{*}, the command operates
461 Otherwise, the command operates on the current file only.
464 Here are the file-manipulating commands that operate on files in this
465 way. (Some other Dired commands, such as @kbd{!} and the @samp{%}
466 commands, also use these conventions to decide which files to work on.)
469 @findex dired-do-copy
470 @kindex C @r{(Dired)}
471 @cindex copying files (in Dired)
472 @item C @var{new} @key{RET}
473 Copy the specified files (@code{dired-do-copy}). The argument @var{new}
474 is the directory to copy into, or (if copying a single file) the new
477 @vindex dired-copy-preserve-time
478 If @code{dired-copy-preserve-time} is non-@code{nil}, then copying with
479 this command sets the modification time of the new file to be the same
480 as that of the old file.
482 @vindex dired-recursive-copies
483 The variable @code{dired-recursive-copies} controls whether
484 directories are copied recursively. The default is to not copy
485 recursively, which means that directories cannot be copied.
488 @findex dired-do-delete
489 @kindex D @r{(Dired)}
490 Delete the specified files (@code{dired-do-delete}). Like the other
491 commands in this section, this command operates on the @emph{marked}
492 files, or the next @var{n} files. By contrast, @kbd{x}
493 (@code{dired-expunge}) deletes all @dfn{flagged} files.
495 @findex dired-do-rename
496 @kindex R @r{(Dired)}
497 @cindex renaming files (in Dired)
498 @item R @var{new} @key{RET}
499 Rename the specified files (@code{dired-do-rename}). The argument
500 @var{new} is the directory to rename into, or (if renaming a single
503 Dired automatically changes the visited file name of buffers associated
504 with renamed files so that they refer to the new names.
506 @findex dired-do-hardlink
507 @kindex H @r{(Dired)}
508 @cindex hard links (in Dired)
509 @item H @var{new} @key{RET}
510 Make hard links to the specified files (@code{dired-do-hardlink}). The
511 argument @var{new} is the directory to make the links in, or (if making
512 just one link) the name to give the link.
514 @findex dired-do-symlink
515 @kindex S @r{(Dired)}
516 @cindex symlinks (in Dired)
517 @item S @var{new} @key{RET}
518 Make symbolic links to the specified files (@code{dired-do-symlink}).
519 The argument @var{new} is the directory to make the links in, or (if
520 making just one link) the name to give the link.
522 @findex dired-do-chmod
523 @kindex M @r{(Dired)}
524 @cindex changing file permissions (in Dired)
525 @item M @var{modespec} @key{RET}
526 Change the mode (also called ``permission bits'') of the specified files
527 (@code{dired-do-chmod}). This uses the @code{chmod} program, so
528 @var{modespec} can be any argument that @code{chmod} can handle.
530 @findex dired-do-chgrp
531 @kindex G @r{(Dired)}
532 @cindex changing file group ownership (in Dired)
533 @item G @var{newgroup} @key{RET}
534 Change the group of the specified files to @var{newgroup}
535 (@code{dired-do-chgrp}).
537 @findex dired-do-chown
538 @kindex O @r{(Dired)}
539 @cindex changing file owner (in Dired)
540 @item O @var{newowner} @key{RET}
541 Change the owner of the specified files to @var{newowner}
542 (@code{dired-do-chown}). (On most systems, only the superuser can do
545 @vindex dired-chown-program
546 The variable @code{dired-chown-program} specifies the name of the
547 program to use to do the work (different systems put @code{chown} in
550 @findex dired-do-print
551 @kindex P @r{(Dired)}
552 @cindex printing files (in Dired)
553 @item P @var{command} @key{RET}
554 Print the specified files (@code{dired-do-print}). You must specify the
555 command to print them with, but the minibuffer starts out with a
556 suitable guess made using the variables @code{lpr-command} and
557 @code{lpr-switches} (the same variables that @code{lpr-buffer} uses;
560 @findex dired-do-compress
561 @kindex Z @r{(Dired)}
562 @cindex compressing files (in Dired)
564 Compress the specified files (@code{dired-do-compress}). If the file
565 appears to be a compressed file already, it is uncompressed instead.
567 @findex dired-do-load
568 @kindex L @r{(Dired)}
569 @cindex loading several files (in Dired)
571 Load the specified Emacs Lisp files (@code{dired-do-load}).
572 @xref{Lisp Libraries}.
574 @findex dired-do-byte-compile
575 @kindex B @r{(Dired)}
576 @cindex byte-compiling several files (in Dired)
578 Byte compile the specified Emacs Lisp files
579 (@code{dired-do-byte-compile}). @xref{Byte Compilation,, Byte
580 Compilation, elisp, The Emacs Lisp Reference Manual}.
582 @kindex A @r{(Dired)}
583 @findex dired-do-search
584 @cindex search multiple files (in Dired)
585 @item A @var{regexp} @key{RET}
586 Search all the specified files for the regular expression @var{regexp}
587 (@code{dired-do-search}).
589 This command is a variant of @code{tags-search}. The search stops at
590 the first match it finds; use @kbd{M-,} to resume the search and find
591 the next match. @xref{Tags Search}.
593 @kindex Q @r{(Dired)}
594 @findex dired-do-query-replace
595 @cindex search and replace in multiple files (in Dired)
596 @item Q @var{from} @key{RET} @var{to} @key{RET}
597 Perform @code{query-replace-regexp} on each of the specified files,
598 replacing matches for @var{from} (a regular expression) with the string
599 @var{to} (@code{dired-do-query-replace}).
601 This command is a variant of @code{tags-query-replace}. If you exit the
602 query replace loop, you can use @kbd{M-,} to resume the scan and replace
603 more matches. @xref{Tags Search}.
605 @kindex a @r{(Dired)}
606 @findex dired-do-apply
607 @cindex apply arbitrary function to many files
608 @item a @var{function} @kbd{RET}
609 Apply an arbitrary Lisp function to the name of each marked file
610 (@code{dired-do-apply}).
613 @kindex + @r{(Dired)}
614 @findex dired-create-directory
615 One special file-operation command is @kbd{+}
616 (@code{dired-create-directory}). This command reads a directory name and
617 creates the directory if it does not already exist.
619 @node Shell Commands in Dired
620 @section Shell Commands in Dired
621 @cindex shell commands, Dired
623 @findex dired-do-shell-command
624 @kindex ! @r{(Dired)}
625 The dired command @kbd{!} (@code{dired-do-shell-command}) reads a shell
626 command string in the minibuffer and runs that shell command on all the
627 specified files. You can specify the files to operate on in the usual
628 ways for Dired commands (@pxref{Operating on Files}). There are two
629 ways of applying a shell command to multiple files:
633 If you use @samp{*} in the shell command, then it runs just once, with
634 the list of file names substituted for the @samp{*}. The order of file
635 names is the order of appearance in the Dired buffer.
637 Thus, @kbd{! tar cf foo.tar * @key{RET}} runs @code{tar} on the entire
638 list of file names, putting them into one tar file @file{foo.tar}.
641 If the command string doesn't contain @samp{*}, then it runs once
642 @emph{for each file}, with the file name added at the end.
644 For example, @kbd{! uudecode @key{RET}} runs @code{uudecode} on each
648 What if you want to run the shell command once for each file, with the
649 file name inserted in the middle? You can use @samp{?} in the command
650 instead of @samp{*}. The current file name is substituted for
651 @samp{?}. You can use @samp{?} more than once. For instance, here is
652 how to uuencode each file, making the output file name by appending
653 @samp{.uu} to the input file name:
659 To use the file names in a more complicated fashion, you can use a
660 shell loop. For example, this shell command is another way to
664 for file in *; do uuencode $file $file >$file.uu; done
667 The working directory for the shell command is the top-level directory
670 The @kbd{!} command does not attempt to update the Dired buffer to show
671 new or modified files, because it doesn't really understand shell
672 commands, and does not know what files the shell command changed. Use
673 the @kbd{g} command to update the Dired buffer (@pxref{Dired
676 @node Transforming File Names
677 @section Transforming File Names in Dired
679 Here are commands that alter file names in a systematic way:
683 @kindex % u @r{(Dired)}
684 @cindex upcase file names
686 Rename each of the selected files to an upper-case name
687 (@code{dired-upcase}). If the old file names are @file{Foo}
688 and @file{bar}, the new names are @file{FOO} and @file{BAR}.
691 @findex dired-downcase
692 @kindex % l @r{(Dired)}
693 @cindex downcase file names
694 Rename each of the selected files to a lower-case name
695 (@code{dired-downcase}). If the old file names are @file{Foo} and
696 @file{bar}, the new names are @file{foo} and @file{bar}.
698 @item % R @var{from} @key{RET} @var{to} @key{RET}
699 @kindex % R @r{(Dired)}
700 @findex dired-do-rename-regexp
701 @itemx % C @var{from} @key{RET} @var{to} @key{RET}
702 @kindex % C @r{(Dired)}
703 @findex dired-do-copy-regexp
704 @itemx % H @var{from} @key{RET} @var{to} @key{RET}
705 @kindex % H @r{(Dired)}
706 @findex dired-do-hardlink-regexp
707 @itemx % S @var{from} @key{RET} @var{to} @key{RET}
708 @kindex % S @r{(Dired)}
709 @findex dired-do-symlink-regexp
710 These four commands rename, copy, make hard links and make soft links,
711 in each case computing the new name by regular-expression substitution
712 from the name of the old file.
715 The four regular-expression substitution commands effectively perform
716 a search-and-replace on the selected file names in the Dired buffer.
717 They read two arguments: a regular expression @var{from}, and a
718 substitution pattern @var{to}.
720 The commands match each ``old'' file name against the regular
721 expression @var{from}, and then replace the matching part with @var{to}.
722 You can use @samp{\&} and @samp{\@var{digit}} in @var{to} to refer to
723 all or part of what the pattern matched in the old file name, as in
724 @code{replace-regexp} (@pxref{Regexp Replace}). If the regular expression
725 matches more than once in a file name, only the first match is replaced.
727 For example, @kbd{% R ^.*$ @key{RET} x-\& @key{RET}} renames each
728 selected file by prepending @samp{x-} to its name. The inverse of this,
729 removing @samp{x-} from the front of each file name, is also possible:
730 one method is @kbd{% R ^x-\(.*\)$ @key{RET} \1 @key{RET}}; another is
731 @kbd{% R ^x- @key{RET} @key{RET}}. (Use @samp{^} and @samp{$} to anchor
732 matches that should span the whole filename.)
734 Normally, the replacement process does not consider the files'
735 directory names; it operates on the file name within the directory. If
736 you specify a numeric argument of zero, then replacement affects the
737 entire absolute file name including directory name.
739 Often you will want to select the set of files to operate on using the
740 same @var{regexp} that you will use to operate on them. To do this,
741 mark those files with @kbd{% m @var{regexp} @key{RET}}, then use the
742 same regular expression in the command to operate on the files. To make
743 this easier, the @kbd{%} commands to operate on files use the last
744 regular expression specified in any @kbd{%} command as a default.
746 @node Comparison in Dired
747 @section File Comparison with Dired
748 @cindex file comparison (in Dired)
749 @cindex compare files (in Dired)
751 Here are two Dired commands that compare specified files using
757 @kindex = @r{(Dired)}
758 Compare the current file (the file at point) with another file (the file
759 at the mark) using the @code{diff} program (@code{dired-diff}). The
760 file at the mark is the first argument of @code{diff}, and the file at
761 point is the second argument.
763 @findex dired-backup-diff
764 @kindex M-= @r{(Dired)}
766 Compare the current file with its latest backup file
767 (@code{dired-backup-diff}). If the current file is itself a backup,
768 compare it with the file it is a backup of; this way, you can compare
769 a file with any backup version of your choice.
771 The backup file is the first file given to @code{diff}.
774 @node Subdirectories in Dired
775 @section Subdirectories in Dired
776 @cindex subdirectories in Dired
777 @cindex expanding subdirectories in Dired
779 A Dired buffer displays just one directory in the normal case;
780 but you can optionally include its subdirectories as well.
782 The simplest way to include multiple directories in one Dired buffer is
783 to specify the options @samp{-lR} for running @code{ls}. (If you give a
784 numeric argument when you run Dired, then you can specify these options
785 in the minibuffer.) That produces a recursive directory listing showing
786 all subdirectories at all levels.
788 But usually all the subdirectories are too many; usually you will
789 prefer to include specific subdirectories only. You can do this with
793 @findex dired-maybe-insert-subdir
794 @kindex i @r{(Dired)}
796 @cindex inserted subdirectory (Dired)
797 @cindex in-situ subdirectory (Dired)
798 Insert the contents of a subdirectory later in the buffer.
801 Use the @kbd{i} (@code{dired-maybe-insert-subdir}) command on a line
802 that describes a file which is a directory. It inserts the contents of
803 that directory into the same Dired buffer, and moves there. Inserted
804 subdirectory contents follow the top-level directory of the Dired
805 buffer, just as they do in @samp{ls -lR} output.
807 If the subdirectory's contents are already present in the buffer, the
808 @kbd{i} command just moves to it.
810 In either case, @kbd{i} sets the Emacs mark before moving, so @kbd{C-u
811 C-@key{SPC}} takes you back to the old position in the buffer (the line
812 describing that subdirectory).
814 Use the @kbd{l} command (@code{dired-do-redisplay}) to update the
815 subdirectory's contents. Use @kbd{k} to delete the subdirectory.
816 @xref{Dired Updating}.
818 @node Subdirectory Motion
819 @section Moving Over Subdirectories
821 When a Dired buffer lists subdirectories, you can use the page motion
822 commands @kbd{C-x [} and @kbd{C-x ]} to move by entire directories.
824 @cindex header line (Dired)
825 @cindex directory header lines
826 The following commands move across, up and down in the tree of
827 directories within one Dired buffer. They move to @dfn{directory header
828 lines}, which are the lines that give a directory's name, at the
829 beginning of the directory's contents.
832 @findex dired-next-subdir
833 @kindex C-M-n @r{(Dired)}
835 Go to next subdirectory header line, regardless of level
836 (@code{dired-next-subdir}).
838 @findex dired-prev-subdir
839 @kindex C-M-p @r{(Dired)}
841 Go to previous subdirectory header line, regardless of level
842 (@code{dired-prev-subdir}).
844 @findex dired-tree-up
845 @kindex C-M-u @r{(Dired)}
847 Go up to the parent directory's header line (@code{dired-tree-up}).
849 @findex dired-tree-down
850 @kindex C-M-d @r{(Dired)}
852 Go down in the directory tree, to the first subdirectory's header line
853 (@code{dired-tree-down}).
855 @findex dired-prev-dirline
856 @kindex < @r{(Dired)}
858 Move up to the previous directory-file line (@code{dired-prev-dirline}).
859 These lines are the ones that describe a directory as a file in its
862 @findex dired-next-dirline
863 @kindex > @r{(Dired)}
865 Move down to the next directory-file line (@code{dired-prev-dirline}).
868 @node Hiding Subdirectories
869 @section Hiding Subdirectories
871 @cindex hiding in Dired (Dired)
872 @dfn{Hiding} a subdirectory means to make it invisible, except for its
873 header line, via selective display (@pxref{Selective Display}).
877 @findex dired-hide-subdir
878 @kindex $ @r{(Dired)}
879 Hide or reveal the subdirectory that point is in, and move point to the
880 next subdirectory (@code{dired-hide-subdir}). A numeric argument serves
884 @findex dired-hide-all
885 @kindex M-$ @r{(Dired)}
886 Hide all subdirectories in this Dired buffer, leaving only their header
887 lines (@code{dired-hide-all}). Or, if any subdirectory is currently
888 hidden, make all subdirectories visible again. You can use this command
889 to get an overview in very deep directory trees or to move quickly to
890 subdirectories far away.
893 Ordinary Dired commands never consider files inside a hidden
894 subdirectory. For example, the commands to operate on marked files
895 ignore files in hidden directories even if they are marked. Thus you
896 can use hiding to temporarily exclude subdirectories from operations
897 without having to remove the markers.
899 The subdirectory hiding commands toggle; that is, they hide what was
900 visible, and show what was hidden.
903 @section Updating the Dired Buffer
904 @cindex updating Dired buffer
905 @cindex refreshing displayed files
907 This section describes commands to update the Dired buffer to reflect
908 outside (non-Dired) changes in the directories and files, and to delete
909 part of the Dired buffer.
913 Update the entire contents of the Dired buffer (@code{revert-buffer}).
916 Update the specified files (@code{dired-do-redisplay}).
919 Delete the specified @emph{file lines}---not the files, just the lines
920 (@code{dired-do-kill-lines}).
923 Toggle between alphabetical order and date/time order
924 (@code{dired-sort-toggle-or-edit}).
926 @item C-u s @var{switches} @key{RET}
927 Refresh the Dired buffer using @var{switches} as
928 @code{dired-listing-switches}.
931 @kindex g @r{(Dired)}
932 @findex revert-buffer @r{(Dired)}
933 Type @kbd{g} (@code{revert-buffer}) to update the contents of the
934 Dired buffer, based on changes in the files and directories listed.
935 This preserves all marks except for those on files that have vanished.
936 Hidden subdirectories are updated but remain hidden.
938 @kindex l @r{(Dired)}
939 @findex dired-do-redisplay
940 To update only some of the files, type @kbd{l}
941 (@code{dired-do-redisplay}). This command applies to the next @var{n}
942 files, or to the marked files if any, or to the current file. Updating
943 them means reading their current status from the file system and
944 changing the buffer to reflect it properly.
946 If you use @kbd{l} on a subdirectory header line, it updates the
947 contents of the corresponding subdirectory.
949 @kindex k @r{(Dired)}
950 @findex dired-do-kill-lines
951 To delete the specified @emph{file lines}---not the files, just the
952 lines---type @kbd{k} (@code{dired-do-kill-lines}). With a numeric
953 argument @var{n}, this command applies to the next @var{n} files;
954 otherwise, it applies to the marked files.
956 If you kill the line for a file that is a directory, the directory's
957 contents are also deleted from the buffer. Typing @kbd{C-u k} on the
958 header line for a subdirectory is another way to delete a subdirectory
959 from the Dired buffer.
961 The @kbd{g} command brings back any individual lines that you have
962 killed in this way, but not subdirectories---you must use @kbd{i} to
963 reinsert each subdirectory.
965 @cindex Dired sorting
966 @cindex sorting Dired buffer
967 @kindex s @r{(Dired)}
968 @findex dired-sort-toggle-or-edit
969 The files in a Dired buffers are normally listed in alphabetical order
970 by file names. Alternatively Dired can sort them by date/time. The
971 Dired command @kbd{s} (@code{dired-sort-toggle-or-edit}) switches
972 between these two sorting modes. The mode line in a Dired buffer
973 indicates which way it is currently sorted---by name, or by date.
975 @kbd{C-u s @var{switches} @key{RET}} lets you specify a new value for
976 @code{dired-listing-switches}.
979 @section Dired and @code{find}
980 @cindex @code{find} and Dired
982 You can select a set of files for display in a Dired buffer more
983 flexibly by using the @code{find} utility to choose the files.
985 @findex find-name-dired
986 To search for files with names matching a wildcard pattern use
987 @kbd{M-x find-name-dired}. It reads arguments @var{directory} and
988 @var{pattern}, and chooses all the files in @var{directory} or its
989 subdirectories whose individual names match @var{pattern}.
991 The files thus chosen are displayed in a Dired buffer in which the
992 ordinary Dired commands are available.
994 @findex find-grep-dired
995 If you want to test the contents of files, rather than their names,
996 use @kbd{M-x find-grep-dired}. This command reads two minibuffer
997 arguments, @var{directory} and @var{regexp}; it chooses all the files in
998 @var{directory} or its subdirectories that contain a match for
999 @var{regexp}. It works by running the programs @code{find} and
1000 @code{grep}. See also @kbd{M-x grep-find}, in @ref{Compilation}.
1001 Remember to write the regular expression for @code{grep}, not for Emacs.
1002 (An alternative method of showing files whose contents match a given
1003 regexp is the @kbd{% g @var{regexp}} command, see @ref{Marks vs Flags}.)
1006 The most general command in this series is @kbd{M-x find-dired}, which
1007 lets you specify any condition that @code{find} can test. It takes two
1008 minibuffer arguments, @var{directory} and @var{find-args}; it runs
1009 @code{find} in @var{directory}, passing @var{find-args} to tell
1010 @code{find} what condition to test. To use this command, you need to
1011 know how to use @code{find}.
1015 @findex locate-with-filter
1016 @cindex file database (locate)
1017 @vindex locate-command
1018 @kbd{M-x locate} provides a similar interface to the @code{locate}.
1019 @kbd{M-x locate-with-filter} is similar, but keeps only lines matching
1020 a given regular expression.
1022 @vindex find-ls-option
1023 The format of listing produced by these commands is controlled by the
1024 variable @code{find-ls-option}, whose default value specifies using
1025 options @samp{-ld} for @code{ls}. If your listings are corrupted, you
1026 may need to change the value of this variable.