14

OS: Debian. I want to be able to move files and folders around as root and from the command line without changing it's owner and group. Is this possible?

Rombus
  • 341
  • 1
  • 7
  • 19
  • Offtopic for Stackoverflow. You probably want to ask on [SuperUser](http://superuser.com/) or [Unix & Linux](http://unix.stackexchange.com/) –  Aug 20 '13 at 20:41
  • where did you experience that moving files as root changes permissions? it shouldn't. – mata Aug 20 '13 at 20:45
  • 2
    @mata I was using mv command to move a file from x user and group. When I moved the file as root it was now owned by root and root group – Rombus Aug 20 '13 at 20:49
  • 3
    This question appears to be off-topic because it is about Linux and belongs on http://unix.stackexchange.com/ – Steve Robbins Aug 21 '13 at 01:05
  • 1
    @mata it seems permissions are changed if the file is being moved between filesystems – zaTricky Mar 13 '21 at 15:55

2 Answers2

15

rsync :

 -A, --acls                  preserve ACLs (implies --perms)
 -X, --xattrs                preserve extended attributes
 -o, --owner                 preserve owner (super-user only)
 -g, --group                 preserve group
     --devices               preserve device files (super-user only)
     --specials              preserve special files

man rsync

Radu Toader
  • 1,521
  • 16
  • 23
  • 1
    Thanks a lot Radu, I never thought of rsync for local use. It worked. I added the --remove-source-files flag so that it acts as the `mv` command. So it'll be like thís: `#rsync -o -g --remove-source-files source dest` – Rombus Aug 20 '13 at 21:05
  • Probably worth noting that `--remove-source-files` removes the files continuously as the move progresses. That's different from `mv` which only does so after the entire operation is complete. – smheidrich Jul 30 '22 at 18:25
9

The mv command preserves ownership and time-stamps. For the cp command there are various command line options you can use. For example

cp -p file1 file2

man cp will display all the available options

dseiple
  • 598
  • 2
  • 6
  • 17