$ for i in a b c; do cd $i; echo "DIR: `pwd`"; ls -l; cd ..; done
DIR: /home/ken/tmp/pm_11135362/a
total 1
-rw-r--r-- 1 ken None 0 Jul 25 16:44 empty
---------- 1 ken None 18 Jul 25 16:45 no_access
DIR: /home/ken/tmp/pm_11135362/b
total 1
-r--r--r-- 1 ken None 20 Jul 25 16:49 read_only
DIR: /home/ken/tmp/pm_11135362/c
total 1
-rw-r--r-- 1 ken None 7 Jul 25 16:51 read_write
####
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd;
use File::Find;
my $cwd = getcwd();
my @dirs = map "$cwd/$_", qw{a b c};
print "--- READING ---\n";
find(\&wanted_to_read, @dirs);
print "--- WRITING ---\n";
find(\&wanted_to_write, @dirs);
sub wanted_to_read {
if (! -f $File::Find::name) {
print "$File::Find::name is not a normal file.\n";
}
elsif (-z _) {
print "$File::Find::name is zero-length.\n";
}
elsif (! -r _) {
print "$File::Find::name is not readable.\n";
}
else {
print "OK to READ: $File::Find::name\n";
}
return;
}
sub wanted_to_write {
if (! -f $File::Find::name) {
print "$File::Find::name is not a normal file.\n";
}
elsif (! -r _) {
print "$File::Find::name is not readable.\n";
}
elsif (! -w _) {
print "$File::Find::name is not writable.\n";
}
else {
print "OK to WRITE: $File::Find::name\n";
}
return;
}
####
ken@titan ~/tmp/pm_11135362
$ ./pm_11135362_file_find_example.pl
--- READING ---
/home/ken/tmp/pm_11135362/a is not a normal file.
/home/ken/tmp/pm_11135362/a/empty is zero-length.
/home/ken/tmp/pm_11135362/a/no_access is not readable.
/home/ken/tmp/pm_11135362/b is not a normal file.
OK to READ: /home/ken/tmp/pm_11135362/b/read_only
/home/ken/tmp/pm_11135362/c is not a normal file.
OK to READ: /home/ken/tmp/pm_11135362/c/read_write
--- WRITING ---
/home/ken/tmp/pm_11135362/a is not a normal file.
OK to WRITE: /home/ken/tmp/pm_11135362/a/empty
/home/ken/tmp/pm_11135362/a/no_access is not readable.
/home/ken/tmp/pm_11135362/b is not a normal file.
/home/ken/tmp/pm_11135362/b/read_only is not writable.
/home/ken/tmp/pm_11135362/c is not a normal file.
OK to WRITE: /home/ken/tmp/pm_11135362/c/read_write