Delete directory from ghost owner with awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Delete directory from ghost owner with awk
# 1  
Old 08-17-2011
Delete directory from ghost owner with awk

Hi all Unix gurus Smilie

I really need urgent help with deleting ghost (owner is only numbers) accounts on our OS X server in users home directory.

I get this ghost accounts after deleting a user from Workgroup Manager.

I have done a script that I can see this ghost accounts with:
Code:
ls -la | Awk '{print $3}' | grep -v '[A-Za-z][A-Za-z]'

The thing here is that I want to delete the directory to this owner, here is some examples for users on the server.

Code:
drwxr-x---+  12 5074          personal    408 Aug 27  2010 AbdMow1121
drwxr-x---+  12 5248          personal    408 Aug 27  2010 AdaChr0814
drwxr-x---+  16 5196          personal    544 Aug 27  2010 AdrLuk0908
drwxr-x---@  17 1355          personal    578 Jul 29  2009 AgnBr02
drwxr-x---+  12 5276          personal    408 Aug 27  2010 AgnHel0616
drwxr-x---+  16 AgnMat0422    personal    544 May 31 12:39 AgnMat0422
drwxr-x---+  12 5274          personal    408 Aug 27  2010 AhmHas0101
drwxr-x---+  17 AidHod0126    personal    578 May 30 12:18 AidHod0126
drwxr-x---+  12 5288          personal    408 Aug 27  2010 AidJac0313
drwxr-x---   14 1104          personal    476 Apr 23  2008 AleBe11
drwxr-x---+  12 5242          personal    408 Aug 27  2010 AleBor1201
drwxr-x---+  12 5265          personal    408 Aug 27  2010 AleFor0217
drwxr-x---+  12 5019          personal    408 Aug 27  2010 AleHal0502
drwxr-x---+  16 AleNou1112    personal    544 May 30 11:03 AleNou1112
drwxr-x---+  12 5051          personal    408 Aug 27  2010 AleSto1126
drwxr-x---+  12 5084          personal    408 Aug 27  2010 AleSve0201
drwxr-x---+  12 5371          personal    408 Aug 27  2010 AleVel0209
drwxr-x---+  12 5372          personal    408 Aug 27  2010 AleWik1220
drwxr-x---+  12 5025          personal    408 Aug 27  2010 AliAbb0825
drwxr-x---   14 1406          personal    476 May 23  2008 AliDe01
drwxr-x---+  26 AliSek1027    personal    884 May 26 11:38 AliSek1027
drwxr-x---+  12 5231          personal    408 Aug 27  2010 AmaAho0414
drwxr-x---+  12 5251          personal    408 Aug 27  2010 AmaDan0216
drwxr-x---+  20 AmaHag1225    personal    680 Jun  8 14:34 AmaHag1225
drwxr-x---+  18 AmaIbr1223    personal    612 May  6 09:19 AmaIbr1223
drwxr-x---+  17 AmaLun0407    personal    578 Jun  1 11:25 AmaLun0407
drwxr-x---+  12 5076          personal    408 Aug 27  2010 AmaPau0627
drwxr-x---+  17 AmaSte0511    personal    578 Apr 15 12:51 AmaSte0511
drwxr-x---+  12 5050          personal    408 Aug 27  2010 AmeSba0510
drwxr-x---+  19 AnaKor0330    personal    646 May 27 14:45 AnaKor0330
drwxr-x---+  12 5243          personal    408 Aug 27  2010 AndBro0624
drwxr-x---+  12 5245          personal    408 Aug 27  2010 AndCav0222
drwxr-x---+  12 5031          personal    408 Aug 27  2010 AndEne0522
drwxr-x---+  14 5217          personal    476 Dec 15  2010 AndHul1213
drwxr-x---+  14 AndSeg0308    personal    476 May 31 11:44 AndSeg0308
drwxr-x---+  12 5230          personal    408 Aug 27  2010 AngAgd0510
drwxr-x---+  12 5238          personal    408 Aug 27  2010 AngBae0802
drwxr-x---+  12 5291          personal    408 Aug 27  2010 AngJoh1231
drwxr-x---+  12 5049          personal    408 Aug 27  2010 AnhSab0731
drwxr-x---   13 1065          personal    442 Feb 14  2008 AnnAm02

I want to delete all directories with numbers (only 4 digits) as owner like:

5074 = AbdMow1121
5248 = AdaChr0814
5196 = AdrLuk0908
1355 = AgnBr02
5276 = AgnHel0616

Hope someone can help me with this, because I have many user home directories that needs to be removed from the server.

Thank you very much in advance

Last edited by vbe; 08-17-2011 at 12:45 PM.. Reason: please use code tags for your data or code
# 2  
Old 08-17-2011
These ghosts user users as you call them seem to be past defined users removed from the passwd file but where you chose NOt to remove their files... You should be able to extract all defined users and compare with the home directory (what is not in passwd is old and so you see the affected UID given at the time)
# 3  
Old 08-18-2011
Quote:
Originally Posted by vbe
These ghosts user users as you call them seem to be past defined users removed from the passwd file but where you chose NOt to remove their files... You should be able to extract all defined users and compare with the home directory (what is not in passwd is old and so you see the affected UID given at the time)
Hi!

I'm not exactly sure how you mean now. I know, it's strange that when I delete an account in workgroup manager, the home directory for that user doesn't removes. So that is why I need a script to delete this numbered owners on the server.

I don't think I can do this on an easier way than to delete the user home directories with some script... But I need help do do this.

EDIT

This seems to be more difficult than I first thought. But I have some idea how to do this.
With this script ls -la | Awk '{print $3" "$9}' | grep '[0-9][0-9][0-9][0-9]'

I get this result:
5074 AbdMow1121
5248 AdaChr0814
5196 AdrLuk0908
1355 AgnBr02
5276 AgnHel0616
AgnMat0422 AgnMat0422
5274 AhmHas0101
AidHod0126 AidHod0126
5288 AidJac0313
1104 AleBe11
5242 AleBor1201
5265 AleFor0217
5019 AleHal0502

I'm very near my goal now, the only thing I want to do know is to filter the names from the first column like:
AgnMat0422
AidHod0126
AleNou1112

And so on...

Do someone know any possibility to only focus on the first column with grep so grep doesn't notice about the second column so I only need to type:
ls -la | Awk '{print $3" "$9}' | grep '[0-9][0-9][0-9][0-9]' But are only interested to grep $3 not the $9 column...

---------- Post updated at 09:11 AM ---------- Previous update was at 07:00 AM ----------

I solved the problem myself:
I was very near the solution but forget the ^ in the beginning of the grep syntax. Here is the code to delete this user directories:
Code:
for i in `ls -la | Awk '{print $3" "$9}' | grep '^[0-9][0-9][0-9][0-9]'` ; do rm -rf $i; done

Smilie

Last edited by vbe; 08-18-2011 at 10:01 AM.. Reason: use code tags please for code and data
# 4  
Old 08-18-2011
I was a bit reticent to answer for the only Mac OSX I have is my power book I bought second hand last year and I was very confused ( and still am..) not finding the files where expected (like all other unixes...). Glad you solved by yourself and thaks for letting us know
About the accounts I dont know linux enough to tell but lets say on HPUX when you remove a user you have the choice to remover all files belonging to user and home directory, leave the files whre they are ( your case) or give them to another user. Files created by such users could be elsewhere also...( but again I have no knowledge on Mac...). So if you had a list of uid that have files that have to be removed you would a root look for them and remove them, so looking for them: say uid 2020
Code:
find . -user 2020 -print 
# give them to user dummy...
find . user 2020 -print -exec chown dummy {} \;
# And if you had a list of UIDs in  UIDfile: the script would be:
#!/usr/bin/ksh
while read ID
do 
   find . user $ID -print -exec chown dummy {} \; 
done <UIDfile

I let you correct for removing...
# 5  
Old 08-18-2011
This would be alot cleaner->
Code:
ls -l | awk '$3 ~ /^[0-9][0-9][0-9][0-9]$/ {system ("echo " "found file " $9)}'

Try creating some files with fictitious four digit UID's then run this command. When you familiarize yourself with awk's system call then apply the appropriate command.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a way to restrict a user (owner) to execute scripts from a specific directory

Hello, I have a user Bob on a RHEL 7 server1. Where his script area is "/home/Bob/scripts/" and he is the owner for this directory. On the server1, there is a NFS mount from another server2, with path as "/global/work/" and Bob is the owner for this directory too in server2. (Same UID and GID... (5 Replies)
Discussion started by: karumudi7
5 Replies

2. AIX

How to set owner and permission for files/directory in directory in this case?

Hi. My example: I have a filesystem /log. Everyday, log files are copied to /log. I'd like to set owner and permission for files and directories in /log like that chown -R log_adm /log/* chmod -R 544 /log/*It's OK, but just at that time. When a new log file or new directory is created in /log,... (8 Replies)
Discussion started by: bobochacha29
8 Replies

3. Solaris

Changing file/directory owner

Hi , I want to change owner of files or folder from 23186 to dsadm, Present ------- -rw-r--r-- 1 23186 gdstage 10240 Oct 31 2007 BLTRS drwxrwxrwx 3 23186 gdstage 512 Sep 1 2010 sql Required as ----------- -rw-r--r-- 1 dsadm gdstage 10240 Oct 31 2007 BLTRS drwxrwxrwx 3 dsadm gdstage... (6 Replies)
Discussion started by: sridhardwh
6 Replies

4. OS X (Apple)

Logic of owner of directory or file

Would the owner as listed , be the one who made the directory in the first place. when any one could use chown in a script. I do not know where this directory came form or who made it. And am attempting to just find out more info on it. what could i use to find its origins and its why does it... (3 Replies)
Discussion started by: cowLips
3 Replies

5. AIX

Unix root directory owner wrong AIX 5.3

The a chown was done and instead of using ./ a / was used and root ownership files got changed. I need to change the ownership of the files/directory back - backups are not working and I am concerned a reboot will not be successful. Can anyone provide the ownership of these files/directories... (6 Replies)
Discussion started by: spike1
6 Replies

6. UNIX for Dummies Questions & Answers

Group files by owner and show directory

Hello, i would like to find huge files and group them by owners. To find big files i use this command: ls -lR | sort -bnr +4 | head -n 75 which give me 75 biggest files, then i need to see in which subdirectory is every file. second thing i dont know is how to group those files by owner, could... (6 Replies)
Discussion started by: dealer1985
6 Replies

7. UNIX for Dummies Questions & Answers

How to display only Owner and directory/sub directory names under particular root

hai, I am new to Unix, I have a requirement to display owner name , directory or sub directory name, who's owner name is not equal to "oasitqtc". (here "oasitqtc" is the owner of the directory or sub directory.) i have a command (below) which will display all folders and sub folders, but i... (6 Replies)
Discussion started by: gagan4599
6 Replies

8. Shell Programming and Scripting

to parse a directory and its subdirectories and find owner name of files

hi all, i need to capture all the files in a directory and its subdirectories that have owner name different than the root owner. for one file it is " stat -c %U filename " but i need to search for each and every file and record it. thanks in advance (14 Replies)
Discussion started by: vyasa
14 Replies

9. UNIX for Dummies Questions & Answers

ghost file, delete help

I'm trying to delete a folder in my trash that contains no files or hidden files and nothing happens. It is still there. However, when I try to delete everything in the folder in terminal I receive an error: Ti?ng Viê?t.html: No such file or directory. I can't find this file anywhere on my tiger... (1 Reply)
Discussion started by: jopper
1 Replies

10. Shell Programming and Scripting

determine owner directory permissions from within the directory

From within a directory, how do I determine whether I have write permission for it. test -w pwd ; echo ? This doesn't work as it returns false, even though I have write permission. (4 Replies)
Discussion started by: Sniper Pixie
4 Replies
Login or Register to Ask a Question