How can I safely merge or resume copying folders with overwrite prompts?

0
0
Asked By MellowPine42 On

I'm looking for a script or command-line workflow that behaves like a typical graphical file manager when copying or moving one directory into another directory with the same name. Ideally, it would merge the directory contents recursively instead of aborting. When a file with the same name already exists, it should show useful details such as which version is newer or larger and ask whether to overwrite it.

This would be especially useful for resuming a copy or move operation that was interrupted. I'd also prefer move behavior where each source file is removed immediately after it has been successfully transferred, so the source reflects the current progress.

Would it make sense to process files individually, compare the source and destination with stat information before each transfer, and then invoke rsync or another tool? Is there a more efficient approach, or can existing tools provide most of this behavior, including a preview of conflicts?

3 Answers

Answered By NimbleWillow6 On

If you build your own interactive tool, comparing both paths with `stat` can provide size, modification time, permissions, ownership, and file type. You may also want to detect symlinks and other non-regular files, and optionally compare checksums when timestamps and sizes aren’t enough. After the user chooses an action, transfer one item at a time and remove the source only after that item succeeds. Be careful with interrupted transfers, metadata preservation, symlinks, and failures during deletion.

Answered By BrightCobalt31 On

Rsync is probably the better foundation. Start with a dry run, such as `rsync --dry-run /path/to/source/ /path/to/destination/`, to see which files would be added or updated before changing anything. It handles recursive directory merging and skips files that already appear up to date, which is much more efficient than manually invoking a transfer for every file.

SilverKite58 -

A dry run is especially helpful for nested directories because it gives you a list of planned changes and potential conflicts before committing to a long operation.

Answered By QuartzHarbor7 On

For basic confirmation, `cp -i` and `mv -i` can ask before replacing files. You can also define shell aliases if you use this behavior often. However, they don’t provide detailed size or timestamp comparisons, and when directories are copied recursively they generally merge existing directories without asking about the directory itself—only conflicting files trigger prompts.

CedarOrbit19 -

So those options are useful for simple file-level protection, but they don’t fully reproduce the detailed, recursive behavior of a graphical file manager.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.