Skip to content

Instantly share code, notes, and snippets.

@pnsinha
Last active February 21, 2024 21:54
Show Gist options
  • Save pnsinha/34d79f94f79d56f164d271cdebccf0ef to your computer and use it in GitHub Desktop.
Save pnsinha/34d79f94f79d56f164d271cdebccf0ef to your computer and use it in GitHub Desktop.
rsync Permissions
#https://unix.stackexchange.com/questions/43957/using-rsync-to-move-not-copy-files-between-directories
#!/usr/bin/bash
echo -e "Would you like to use a relative path to your source?[y/n?]\n"
read ans
if [[ $ans == y ]]; then
echo -e "Relative source?\n"
read source
source="`pwd`/$source"
elif [[ $ans == n ]]; then
echo -e "Absolute path to your source?\n"
read source
source=${source/"~/"/"/home/jerzy/"}
else
echo -e "Use small cap 'y' or 'n' only."
exit
fi
echo -e "Would you like to use a relative path to your destination?[y/n?]\n"
read ans2
if [[ $ans2 == y ]]; then
echo -e "Relative destination?\n"
read dest
dest="`pwd`/$dest"
dest=${dest/"~/"/"/home/jerzy/"}
elif [[ $ans2 == n ]]; then
echo -e "Absolute path to your destination?\n"
read dest
dest=${dest/"~/"/"/home/jerzy/"}
else
echo -e "Use small cap 'y' or 'n' only."
exit
fi
rsync -avh --remove-source-files --info=progress2 ${source} ${dest} && find ${source} -type d -empty -delete

Permissions and Rsync

The rsync command is a powerful tool for synchronizing files and directories between two locations. It is commonly used in backup and mirroring operations.

When using rsync, the -a or --archive option is often used to preserve the permissions, ownership, and timestamps of the files being transferred(https://www.zyxware.com/articles/3660/how-to-preserve-permissions-ownership-timestamp-archive-mode-in-rsync-using-rsync-a)[[4]]. This means that rsync will attempt to maintain the same permissions and ownership in the destination as in the source.

However, it's important to note that the user running the rsync command needs to have the necessary permissions to read the source files and write to the destination directory. If the user doesn't have these permissions, the rsync command may fail.

Moving or Archiving a Folder

The rsync command can be used to move files from one location to another by using the --remove-source-files option. This option tells rsync to remove the source files after they have been successfully transferred to the destination.

Here's an example of how to use this option:

rsync -av --remove-source-files source_directory/ destination_directory/

In this command, -a stands for "archive", which preserves permissions, ownership, and timestamps, and -v stands for "verbose", which provides detailed output of the operation. The --remove-source-files option tells rsync to remove the source files after they have been successfully transferred.

Ownership and Permissions

While the -a option preserves the permissions and ownership of the files, the ownership of the transferred files may change to the invoking user on the receiving end if you don't use certain options. If you want to specify some other user, you will need to add a chown command to your script.

Moreover, if the source has nonsensical permissions, rsync may fail because it creates the destination directories with the same permissions, then can't create any files in them. In such cases, you can use the --chmod=ugo=rwX option to ensure that all non-masked bits get enabled, giving new files the destination-default permissions while leaving existing files unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment