nixCraft Linux Forum

nixCraft

Linux 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

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 01-23-2007, 12:47 AM
Junior Member
User
 
Join Date: Feb 2005
Posts: 23
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 (user 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
Sponsored Links
  #2 (permalink)  
Old 01-23-2007, 01:03 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Any distro with shell
Posts: 973
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 | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 01-23-2007, 02:12 AM
Junior Member
User
 
Join Date: Feb 2005
Posts: 23
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 01-23-2007, 03:13 AM
Junior Member
User
 
Join Date: Feb 2005
Posts: 23
Rep Power: 0
marinm
Default

I belive the script picks up the time instead of the username...help?
Reply With Quote
  #5 (permalink)  
Old 01-23-2007, 06:56 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Any distro with shell
Posts: 973
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 | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #6 (permalink)  
Old 01-23-2007, 12:53 PM
Junior Member
User
 
Join Date: Feb 2005
Posts: 23
Rep Power: 0
marinm
Default

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

Bookmarks


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 On

Similar Threads

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


All times are GMT +5.5. The time now is 04:36 AM.


Powered by vBulletin® Version 3.7.3 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

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