Finding orphan files in linux system after owner account is deleted

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • powerfulperl
    New Member
    • Feb 2010
    • 14

    Finding orphan files in linux system after owner account is deleted

    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.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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

    Comment

    • powerfulperl
      New Member
      • Feb 2010
      • 14

      #3
      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..

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Ok, dude, so give it a shot. Any Perl command you want to use, simply look up its page at perldoc.perl.or g.

        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

        Comment

        • RonB
          Recognized Expert Contributor
          • Jun 2009
          • 589

          #5
          See:

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

          Comment

          • powerfulperl
            New Member
            • Feb 2010
            • 14

            #6
            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.

            Comment

            • RonB
              Recognized Expert Contributor
              • Jun 2009
              • 589

              #7
              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.

              Comment

              Working...