CodexBloom - Programming Q&A Platform

Bash script not preserving file permissions when copying files using rsync

πŸ‘€ Views: 96 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-19
bash rsync permissions

Hey everyone, I'm running into an issue that's driving me crazy... I'm performance testing and I'm working with an scenario where my Bash script that uses `rsync` to copy files from one directory to another is not preserving the original file permissions as expected. I am using `rsync` version 3.2.3 on Ubuntu 20.04. My command looks something like this: ```bash rsync -av /source/directory/ /destination/directory/ ``` I have the `-p` flag included in the `-a` option, which should preserve permissions, but the permissions of the copied files in the destination directory do not match those in the source directory. I've also tried running the script with `sudo`, but that didn't change the outcome. Additionally, I checked the ownership of the destination directory and ensured that the user running the script has the necessary permissions to write to that directory. When I run the command manually in the terminal, it preserves the permissions correctly, but when it's executed from the script, it does not work as intended. I added debugging lines to my script to see what’s happening: ```bash echo 'Copying files...' rsync -av /source/directory/ /destination/directory/ echo 'Files copied!' ``` This outputs properly, but the permissions scenario continues. Could this be related to how the script is executed? Is there a known scenario or best practice regarding permission preservation when copying files in a script? Any insights or suggestions would be greatly appreciated! I'm using Bash latest in this project. What would be the recommended way to handle this? I'd love to hear your thoughts on this.