What process has what files open?

I think everyone knows the problem: Sometimes some process changes files while you want to play with them. Be it unmounting a device, or simply some concurrent writing. Under Linux it’s fairly easy to find out what process has what file descriptors open. That’s all simply stored in /proc. Now I wanted to find | grep it and … well, I simply don’t really like Bash so I wrote a small script in Perl that will print all open files with their name and the process id that has the file open:

my $procdir = '/proc';
opendir(PROCDIR,"/proc") or die("Can't open proc...");
my @procs;
while(defined($file = readdir(PROCDIR))){
    if ($file =~ /^[0-9]/) {
        if (-e '/proc/'.$file.'/fd/'){
            push @procs,$file;
    my $pid = $_;
    my $fdpath = '/proc/'.$_."/fd/";
    while(defined($file = readdir(DIR))){
        if (-l $fdpath.$file){
            if(defined($act = readlink($fdpath.$file))){
                if ($act =~ /^\//){
                    print "[PID:$pid]",$act,"\n";

Perhaps someone else will find this useful :-) It’s not fast, it’s not nice, but it does the job :-)

comments powered by Disqus