Skip to content

Instantly share code, notes, and snippets.

@syohex
Created April 2, 2014 09:54
Show Gist options
  • Save syohex/9931207 to your computer and use it in GitHub Desktop.
Save syohex/9931207 to your computer and use it in GitHub Desktop.
Convert file descriptor to file name in Linux
#!/usr/bin/env perl
use strict;
use warnings;
use File::Spec;
my @fhs;
opendir my $dh, '/etc';
for my $entry (readdir $dh) {
my $file = File::Spec->catfile('/etc', $entry);
next unless -f $file;
open my $fh, '<', $file or next;
push @fhs, $fh;
last if @fhs > 20;
}
show_fd_names();
closedir $dh;
sub show_fd_names {
opendir my $dh, "/proc/self/fd";
for my $entry (readdir $dh) {
my $file = File::Spec->catfile('/proc/self/fd', $entry);
next unless -l $file;
my $opened_file = readlink $file;
print "fd=$entry => $opened_file\n";
}
closedir $dh;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment