Dired: marking, copying, moving and deleting files

Following from our introduction to dired, here is an example of how to use dired for simple file operations. You can do much more with dired, and we will return to it in the future.

Open dired for a directory containing files you want to work with. Then use C, R, or D when the cursor is on the line of a file to copy, rename/move or delete the file, respectively. This can also be done for multiple files by marking them.

You can mark files in dired by pressing m on the line of a file, and unmark it by pressing u. You can mark several files individually using m or mark all files whose names match a search string by using % m string <RET>. Use U to unmark all files.

The strings used for matching are regular expressions, so are very powerful. For example:

  • % m test <RET> will mark all files with names containing the string “test” (equivalent to *test* on the command line.
  • % m ^test <RET> will mark all files with names starting with the string “test” (equivalent to test* on the command line).
  • % m txt$ <RET> will mark all files with names ending with the string “txt” (equivalent to *txt on the command line).
  • % m ^test.*org$ <RET> will mark all files with names starting with the string “test” and ending with “org” (equivalent to test*org on the command line).
  • % m [kxj] <RET> will mark all files with names containing the letters k, x, or j
  • % m [kxj] <RET> will mark all files with names containing the letters k, x, or j
  • % m [6-9] <RET> will mark all files with names containing the digits 6,7,8,9

Read more about emacs’ regular expressions here.

Once you have marked files, use C, R, or D as before to copy, move or delete them.

You can invert the marking of files using * t, and mark all files (or directories) using * s.

You can hide marked files from the current dired view using k. Note that these are not deleted, use g to see them again.

So, supposing you wanted to delete a bunch of files, but wanted to see them first to visually check you are deleting the right ones, you might do the following

  1. mark the files manually, or using a regular expression
  2. * t invert the selection
  3. k hide the selected files so now you see the ones you originally selected for deletion so you can check you got the right ones
  4. * s to select those visible files
  5. D to delete them

This animation illustrates me going through these steps when I have selected all files ending “txt”.

dired-files.gif