Operating System - HP-UX
1753416 Members
7504 Online
108793 Solutions
New Discussion юеВ

Re: determine file type with perl

 
SOLVED
Go to solution
Billa-User
Regular Advisor

determine file type with perl

hello,

i like to know please how can i determine file type with perl not using the unix "file" program .

i have following files (normal,link,special file) and directory as examples:

brw-r--r-- 1 root sys 101 0x000000 Dec 18 11:41 bdevice
crw-r--r-- 1 root sys 101 0x000000 Dec 18 11:40 cdevice
drwxr-xr-x 2 root sys 96 Dec 18 11:39 directroy
-rw-r--r-- 1 root sys 0 Dec 18 11:39 file
lrwxr-xr-x 1 root sys 4 Dec 18 11:41 link -> file

i like to know :

file => file
directory => directory
link => link
bdevice => block device
cdevice => character device

i tried to use "stat", can i interpret "modus" ?
perl -e '$mode=(stat $ARGV[0])[2];printf "%04o\n",$mode ' file

regards
6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: determine file type with perl

Look at the stat(2) system call and see what fields have what you want.
http://docs.hp.com/en/B2355-60130/stat.2.html
Steven E. Protter
Exalted Contributor

Re: determine file type with perl

Shalom,

Looks good in this link.

http://www.unix.com/shell-programming-scripting/24346-determine-file-type-perl.html

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Billa-User
Regular Advisor

Re: determine file type with perl


you mean : ?

checktype_filename($filename)

Opens $filename (if possible; if not, returns undef) and returns the MIME type of the file.

but is this also for a directory?
James R. Ferguson
Acclaimed Contributor
Solution

Re: determine file type with perl

Hi:

You can use the Stat::lsMode module as you suggest with files or directories.

In you original code snippet, your octal permissions should be deduced with:

# perl -e '$mode=((stat $ARGV[0])[2])&07777;printf "%04o\n",$mode'

...to mask everything but the permissions.

You can report files, directories and links by testing the last stat() structure for :

1. ...print "file" file if -f _
2. ...print "directory" if -d _
3. ...print "symlink" if -l _
4. ...print "block device" if -b _
5. ...print "char device" if -c _

...of course some of these are mutually exclusive. For example, you can't have both a block and a character device for the same file.

Regards!

...JRF...
Billa-User
Regular Advisor

Re: determine file type with perl

thanks to all, here my solution:

use POSIX;
use Fcntl ':mode';

if ( -e $ARGV[0] )
{
# note: if the file is a symlink (type "l"), you might
# want to use lstat() instead of stat()
# note: "use POSIX" doesn't seem to provide constants for types
# "n" "s" or "l"

$mode = (lstat $ARGV[0])[2];

$type = "b" if (S_ISBLK($mode));
$type = "c" if (S_ISCHR($mode));
$type = "d" if (S_ISDIR($mode));
$type = "p" if (S_ISFIFO($mode));
$type = "s" if (S_ISSOCK($mode));
$type = "l" if (S_ISLNK($mode));
$type = "f" if (S_ISREG($mode));

printf "%s\n",$type;

}
else
{
# file not found
printf "file not found\n";
}
James R. Ferguson
Acclaimed Contributor

Re: determine file type with perl

Hi (again):

Instead of having to load the 'Fcntl' and 'POSIX' modules, you could simply use the file test operators like:

# cat ./showmodes
#!/usr/bin/perl
use strict;
use warnings;
my $name = shift or die "Usage: $0 file|directory\n";
my $mode = ( ( lstat($name) )[2] ) & 07777 or die "Can't stat() '$name'\n";
printf "%s %04o %s\n",
( -f _ ) ? 'f'
: ( -l _ ) ? 'l'
: ( -d _ ) ? 'd'
: ( -b _ ) ? 'b'
: ( -c _ ) ? 'c'
: ( -p _ ) ? 'p'
: ( -S _ ) ? 's'
: '?',
$mode,
$name;
1;

...for example:

# ./showmodes /tmp
d 1777 /tmp
# ./showmodes /usr/adm
l 1755 /usr/adm
# showmodes /tmp/mysql.sock
s 0777 /tmp/mysql.sock

Regards!

...JRF...