Okay, rsync is an excellent tool for this scenario, especially when dealing with potential permission issues. It can copy files efficiently and has options to handle permissions, ownership, and timestamps, and critically, it can delete the source files after a successful transfer, effectively acting like a move command.
Here's how you can do it:
The Command:
The most common and recommended way uses the -a (archive) flag, which implies recursive copying and preserving permissions, timestamps, owner, group, symlinks, etc.
sudo: This is often crucial. If the permission issues mean the current user doesn't have permission to read certain files/directories within source_dir, or write to destination_dir, sudo elevates privileges to run the command as the superuser (root). If you know you have sufficient permissions already, you might omit sudo, but with permission issues, it's usually necessary.
rsync: The command itself.
-a (archive): This is a shortcut for several options (-rlptgoD). It means:
-r: Recursive (copy directories and their contents).
-l: Copy symlinks as symlinks.
-p: Preserve permissions.
-t: Preserve modification times.
-g: Preserve group.
-o: Preserve owner (requires sudo).
-D: Preserve device files and special files (requires sudo).
Note:-o and -D generally require root privileges.
-v (verbose): Shows you which files are being transferred. Helpful for tracking progress and seeing errors.
--progress: Gives you a detailed progress status for each file being transferred. It can be helpful for large directories. You can omit this if you prefer less output.
--remove-source-files: This is the key option that makes rsync act like a mv command. After a file is successfully transferred to the destination, it's deleted from the source. This is safer than manually deleting the source later, as it only deletes files that rsync confirmed were copied.
/path/to/source_dir: The source directory you want to move.
/path/to/destination_dir: The destination directory.
Important Syntax Note:
If /path/to/destination_dirdoes not exist, rsync will create it and copy source_dirinto it, resulting in /path/to/destination_dir/source_dir.
If /path/to/destination_diralready exists, rsync will copy source_dirinside the existing /path/to/destination_dir, resulting in /path/to/destination_dir/source_dir. Adding a trailing slash (/) to the destination (destination_dir/) makes this behaviour explicit.
Before Running (Highly Recommended): Dry Run
Because --remove-source-files is destructive, always perform a dry run first using the -n option. This will show you exactly what rsyncwould do without actually transferring or deleting anything.
Review the output carefully. Check that the files and directories listed are the ones you expect to be moved.
What if sudo isn't available or desired?
If you cannot use sudo or don't want to preserve ownership/special files, you can use a slightly different set of flags. -a includes -o and -g, which might fail or have unintended consequences without root. A good alternative is:
This version skips preserving owner (-o) and device/special files (-D), which are the most likely options to require root. It will still preserve permissions as much as possible for the user running the command.
Summary Steps:
Backup: If the data is critical, back it up first.
Dry Run: Run sudo rsync -avn --progress /path/to/source_dir /path/to/destination_dir (or the non-sudo version). Review the output.
Execute: If the dry run looks correct, run the actual command: sudo rsync -av --progress --remove-source-files /path/to/source_dir /path/to/destination_dir (or the non-sudo version).
Verify: Check the destination directory to ensure everything arrived correctly. The source directory and its contents should now be gone (because of --remove-source-files).
rsync is robust and handles interruptions better than mv or cp in many cases, making it ideal for potentially complex moves like this.