LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-25-2007, 04:24 AM   #1
danielsbrewer
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Rep: Reputation: 0
syncing ownership and groups


Hello,

Recently, I was a complete doofus and changed all the ownership and group of all files in the home directory to a particular user and group. Luckily however I have a backup, but it is slightly old so there probably is some changes. So what I would like to do is to be able to "sync" the ownership and groups. Does anyone know of the best way to do this?

My current thinking is:
1) Use find to get all files in backup
2) Find the user and group
3) Apply this to the actual file

Not sure on the implementation though.

Thanks

Dan
 
Old 09-25-2007, 05:04 AM   #2
danielsbrewer
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Original Poster
Rep: Reputation: 0
This is what I have so far:

Quote:
#!/bin/bash

BACKUP="/restore060907/home"
REAL="/home"

cd $BACKUP

for i in `find .`
do
echo $i

USER=`stat -c %U "$i"`
GROUP=`stat -c %G "$i"`

oldUSER=`stat -c %U $REAL/"$i"`
oldGROUP=`stat -c %G $REAL/"$i"`

if [ "$USER" != "$oldUSER" -a "$GROUP" != "$oldGROUP" ]; then
ls -l $REAL/"$i"
echo "$USER $GROUP"
chown $USER.$GROUP $REAL/"$i"
ls -l $REAL/"$i"
fi
done
This seems to work, but it doesn't with filenames that have spaces in it. I suppose the source of the problem is the find command.
 
Old 09-25-2007, 05:30 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I don't know of a tool which does this automatically, but given some time for the problem, here's how I would approach it.
  1. Set sane defaults for permissions of the existing files. This probably means:
    Code:
    chown -R user:group /home/user
    find /home/user -type d -print0 |xargs -0 chmod 755
    find /home/user -type f -print0 |xargs -0 chmod 644
    chmod 755 /home/user/bin/*
    chmod 700 /home/user/private /home/user/.gnupg /home/user/.ssh
  2. Extract the backup to a different location.
  3. Write a little Perl program (or similar) to check each file and directory in /home/user (recursively), and if the file exists in the restored backup, set the permissions and ownership accordingly.
 
Old 09-25-2007, 05:47 AM   #4
danielsbrewer
LQ Newbie
 
Registered: Aug 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Fixed it.

Used
Code:
find . | while read i
instead of
Code:
for i in `find .`
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
groups (system default groups) Xavius Linux - General 2 07-18-2012 02:50 AM
Map Windows NT Groups to UNIX Groups - why? kenji1903 Linux - Networking 4 10-16-2007 11:52 AM
limit to nesting groups within groups? geekgrl Linux - General 3 10-16-2007 11:50 AM
winbind: wbinfo -g only lists global groups from PDC and not local groups saradiya Linux - Networking 0 12-01-2003 02:58 AM
Groups members of other groups bentz Linux - Security 6 06-22-2003 11:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration