Dired: rename multiple files

Following on from the previous dired tips we can do much more in dired. A very powerful feature is that you can switch the dired buffer which lists the files into editable mode. Then you can directly edit file names, links and so on. Here we will look at using dired to rename multiple files in a nice visual way.

Now we will use dired to rename multiple files at the same time. Suppose in some directory we have a bunch of files called test_foo_01.dat, test_foo_02.dat, etc and we wanted to rename them to replace foo with bar, then we would do the following:

  • use C-x d to enter dired and choose the directory with the files in
  • use C-x C-q to turn dired into editing mode. You can then edit the file names by hand or
  • use M-% to replace foo with bar in the dired buffer. This will change the file names
  • use C-c C-c so apply the changes, or C-c ESC to cancel

The animation below shows this example. Of course you can also manually change the name of the file, as I do for the last file on the list.

dired-rename.gif

  • offby1

    You can also edit the _target_ of a symlink, which makes me smile every time I do it.

    • Ben Maughan

      You have anticipated my next post on dired! I agree – it feels a bit like magic!

  • Kaushal Modi

    Just to clarify, are you using something like anzu-query-replace (anzu package) or vr/query-replace (visual-regexp package) in the gif above?

    • Ben Maughan

      I am using anzu-query-replace which is installed with prelude

  • verdammelt

    You can also rename by regexp by using ‘%r’ in dired.