Is there a way to move a file and preserve original owner(group) and file mode in one atomic operation? So there will not be change for others to access moved file in time when is not already set original owner or mode?

Filesystem is XFS.

  • 4
    Moving within the same filesystem or across filesystems? One is inherently atomic; the second isn't.
    – roaima
    Jun 4, 2015 at 9:17
  • Please note that i need atomicity, file must exists with original mode and owner or must not exists.
    – isevcik
    Jun 4, 2015 at 13:49
  • That's fine. Are you moving within the same filesystem or across filesystems? The first is inherently atomic; the second isn't.
    – roaima
    Jun 4, 2015 at 14:11
  • Same filesystem, but how can be inherent, when mv command change owner to executing user?
    – isevcik
    Jun 4, 2015 at 22:22
  • 1
    In this situation mv doesn't change the owner.
    – roaima
    Jun 4, 2015 at 23:20

3 Answers 3


The requirement now stands as a mv operation on a file, or tree of files, within the same filesystem.

This satisfies the atomicity and ownership/group requirements. It will also retain setgid and sticky-bit flags if the operation is performed as the root user. For normal files and directories any user can move then provided they have read+write+execute permission to the source and destination parent directories (i.e. old and new in this example):

mv /path/to/old/source /path/to/new/destination

See POSIX mv and POSIX rename for the precise detail.


If you're moving the file withing the same filesystem, then mv is an atomic operation. Up to a point, the file is at its old location; after that point the file is at its new location. The entry for the file in the old directory is removed, and a new one is added to the new directory, in a single operation. The entry is not just the file content but the whole inode, including the file metadata such as the owner and permissions. There is no point at which the permissions are modified.

If you're moving the file to a different filesystem, then the operation is not atomic: it involves creating a new file, then modifying both its contents and its metadata until they match the old file, and removing the old file. A new file is always created empty and belonging to the user who created it. The creator can set the traditional Unix permissions as soon as the file is created, but not access control lists.

You can control the initial owner, group and mode of the new file by running mv as the desired owner and group and setting umask to the complement of the permissions of the file. Here's a Linux shell method, assuming the user and group exist and have a Bourne-style shell as their login shell:

set $(stat -c '%U %G %a' "$original_file")
export user=$1 group=$2 mode=0$3 original_file destination
su "$user" -c 'sg "$group" -c "umask $((07777 & ~mode)) && touch "$destination" && mv -- "$original_file" "$destination"'

This is pretty fiddly, and the file will be created with the right ownership and permissions but different contents, different timestamps, etc. If you don't want the file to be visible until it's fully reproduced, first move the file to a temporary directory on the destination filesystem which is visible only to root, then atomically move the file into place.

d=$(TMPDIR="$(dirname -- "${destination}")" mktemp -d)
chmod 700 "$d"
mv -- "$original" "$d/file"
mv -- "$d/file" "$destination"
rmdir d

These are some ways to do it; although not atomic, should be safe enough for your needs.

  • You could copy the file using the -p flag to preserve permissions and ownership, then remove the original file: cp -p /old/path/file /new/path/ && rm -f /old/path/file

  • You could use rsync: rsync -pogXA --remove-source-files /old/path/file /new/path/

  • If you're the root user, you could chmod 000 the file, move it, then restore the appropriate owner and permissions.

  • 1
    Those methods still have windows where the file does not have its original owner or permissions.
    – JdeBP
    Jun 4, 2015 at 12:11
  • 1
    Only the third method, and it's not a problem as the file is not accessible while it has 000 permissions.
    – dr_
    Jun 4, 2015 at 12:14
  • 1
    The other ones do as well. Think about what the programs actually do, and indeed read their man pages for the second one. As for it not being a problem: read the requirement for atomicity in the very question that you are answering.
    – JdeBP
    Jun 4, 2015 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.