nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Shell script to change folder directory owner after restore

This is a discussion on Shell script to change folder directory owner after restore within the Shell scripting forums, part of the Development/Scripting category; Hello, Recently I had to backup my data because the hard drives were failing. When I restored the accounts, all ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 23-01-2007, 12:47 AM
Junior Member
User
 
Join Date: Feb 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
marinm
Default Shell script to change folder directory owner after restore

Hello,

Recently I had to backup my data because the hard drives were failing. When I restored the accounts, all folders (users) in /home had the same owner (a3u3). Here's an example:

Quote:
root@voyager [/home]# ls -al |more
total 12845244
drwxr-xr-x 1099 root root 20480 Jan 21 16:42 ./
drwxr-xr-x 23 root root 4096 Jan 21 16:42 ../
drwx--x--x 7 a3u3 a3u3 4096 Jan 19 08:23 a3u3/
drwx--x--x 7 a3u3 a3u3 4096 Jan 19 08:23 a789crew/
drwx--x--x 7 a3u3 a3u3 4096 Jan 19 08:23 ace88/
drwx--x--x 7 a3u3 a3u3 4096 Jan 19 08:23 adamnic/
drwx--x--x 7 a3u3 a3u3 4096 Jan 19 08:24 adamsand/
drwx--x--x 6 a3u3 a3u3 4096 Jan 19 08:34 adi/
drwx--x--x 7 a3u3 a3u3 4096 Jan 19 08:24 adriana/
drwx--x--x 7 a3u3 a3u3 4096 Jan 19 08:34 adultph/
drwx--x--x 7 a3u3 a3u3 4096 Jan 19 08:24 aeortic/
drwx--x--x 7 a3u3 a3u3 4096 Jan 19 08:24 afclan/
drwx--x--x 9 a3u3 a3u3 4096 Jan 19 08:24 afzal/
....and so on.

Can anyone help me a script which will change the ownership of each directory? This is how it's supposed to look like:

Quote:
drwx--x--x 7 a3u3 a3u3 4096 Jan 19 08:23 a3u3/
drwx--x--x 7 a789crew a789crew 4096 Jan 19 08:23 a789crew/
drwx--x--x 7 ace88 ace88 4096 Jan 19 08:23 ace88/
drwx--x--x 7 adamnic adamnic 4096 Jan 19 08:23 adamnic/
drwx--x--x 7 adamsand adamsand 4096 Jan 19 08:24 adamsand/
drwx--x--x 6 adi adi 4096 Jan 19 08:34 adi/
drwx--x--x 7 adriana adriana 4096 Jan 19 08:24 adriana/
...and so on.

I guess this should be easy to make for someone who knows scripting....Can anyone help me please? I have over 1000 accounts and doing this manually will take ages.

The script should execute chown -R user.group directory...

Thanks,
Marin
Reply With Quote
  #2 (permalink)  
Old 23-01-2007, 01:03 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Try
Code:
#!/bin/bash
HOMED="/home"
DIRS=$(ls -l $HOMED | grep ^d | awk '{ print $8}')
for d in $DIRS
do
  echo "chown -R $d.$d $HOMED/$d"
done
Replace when satisfied
Code:
  echo "chown -R $d.$d $HOMED/$d"
With
Code:
  chown -R $d.$d $HOMED/$d
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 23-01-2007, 02:12 AM
Junior Member
User
 
Join Date: Feb 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
marinm
Default

Thanks for the quick reply! This is what I get:

root@voyager [~]# ./home
chown -R 08:23.08:23 /home/08:23
chown -R 08:23.08:23 /home/08:23
chown -R 08:23.08:23 /home/08:23
chown -R 08:23.08:23 /home/08:23
chown -R 08:23.08:23 /home/08:23
chown -R 08:24.08:24 /home/08:24
chown -R 08:34.08:34 /home/08:34
chown -R 08:24.08:24 /home/08:24
chown -R 08:34.08:34 /home/08:34
chown -R 08:24.08:24 /home/08:24
chown -R 08:24.08:24 /home/08:24

Is it normal?
Reply With Quote
  #4 (permalink)  
Old 23-01-2007, 03:13 AM
Junior Member
User
 
Join Date: Feb 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
marinm
Default

I belive the script picks up the time instead of the username...help?
Reply With Quote
  #5 (permalink)  
Old 23-01-2007, 06:56 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Is this a Linux box? Just count output using ls -l and select correct col $8 is for dir. Try $9 and run script again
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #6 (permalink)  
Old 23-01-2007, 12:53 PM
Junior Member
User
 
Join Date: Feb 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
marinm
Default

That worked! $9 worked! Thank you so much!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Shell Delete Directory chimu Slackware 2 31-05-2008 03:36 PM
A script for Removing all the files inside a folder and its sub folder vivekv Shell scripting 1 25-10-2007 01:44 PM
i need a script to delete one folder with files on my ftp silver_ch Shell scripting 1 27-03-2007 10:15 AM
How to change the login shell for Linux user chimu Getting started tutorials 0 26-01-2007 07:20 PM
error shell script no such file or directory /bin/sh Linux software 1 08-01-2006 08:34 PM


All times are GMT +5.5. The time now is 08:54 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38