Need script that will copy the ownerships of files in a backup directory (rsync) and apply them to the original directory, the ownership of which has been mistakenly changed.

Posted on 2009-03-31
Last Modified: 2013-12-16
I need a script that will go through the backup directory and copy the ownership of each file and make a script that will restore ownership of those files. The script will have to begin in /home and then drill down, attaching the relative directories and putting them on the front of the filename. Something like the recursive 'ls -n' that goes through the backup directory would be great. The results would look like:
chown /home/users/a/user/MyTrip.doc
For each file as I go down.
Question by:shinmaikeru
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 68

Accepted Solution

woolmilkporc earned 250 total points
ID: 24027056
Try this -

echo "#!/bin/ksh" >
find /home -type f -ls | awk '{print "chown", $5"."$6, $11}' >>
echo "exit" >>
chmod +x

Assisted Solution

jdevera earned 250 total points
ID: 24027099
I had to do something like this a while ago, I also needed to store the permissions.
The first command line will create a script that you can later run and will restore both permissions and ownership. If you only want ownership, use the second option.
# This will generate a permission and ownership restoration script.
# It can be run like this later:
# $ sh
find /home | xargs -iXXX stat -c "chown %U.%G %n; chmod %a %n" XXX >
# Alternatively, this will only generate ownership restoration.
# It can be run like this later:
# $ sh
find /home | xargs -iXXX stat -c "chown %U.%G %n" XXX >

Open in new window


Author Comment

ID: 24027284
I was trying the method with the find command on my own, but another complication is the fact that this file server contains lots of Japanese filenames and directory names. Xargs can't parse them and the xargs and find methods don't work on them.

I have given the users access again, however, by changing the user directories to the user and group with -R, so I am just working with the alphabetic top directories. Unfortunately, this wipes away any fine-tuned group memberships under the main directories.
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.


Expert Comment

ID: 24027428
I don't know if this will solve the problem with file names, but you can try this alternative:
find /home | while read filename; do stat -c "chown %U.%G %n" $filename; done >

Open in new window


Author Comment

ID: 24027594
It still chokes on multibyte. I have the users back on line, but I have no idea if the server will run after the next reboot.

I need to migrate to a new file server soon, so I will just have to hope that this will do until then.

Author Closing Comment

ID: 31564739
Thank you very much. EE is the best budget money I ever spent.

Author Comment

ID: 24033794
The server is still up, but overnight the Logwatch and rsync backups didn't work, so there must be some dot files which, invisible, were not changed back. How would I get 'stat' or 'find' to gather a list of the dot files that had the owner changed? I can do it with 'ls -lR | grep', but it would be nice to be able to filter out a list that includes the entire path. If 'ls' just had an option to do full path, I would be golden, because I am not that familiar with stat.

Expert Comment

ID: 24037098
There must be a different cause because find, by default, will list everything, including dot files.

I think you should avoid using ls for this kind of scripting tasks. Find is much more powerful and provides a nice list that can be piped to other scripts.

Doing "find . | grep yourfilter" will let you filter out a list of full paths.

About stat, the power in stat is that you can, as shown in my snippets above, specify the format that you want to use for its output, so it lets you write scripts or just summarize the information you want quickly.

So you could do something like

stat -c "%A %h %U %G %s %y %n" filename

And that would mimic (approximately) the output of ls -lR, only for that file. To get this recursively for a directory, you could call stat from within find:

find /home -exec stat -c "%A %h %U %G %s %y %n" {} \;

The -c switch for stat is always followed by a format string that will determine what information is shown, you can find a list of the available placeholders if you have a quick look at the stat manpage (just run: man stat), or here:

I hope this helps.

Author Comment

ID: 24037927
Thank you. That is exactly the information I need. I will look into this immediately tomorrow. My rsync's still aren't working, but I was not able to hit the servers today.

Thank you again. I think this will really help.

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question