If you're encountering permission issues when trying to move a directory and its subdirectories to another location, rsync is indeed a good tool to handle this, especially if you need to preserve permissions, ownership, and other attributes. Here's how you can do it:
Using rsync to Move a Directory with Permissions
The -a (archive) flag in rsync preserves permissions, ownership, timestamps, and other attributes. The --remove-source-files option can help clean up the source after copying, but since you want to move the entire directory, you might want to delete the source afterward.
Step-by-Step Command:
rsync -av --progress /path/to/source_dir/ /path/to/destination_dir/
-a: Archive mode (preserves permissions, ownership, timestamps, etc.).
-v: Verbose output (shows progress).
--progress: Shows detailed progress during transfer.
If You Want to Delete the Source After Copying:
After verifying the copy was successful, you can delete the source directory:
rm -rf /path/to/source_dir
Alternative: rsync with --remove-source-files (for files, not dirs)
If you're moving files (not directories), you can use:
rsync -av --remove-source-files /path/to/source_dir/ /path/to/destination_dir/
But this won't work well for directories, so the first approach is better.
If You Need to Preserve Hard Links or Special Files
Use -H (preserve hard links) or -A (preserve ACLs) if needed:
rsync -avH --progress /path/to/source_dir/ /path/to/destination_dir/
If You Have Permission Issues Even with rsync
If rsync fails due to permission issues, try running it with sudo:
sudo rsync -av --progress /path/to/source_dir/ /path/to/destination_dir/
Then delete the source with:
sudo rm -rf /path/to/source_dir
Alternative: cp with sudo
If rsync is not available, you can use cp with sudo:
sudo cp -a /path/to/source_dir /path/to/destination_dir/
Then delete the source:
sudo rm -rf /path/to/source_dir
Important Notes:
-
Trailing Slash Matters:
/path/to/source_dir/ (with trailing slash) copies the contents of source_dir into destination_dir.
/path/to/source_dir (without trailing slash) copies the entire source_dir into destination_dir.
-
Verify Before Deleting:
Always check that the destination has the correct files before deleting the source.
-
Use --dry-run First:
To test without actually copying:
rsync -av --dry-run /path/to/source_dir/ /path/to/destination_dir/
Would you like help with a specific permission issue you're encountering?