By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,807 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,807 IT Pros & Developers. It's quick & easy.

Finding orphan files in linux system after owner account is deleted

P: 14
I want to write a perl script for the below condition

Files not contained in the user's home directory and files contained in the user's home directory, and owned by that user(user's account deleted), will remain on the system. The file will still be owned by the deleted account's user ID (UID).

I want to throw a warning message whenever files(orphaned files) with no owners (i.e owner's deleted) are exists in the system.
Mar 15 '10 #1
Share this Question
Share on Google+
6 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Ok, you have told us what you want to do, but do you have a question? Have you started coding it? If so, and you are stuck, post your code here and ask your question(s) and we will help you.

My point is that we are not a coding service, but will happily help you achieve your goal.

Regards,

Jeff
Mar 16 '10 #2

P: 14
Dude i want to write a perl script.. I thought of using the stat function but dont have any clue about how to go about it.. any help will be appreciated... thanks..
Mar 16 '10 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Ok, dude, so give it a shot. Any Perl command you want to use, simply look up its page at perldoc.perl.org.

The issue I have with stat though, with relation to your issue, is that it will return the users UID, even if the file in question has the users name on it. The files you are looking for have the users UID, due to the acct being deleted already.

What you really want to do is scour the directory system and look for files that are owned by the UID in question. I have a few ideas in my head at the moment right now on how to do it, but to put them here would be like a book and probably be too confusing to read. (trust me).

One idea is to use the system level command "find" to look for all files on the system owned by the specific UID and store the results in a file. Then, open the file and read it in and then you can do whatever you need to to the files. You can even use Perl's chown function to change ownership of the files to someone else, or even Perl's unlink function to remove the file completely. Its up to you.

If you get stuck, post your code here and let us know the problem you are having and we will help.

Regards,

Jeff
Mar 16 '10 #4

Expert Mod 100+
P: 589
See:

File::Find - http://search.cpan.org/~dapm/perl-5....b/File/Find.pm
man find
man stat
man finger
Mar 16 '10 #5

P: 14
What u said is exactly true, but the problem here is i do not have any list of users whose accounts have been deleted. The first thing what i need to do is find the files whose owner account have been deleted and report the warning message for those files saying .. ORPHAN FILES FOUND IN THE FILE SYSTEM.
Mar 17 '10 #6

Expert Mod 100+
P: 589
What makes you think you need to have a list of deleted users prior to running the script?

Step 1) build a hash table of all user accounts in /etc/passwd

Step 2) use the system's find utility or the perl File::Find module to get each and every file in the system.

Step 3) as you "find" each file, stat it to get the uid.

Step 4) lookup the uid in the hash table
a) do something if it exists
b) do something else if it doesn't


That's the basic outline for this homework assignment.
Mar 17 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.