Finding Unlinked File

Here is the Perl script that I wrote to find the file that had been opened and then unlinked:
#! /usr/local/bin/perl

# find-inodes - looks for open file descriptors that point to unlinked files
#
# Mike Schuh, September 1998

$partition = "85,4";	# a device number
$device = "dev:" . $partition;

open(PS, "ps -e|tail +2|awk '{print $1}'|") ||
	die "could not open pipe from ps";

while() {
  chop;
  ($pid,$junk) = split(' ', $_, 2);
  #printf("trying PID $pid\n");
  open(PFILES, "pfiles $pid 2>&1 |grep $device|") ||
	die "could not open pipe from pfiles for PID $pid";
  while() {
    chop; 
    #printf("$_\n");
    ($filedes,$flags,$mode,$dev,$ino,$fuid,$gid,$stuff,$perms,$junk) =
	split(' ', $_, 10);
    printf("process %6d file %2d dev %s inode %6s\n", $pid, $filedes,
		$dev, $ino);
    $inode_numeric = substr($ino, 4);
    #printf("inode = %d\n", $inode_numeric);
    open(FIND, "find /opt -inum $inode_numeric -ls 2>/dev/null|") ||
	die "could not open pipe from find for inode $inoed_numeric";
    while() {
      ($f_inode,$blocks,$f_perms,$link_count,$owner,$group,$size,$remainder) =
	split(' ', $_, 8);
      printf("inode %6d size %8d %s\n", $f_inode, $size, $remainder);
    }
    close(FIND);
  }
  close(PFILES);
}

# end of find-inodes

If I were to write this again, I would read the file system once (with either the find command that I used or a combination of the readdir() family of functions), storing the inode numbers in a hash. Also, I would have the script detect the missing file, instead of depending on the user (me!) watching the output and noticing when find didn't find a file. Using strict would be a good idea, along with chomp; instead of chop;. I would also spell all of the scalar names correctly...
Back to "Technical Solutions"
To Mike Schuh's home page
Mail (but not spam) is welcome to:
schuh AT farmdale D0T com

Thank you for the visit.