http://www.perlmonks.org?node_id=942294

mmw has asked for the wisdom of the Perl Monks concerning the following question:

How do i retrieve the major and minor number using File::stat?
#!/usr/bin/perl use File::Find; find sub { (-c | -b ) && push(@files,$File::Find::name) } , ("/dev") ; sub retHash () { my ($name, $major, $minor ) = split(",",stat -c '%n,%t,%T' $_`); my $return = {name=> $name, major=> hex($major), minor=> hex($minor +) }; return $return; } @files = map ( &retHash($_) , @files); foreach $item (@files) { print "$item->{name} $item->{major} $item->{minor}\n"; }

Replies are listed 'Best First'.
Re: How do i find a file major and minor number
by johngg (Canon) on Dec 07, 2011 at 17:55 UTC

    Probably something similar to this using lstat or stat.

    knoppix@Microknoppix:~$ perl -E ' > $rdev = ( stat q{/dev/zero} )[ 6 ]; > $minor = $rdev % 256; > $major = int( $rdev / 256 ); > say $rdev; > say $major; > say $minor;' 261 1 5 knoppix@Microknoppix:~$ ls -l /dev/zero crw-rw-rw- 1 root root 1, 5 Dec 7 11:39 /dev/zero knoppix@Microknoppix:~$ perl -E ' > $rdev = ( stat q{/dev/zram0} )[ 6 ]; > $minor = $rdev % 256; > $major = int( $rdev / 256 ); > say $rdev; > say $major; > say $minor;' 64512 252 0 knoppix@Microknoppix:~$ ls -l /dev/zram0 brw-rw---- 1 root disk 252, 0 Dec 7 11:39 /dev/zram0 knoppix@Microknoppix:~$

    I hope this is helpful.

    Cheers,

    JohnGG