3

Q: how do I use dired to copy/move files without overwriting?

I'd like to use dired to copy (or move) files from directory A to directory B. However, I only want to copy files in A that do not already exist in B.

Example:

  • Directory A contains file1.txt, file2.txt, and file3.txt.
  • Directory B contains file1.txt.

How do I tell dired to copy only file2.txt and file3.txt from directory A to directory B? I can tell which ones to mark with just a few files, but I'm thinking about situations in which there are potentially thousands of files in the two directories.

1 Answer 1

3

Dired should prompt the user before overwriting any files by default.

Try this:

  1. Create directories A and B and populate them as per your example.
  2. Mark all files in directory A using dired-mark-subdir-files (default binding * s).
  3. Copy them to directory B using dired-do-copy (default binding C).
  4. Dired should prompt you with something like Overwrite 'B/file1.txt'? [Type yn!q or C-h].
  5. If you type C-h, Dired will explain that you can either overwrite/skip this and/or all following conflicts using the different options. In your case, ESC or q will only copy non-conflicting files.
  6. Dired should report something like Copy failed for 1 of 3 files--type ? for details ((file1.txt)).
1
  • You may also be interested in the documentation/implementation of dired-create-files, which is where the magic happens. At its core, this function effectively wraps the core functions copy-file and rename-file, which see.
    – Basil
    May 9, 2017 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.