Copying files and directories

rsync

Tested. Recommended.

Use cygwin in Windows to get this working.

cp command

Simpler. Less feature-rich than rsync.

Robocopy

Which method to choose?

  1. Use rsync if:
    1. You are copying large amounts of data.
    2. You are copying over a network.
    3. You need fine-grained control or robustness.
    4. You might later want to update existing files (just remove --ignore-existing).
    5. You want reliable handling of many files, hidden files, special files, etc.
  2. Use cp -n if:
    1. It’s a simple, one-off local copy.
    2. You prefer using basic standard tools.
    3. You are sure you don’t have too many files for the * wildcard.

In most cases, rsync -av --ignore-existing is the better, more robust solution.

Trouble copying files with large names

This problem is especially painful in Windows OS. When moving large data from one folder to another in a Windows comptuer, the usual copying options did not work well. cygwin, rsync, cp, robocopy, none of them worked when it comes to moving files with large names. What is worse is, they would move some of the files but not all of the files. If I hadn’t double-checked the differences using the diff tool, I wouldn’t have noticed that some of the files were not moved. That is a crappy user experience.

Windows may not copy files with long filenames due to a MAX_PATH limit of 260 characters for the full file path. This includes the drive letter, folder structure, and the filename itself.

How do we resolve this?

To resolve this, you can either shorten the file path, enable long path support in Windows, or use third-party file managers that handle longer paths.

  1. Shorten the Path:
    1. The most direct solution is to shorten the file path by reducing the number of characters in the folder structure or the filename itself.
  2. Enable Long Path Support:
    1. Windows 10 and later versions allow you to enable long paths. This involves modifying a registry setting:
    2. Open Registry Editor (search for regedit).
    3. Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem.
    4. Create a new DWORD (32-bit) Value named LongPathsEnabled.
    5. Set its value to 1.
    6. You may also need to enable the corresponding Group Policy setting: Local Computer Policy > Computer Configuration > Administrative Templates > System > Filesystem > Enable NTFS long paths.
  3. Use a Third-Party File Manager:
    1. Several file managers, such as Directory Opus, FreeCommander, or OneCommander, can handle files with paths exceeding the 260-character limit.
  4. Use Robocopy:
    1. Robocopy, a command-line utility, is often more reliable at copying files with long paths than File Explorer.
  5. Use zip files
    1. This is the only solution that worked reliably.

      Zip the directories in the source, move the zip file and then unzip it in the destination.


Links to this note