Bash script not preserving file permissions when using cp within a loop
I'm working on a personal project and I've encountered a strange issue with I've been banging my head against this for hours... I'm relatively new to this, so bear with me... I have a bash script that processes files in a directory and copies them to a backup location. The scenario I'm working with is that while the files are copied successfully, their original permissions are not being preserved. I'm using the `cp` command within a for loop to iterate through the files. Hereβs a simplified version of my script: ```bash #!/bin/bash SOURCE_DIR="/path/to/source" BACKUP_DIR="/path/to/backup" for file in "$SOURCE_DIR"/*; do cp "$file" "$BACKUP_DIR/" done ``` I've tried adding the `-p` flag to the `cp` command to preserve the permissions, but it seems like it's not working as expected. When I run the following command to copy a file manually: ```bash cp -p /path/to/source/example.txt /path/to/backup/ ``` The permissions are preserved correctly, but when I run the script, all the copied files in the backup directory end up with default permissions. I ensured that the script is executed with the same user that owns the files. Additionally, I have verified that the `umask` value is not affecting the permissions, as it is set to `0022`. I would appreciate any insights on why the permissions are not being preserved when copying files within the loop in the script. I am currently on Ubuntu 20.04, and using Bash version 5.0.17. Any suggestions or alternative methods to ensure that the permissions are maintained would be greatly appreciated. Am I missing something obvious? Has anyone else encountered this? I'm developing on macOS with Bash. I'm using Bash 3.10 in this project. What's the correct way to implement this?