1754875 Members
5550 Online
108827 Solutions
New Discussion юеВ

Perl and find command

 
SOLVED
Go to solution
Robert Legatie
Advisor

Perl and find command

Hello,
I am using find2perl for finding files with no UIDs.

#! /usr/opt/perl5/bin/perl -w
eval 'exec /usr/opt/perl5/bin/perl -S $0 ${1+"$@"}'
if 0; #$running_under_some_shell

use strict;
use File::Find ();

# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;

sub wanted;


my (%uid, %user);
while (my ($name, $pw, $uid) = getpwent) {
$uid{$name} = $uid{$uid} = $uid;
}


# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '/');
exit;


sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);

(
(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
($dev >= 0)
||
($dev >= 0)
) &&
!exists $uid{$uid} &&
print("$name\n");
}

Can someone help in fixing the following errors.
Use of uninitialized value in numeric ge (>=) at ./find_owner_script.pl line 35.
Use of uninitialized value in exists at ./find_owner_script.pl line 35.

Also I would like to exclude proc from the search.

Thanks.
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor

Re: Perl and find command

Hi Robert:

Why are you adding Perl to what you can't do in a shell?

You closed this thread without evaluatiing or commenting on much of the information you were provided:

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1355210

Perl's 'find2perl' is designed to be a helper agent to translate shell 'find()' syntax into Perl.

That said, you could do this in the shell as:

# find /path -xdev -type f -nouser

Regards!

...JRF...

Robert Legatie
Advisor

Re: Perl and find command

I am using perl to expedite the find command. I know it can be done using shell and I am trying to use perl to compare each of them.

Thanks.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl and find command

Hi (again) Robert:

> I am using perl to expedite the find command. I know it can be done using shell and I am trying to use perl to compare each of them.

OK, then here's a cleaned up variation of what you posted:

# cat ./findnousers
#!/usr/bin/perl
use strict;
use warnings;
use File::Find ();

my @path = @ARGV ? @ARGV : ('.');
my ( %uid, $name, $pw, $uid );

sub wanted {
my ( $dev, $ino, $mode, $nlink, $uid, $gid );
( ( $dev, $ino, $mode, $nlink, $uid, $gid ) = lstat($_) )
&& !exists $uid{$uid}
and print "$File::Find::name\n";
}

while ( ( $name, $pw, $uid ) = getpwent ) {
$uid{$uid}++;
}

File::Find::find( { wanted => \&wanted }, @path );
exit;

...

You can run passing zero or more path arguments. If no arguments are given then the current working directory is assumed.

# ./findnousers /home

Regards!

...JRF...