1

I have a /data folder that has a gazillion files and folders, took all night to copy.

I copied it as root via cp -r /data/* /data2 forgetting the p

All my stuff at the destination is owned by root.

I have not deleted the source /data but I do not want to recopy if I do not have to.

Is there a way to copy just the permissions, or sync the permissions, between source and destination?

ron
  • 6,575
  • You might find this answer helpful. find could be slow in traversing the directory tree but I did come up with an rsync answer. – doneal24 Sep 21 '22 at 17:42
  • You can change ownership & permissions of data partitions, but do not run on any system partitions or you totally break system. sudo chown -R $USER:$USER /mnt/data Note -R is recursion so all underlying folders also changed. sudo chmod -R a+rwX /mnt/data https://askubuntu.com/questions/1013677/storing-data-on-second-hdd-mounting – oldfred Sep 21 '22 at 20:27
  • @oldfred This will reset the ownership the files but the OP was also asking about permissions. You need to incorporate a chmod somewhere in here. – doneal24 Sep 21 '22 at 20:29
  • I updated the title, I meant to say ownership regarding just uid and gid, I didn't have a problem with the -rw-r--r-- permission part. – ron Sep 21 '22 at 20:38
  • but ultimately i want the owner.group, and permissions, on the destination to match what the source was; my observation was the permissions were the same everything got turned into root.root ownership which was my problem – ron Sep 21 '22 at 20:39
  • Is $USER not your user & group? echo $USER Of if you have multiple users, it gets more complicated. – oldfred Sep 21 '22 at 21:13

2 Answers2

2

mtree can do this, and offers the advantage that it doesn't need to read the file contents at all. It compares only the file metadata, and modifies the destination to match the source:

$ sudo mtree -cp /data | sudo mtree -Utp /data2

-c tells the LHS mtree to create a heirarchy spec, rooted in the -p path of /data, and the RHS mtree invocation updates the owner/group/perms -U and modification times -t of the heirarchy rooted in -p /data2.

Jim L.
  • 7,997
  • 1
  • 13
  • 27
1

I would use rsync:

rsync -avic --progress /data/ /data2

Note:
The trailing / on /data/ is deliberate, it ensures rsync will not create /data2/data and re-copy all your files under it. (you don't need a trailing / on /data2)

The options are:
-a which is named for "archive" but really turns on a bunch of useful options (like recursive descent into subdirs) that will imitate your cp -r command's behavior.
-v and -i make the copy verbose and show how rsync sets the permissions and ownership on the copied files, which is useful as the copy goes forward.
-c uses checksums to decide whether the data in each file should be copied. This will let rsync skip copying the file contents and only adjust the ownership/permissions on the destination files.
--progress is not required, but shows the progress copying any file contents to the destination.

The -v and -i options will make at least one line scroll in your terminal window per file. Omit them if you don't want this. You can add -n (i.e., -avicn) if you want rsync to do a verbose dry run that tells you what it would do, but doesn't make changes to the files. So a dry run with the -avicn --progress options, followed by the real thing with just the -ac --progress options may be more desirable for you.

Sotto Voce
  • 4,131
  • This is a better answer than the find I suggested in my comment. – doneal24 Sep 21 '22 at 20:30
  • The -c flag will recalculate checksums for every single file. At that point you might as well just recopy the files - it would be faster for sure – Chris Davies Sep 21 '22 at 20:58
  • @roaima that depends. re-copying the files involves reading and writing all the data bytes. rsync -c involves 2x the reads, but writes can be heaver and slower than reads. – Sotto Voce Sep 21 '22 at 21:07