Skip to content

Seattle Unix Group Web Site

Sections
Personal tools
You are here: Home » Meetings » 1995 » May » Change Owner -- Bash

Change Owner -- Bash

From grieg!bentson Thu May 11 17:07:56 1995
X-Delivered: at request of bill on camco
Return-Path: <grieg!bentson>
Received: from grieg by camco.celestial.com with uucp
(Smail3.1.29.1 #1) id m0s9iGp-0002V6C; Thu, 11 May 95 17:07 PDT
Received: by grieg.seaslug.org (Smail3.1.28.1 #5)
id m0s9gVw-000aEcC; Thu, 11 May 95 15:15 PDT
Message-Id: <m0s9gVw-000aEcC@grieg.seaslug.org>
Date: Thu, 11 May 95 15:15 PDT
From: bentson@grieg.seaslug.org (Randolph Bentson)
To: bill@celestial.com
Subject: my script from Tuesday's meeting
Bcc: bentson@grieg.seaslug.org
Status: RO


Here's what I presented at the SLUG meeting.

Randy


#!/bin/bash

# chownly -- a bash shell script to hunt
# down all files owned by some user and
# change the ownership to some other user.

# A run-time option, "-d", enables a debug
# flag that will just list candidate files.

# The first arguments are the original user
# name and new user name.
# The program will check /etc/passwd to make
# sure the users are known by name.

# The optional final argument is the starting
# directory ( by default / ).

usage(){
if [ $# -ne 0 ]
then
echo $1
fi
echo "chownly [-d] user1 user2 [start_dir]"
echo " -d -> debug "
echo " user1 -> the original owner"
echo " user2 -> the new owner"
echo " start_dir -> where to start"
echo " default is '/'"
exit
}

# return list of all directories in current directory
dirs(){
ls -la|tail +4|awk '/^d/{print $9}'
}

# return list of all files in current
# directory that are owned by the old_user
files(){
ls -la|tail +4|\
awk "{ if ( \$3 == \"$old_user\" )\
print \$9 }"
}

# either change ownership of file or describe
# the intended change (because of debug flag)
chownx(){
if [ $debug -eq 1 ]
then
echo change ownership of $1 to $2
elif [ $debug -eq 0 ]
then
echo chown $2 $1
else
echo error in debug flag
exit
fi
}

# change ownership of everything in this
# directory. Then recursively process
# sub-directories.
doit(){

local old_dir=`pwd`
cd $1
for file in `files` ;
do
chownx $file $new_user
done

for dir in `dirs` ;
do
doit $dir ;
done

cd $old_dir
return
}

# START OF MAIN CODE

PATH=/bin:/usr/bin
export PATH

# Validate and extract arguments
# then start processing at top-most
# directory

if [ $# -lt 1 ]
then
usage 'too few arguments'
fi

if [ $1 = '-d' ]
then
debug=1
shift
else
debug=0
fi

if [ $# -ge 1 ]
then
old_user=$1
shift
else
usage 'no old user name given'
fi

if [ $# -ge 1 ]
then
new_user=$1
shift
else
usage 'no new user name given'
fi

if [ $# -eq 1 ]
then
root=$1
shift
else
root='/'
fi

if [ $# -ne 0 ]
then
usage 'too many arguments given'
fi

grep -q "^$old_user:" /etc/passwd

if [ $? -ne 0 ]
then
usage 'old user name unknown'
fi

grep -q "^$new_user:" /etc/passwd

if [ $? -ne 0 ]
then
usage 'new user name unknown'
fi

doit $root

Created by zoperoot
Last modified 2004-06-20 11:09 AM
« September 2010 »
Su Mo Tu We Th Fr Sa
      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    
 
 

Powered by Plone

This site conforms to the following standards: