do a full comparison of folder contents in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting do a full comparison of folder contents in script
# 1  
Old 11-20-2009
do a full comparison of folder contents in script

Hello everyone....

I have a small issue here at work and I am trying to script out a way to automate a fix for it. I have a small number of users (I work in a 1:1 with 6,000 macbooks) that aren't really managed in my deployment. They are managed with a few policies, but the policies are broken because they never turn their laptops in or get them reimaged. I have had to wipe LDAP a few times for upgrades/maintenance/fixes and their synchronized portable home directory has a different UID than the new UID since I did fresh imports of user accounts both times I destroyed and rebuilt LDAP.

So, I have a script written which seems to be going the correct way of doing this, but I am not exactly a Unix wizard so I have some basic questions hopefully someone here can help me with.

The script's function will be to copy all local user accounts in /Users to /Users/username.bu for a full back up of the account. Then, it will initiate a log out, and a log out hook that will run the second script that will verify all data is copied to the back up folder (.bu) and then delete the account from directory services and wipe the original data out of their local user account.

This way, when the user logs in next, they will have pulled down the new account to their machine that has the exact same user/password but this time it will be the right UID. Then they will be in the proper OD groups and get group policy as normal.

I hope that makes sense. Also, only accounts that have a UID higher than 1000+ are directory accounts, so I want to ignore all accounts with an UID less than 999.

So far here is my script I wrote, and like I mentioned, I am completely self taught and have no formal education in programming.

Code:
#!/bin/bash

#
# First we need to grab the synchronnized user account
# All directory accounts will have a UID greater than 1000
# All local accounts will have a UID of 500 to 999
# We only want to move accounts with UID of greater than 1000
# This will also exclude the shared folder
#

userlist=`/bin/ls /Users | /usr/bin/grep -v "^Shared"`

checkuid=`/usr/bin/dscl . read /Users/$i UniqueID | /usr/bin/awk '/ID/ { print $2 }'`

for i in $userlist ; do

if (( $checkuid > 999 )) 

    then /bin/mkdir /Users/$i.bu ;; /bin/cp -R /Users/$i /Users/$i.bu

    else /bin/echo "$i is not a directory user"
   
    exit 5

fi

done

#
# now the tricky part we need to verify what was done, is done then delete the old account
# so the user can sync the new account with the proper UID
#

homefolder=`/bin/ls -l $userlist/`

backup="/Users/$userlist.bu"

# now compare contents

if [[ $homefolder == $backup ]]

    then /usr/sbin/dscl . -delete /Users/$userlist

    else /bin/echo "Contents do not match, exiting"

    exit 1

fi

done

#
# Now remove the contents of those user accounts by comparing what has 
# .bu on the end
#

removeuser=`/bin/ls /Users | /usr/bin/grep ".bu"

/bin/rm -rf /Users/$removeuser

exit 0

Now the second script will just copy that data back after the user logs in again and resynchronizes their PHD with the proper UID. Then the last bit will apply ownership to that new UID and exit.

A few questions....

1) Should I have used an integer comparison when comparing the current UID to it being greater than 999?

2) There are probably some issues with this script as I have not tested it out fully yet, because I was wanting some feed back to see if there was a better way to go. Is there a more efficient way to go about this?


Thanks for anyone who can help, it is greatly appreciated.

Tom

Last edited by tlarkin; 11-20-2009 at 05:49 PM..
# 2  
Old 11-20-2009
I am not understanding this.
This lists all of the users with UID >= 1000
Code:
awk '$3 > 999 { print "username=", $1, " login directory=", $6}' /etc/passwd

You can change the login directory for any user by using the the equivalent of the useradd utility. Editing the /etc/passwd file is not a great idea on some systems. What flavor of unix do you have?

You can also create a common login entry:
modify /etc/profile
Code:
    if [[ $UID -gt 999 ]] ; then
        # do whatever you need here.  Based on the userid
    fi

You do not need to put code in each users home directory. If you put code in /etc/profile it is protected from modification by users.


But - then I'm confused as to what you are really doing.
# 3  
Old 11-21-2009
I am using OS X 10.5.8 from Apple. All of there user accounts either local or network are handled by directory services. All local accounts have a range of UID 500 to 999 and all directory accounts have UID 1001 and up, with UID 1000 being that of the directory admin account.

I am on my third year of my 1:1 and some staff members have never had their laptops wiped and reimaged. During that time I have completely destroyed and recreated LDAP, so all those user accounts got new UIDs. Some of those faculty members that have their old PHD synchronized to their machines have a mismatching UID in Open Directory, thus that account is not a member of any OD groups.

Sorry if I am not making any sense.

So I need to loop through all users in /Users and check their UID. If the account is local, I don't want to touch it and I want it to be skipped. So any account that is UID 500 to 999 should not be touched. Any account that is in the OD account UID range of 1001+ needs to be copied to a back up folder, then have the account erased and when the new account is created I will have a self service script where the user can execute it and it will copy all data back where it was and apply ownership to the new user account.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Best way to move the contents of a folder to another one

what is the best way to move the contents of a folder to another one without deleting the structure of the first one. the contents could include subfolder too. both folder, the source-folder and the target-folder are on the same host. any idea is appreciated . (7 Replies)
Discussion started by: andy2000
7 Replies

2. UNIX for Dummies Questions & Answers

Script that sums the contents of a folder (help me)

I'm looking for a script that sums the contents of a folder, When you give a parameter to the script , i want to know the size of the directory, the number of files, number of folders, These are commands that I have already found du -s find . -type f | wc -l find . -type d | wc -ly ... (19 Replies)
Discussion started by: Roggy
19 Replies

3. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

File comparison based on contents

Hi I have 2 files 1.del ---- 1,2,3,4,5 1,2,3,4,4 1,1,1,1,2 2.del ---- 1,2,3,4,5 1, 1,2,3,4,4 1,1,1,1,2 I need to compare the above two files in unix, as in the output should only tell the difference in contents as I should get only the line 1 ( from 2.del) , rest all lines are... (4 Replies)
Discussion started by: Ethen561
4 Replies

5. Shell Programming and Scripting

copy folder and its contents to another folder

Hi experts, I am coming to you with this basic question on copying a folder and its content from one location to another folder using PERL script. This is my requirement. I have a folder AB under /users/myhome I want to copy AB and its contents to /user/workspace. Finally it should... (1 Reply)
Discussion started by: amvarma77
1 Replies

6. Shell Programming and Scripting

Script to create a folder with contents

I am working on HP Unix. Require a script for the below requirement. Requirement are: 1. Need to create a folder with files. 2. The folder should have a naming convention like - LRIC_ARCHIVE_ddmmyyhhmmss_version_nnn, the version number needs to be selected from an oracle table. 3. When the... (4 Replies)
Discussion started by: Roadies99
4 Replies

7. Shell Programming and Scripting

Need to extract a folder from a full path

Hi everyone, I have different folders which looks like this: /mnt/ecrm/master/ecrm/templates/brochure/de_DE/zeitlos.ott /mnt/ecrm/master/ecrm/templates/mail/en_US/default.html /templates/header_and_footer/en_US/default.txt I want to get the bold text only in a variable. I already have a... (3 Replies)
Discussion started by: evilass
3 Replies

8. Shell Programming and Scripting

Two folder comparison

Hi, I have few files in a directory. I have the same set of files in another directory. I need to remove the lines starting with the word 'HDR' and 'FTR' if present from all the files in both the directories. Then i need to sort the contents from all the files in both the directories and then... (10 Replies)
Discussion started by: ragavhere
10 Replies

9. UNIX for Dummies Questions & Answers

copy folder contents

I need to make a new dir in side the dir lab5 the new dir is called testLab5 without changing directories copy all files from your lab5 directory into your testLab5 directory then i have to without chaning directories and using exactly one command remove all files that start with the... (1 Reply)
Discussion started by: robsk8_99
1 Replies

10. UNIX for Dummies Questions & Answers

Folder Contents

Hi, I'm trying to allow people to access the contents of a folder on a web site, I am automatically placing files in this folder for people to download. I'm using Apache on Mac OS X, if that makes a difference. Can anyone help with this? I've found no documentation on this so far... ... (6 Replies)
Discussion started by: spencer
6 Replies
Login or Register to Ask a Question