danidar
danidar

Reputation: 309

How to use Perl's File::Find::Rule module to find files with 777 perms

I want to use perl File::Find::Rule in order to find files in the server that have perms 777

I know that the module has stats tests so i could simply do this:

$rule->mode(33279)

I found the 33279 by creating a file and printing the permission on it assuming that File::Find::Rule takes decimal? or should it be formatted somehow?

Is this the right approach to have all the file that have exactly the 777 permissions?

this is a script that finds all files on the home dir of a test server.. i want to change it so that it only finds those with 777 permissions.

#!/usr/bin/perl
use strict;
use warnings;
use File::Find::Rule;

my $rule = File::Find::Rule->new;
$rule->file;
$rule->name( '*' );
my @files = $rule->in( "/root" );

for my $file (@files) {
     my $mode = (stat $file)[2];
     printf ("%04o %s\n",$mode & 07777, $file);
}

Upvotes: 3

Views: 215

Answers (2)

aod
aod

Reputation: 193

Using File::Find::Rule is cool, but you could do it easily with find and get the answers back in perl:

@files = split /\n/, `/bin/find /root -perm 777`;

Upvotes: -1

haukex
haukex

Reputation: 3013

The mode includes the file permissions and type. You need to mask it so that you only get the permission bits. Personally I'd implement a custom rule:

use warnings;
use strict;
use File::stat;
use Fcntl qw/S_IMODE/;
use File::Find::Rule 'rule';

my $rule = rule->file->exec(sub{ S_IMODE(stat($_[2])->mode)==0777 });

my @files = $rule->in('/root');
for my $file (@files) {
    print $file, "\n";
}

Note that this masked mode still includes the setuid/setgid/sticky bits (often known as Xst). If you want to ignore those too, and check only the ugo/rwx bits, then you'd have to mask against 0777 (e.g. $mode & 0777).

Upvotes: 3

Related Questions