70

The problem is I want to be able to see errors when moving a file, but not see errors with permissions problem. In other words - I care if the file is not fully transmitted, but don't want to see errors like this:

mv: failed to preserve ownership for `/home/blah/backup/pgsql.tar.gz': Operation not permitted

So I want something like: mv $backupfile $destination --ignore-permissions.

The backup file can be anything from 1 MiB to 5 GiB and is transfered through NFS.

3
  • It happens also on connected NTFS device, but this is not the only case. It happens in all cases were you do not have rights to change permissions or it is not possible (like on NTFS).
    – Nux
    Commented Nov 24, 2014 at 10:23
  • This is useful when trying to move files that somehow don't get moved because of permissions errors (e.g. because they are mounted over cifs). Commented Jan 7, 2017 at 21:11
  • For me it also happens when moving a file into an sshfs mount, even if it's mounted with -o idmap=user, because the gid does not get mapped (even with -o gidfile).
    – Curt
    Commented Dec 30, 2020 at 8:02

2 Answers 2

98

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.

1
  • 13
    Thanks, it works. Ended up doing: cp --no-preserve=mode,ownership $backupfile $destination, check exit code and then do rm $backupfile if everything is fine.
    – Nux
    Commented May 21, 2014 at 16:54
32

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"
2
  • The rsync solution works excellently. I'd been struggling trying to transfer my old ubuntu install off CIFS share. My rsync didn't report a failure to move the file, but wasn't moving it and was only reporting permissions problems. Commented Jan 7, 2017 at 21:12
  • @SridharSarnobat that sounds like it's working sub-excellently? Commented Aug 29, 2022 at 3:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .