Skip to main content

how to use perl-find-rules to find files with 777 perms [Resolved]

I want to use perl::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 perl-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);
}

Question Credit: danidar
Question Reference
Asked January 11, 2019
Tags: perl, find, rules
Posted Under: Network
14 views
1 Answers

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).


credit: haukex
Answered January 11, 2019
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA