How to move a file without preserving permissions

mv is the wrong tool for this job; you want cp and then rm. Since you're moving the file to another filesystem this is exactly what mv is doing behind the scenes anyway, except that mv is also trying to preserve file permission bits and owner/group information. This is because mv would preserve that information if it were moving a file within the same filesystem and mv tries to behave the same way in both situations. Since you don't care about the preservation of file permission bits and owner/group information, don't use that tool. Use cp --no-preserve=mode and rm instead.


When you move a file within the same filesystem, mv detaches the file from its old location and attaches it to its new location; metadata such as permissions remains the same. When you move a file to a different filesystem, mv copies the file, attempts to replicate as much metadata as possible, and removes the original.

Since you're moving to a different filesystem and you don't want to replicate much metadata, you might as well copy the file then remove the original.

cp "$backupfile" "$destination" && rm "$backupfile"

This preserves the file's permissions to some extent (e.g. world-readability, executability). The file's modification time isn't preserved. With GNU cp, you can use the --preserve=… option to contol what metadata is replicated more finely, e.g. --preserve=mode,timestamps.

You can also use rsync and tell it what you want to preserve. The option -a means “preserve most metadata”, which includes the owner if running as root only.

rsync -a --no-owner --no-group --remove-source-files "$backupfile" "$destination"

Tags:

Bash

Mv