Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: check if file is executable by user/group/world?

by vinoth.ree (Monsignor)
on Aug 06, 2009 at 07:08 UTC ( #786331=note: print w/replies, xml ) Need Help??

in reply to check if file is executable by user/group/world?

I assume that you want to check the permission of the file

Fcntl':mode' module contains all the constants and functions you need to extract the file type and permissions. S_IMODE is the function which helps you extract the permissions: $permissions = S_IMODE($mode);

  • Comment on Re: check if file is executable by user/group/world?

Replies are listed 'Best First'.
Re^2: check if file is executable by user/group/world?
by XooR (Beadle) on Aug 06, 2009 at 08:00 UTC

    Use "use Fcntl ':mode';" to import S_I* constants. They are just bitmasks that masks bits from portions of files permissions. In Unix systems they are grouped into 4 groups of 3 bits. First three are for setuid(4), setgid(2), stickybit(1). Other three groups of bits are for user, owner group and others. Permissions are read(4), write(2) and execute(1).

    Permissions are often represented in octal form, so for this permission "rwxr-x-w-" you will have octal one 0752.

    I recommend you reading "perldoc -f stat" document, you have it all there. Except some of the stuff about inner working, which you could find in "info glibc" if you are working on some Linux system.

    Code from stat doc page:
    #!/usr/bin/env perl use Fcntl ':mode'; use strict; use warnings; my $mode = (stat($ARGV[0]))[2]; my $user_rwx = ($mode & S_IRWXU) >> 6; my $group_read = ($mode & S_IRGRP) >> 3; my $other_execute = $mode & S_IXOTH; printf "Permissions are %04o\n", S_IMODE($mode), "\n"; my $is_setuid = $mode & S_ISUID;
Re^2: check if file is executable by user/group/world?
by Utilitarian (Vicar) on Aug 06, 2009 at 07:57 UTC
    Just a quick clarification on the source of $mode in the ree's example above
    perl -e 'use Fcntl qw(:mode);$permissions=S_IMODE((stat("filename"))[2 +]);print $permissions,"\n";'

      Sorry!Utilitarian I just given the hint, and missed something,Below the code I just tried,

      use Data::Dumper; use Fcntl ':mode'; my $filename=""; my @array=lstat($filename); $permissions = sprintf "%04o", S_IMODE($array[2]); print "File permissions: $permissions\n";

      Removed a file type line.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://786331]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2017-11-19 04:03 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (278 votes). Check out past polls.