- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- determine file type with perl
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-18-2008 02:54 AM
тАО12-18-2008 02:54 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-18-2008 02:58 AM
тАО12-18-2008 02:58 AM
Re: determine file type with perl
http://docs.hp.com/en/B2355-60130/stat.2.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-18-2008 02:59 AM
тАО12-18-2008 02:59 AM
Re: determine file type with perl
Looks good in this link.
http://www.unix.com/shell-programming-scripting/24346-determine-file-type-perl.html
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-18-2008 03:11 AM
тАО12-18-2008 03:11 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-18-2008 05:52 AM
тАО12-18-2008 05:52 AM
SolutionYou 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-23-2008 06:46 AM
тАО12-23-2008 06:46 AM
Re: determine file type with perl
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";
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-23-2008 09:47 AM
тАО12-23-2008 09:47 AM
Re: determine file type with perl
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...