in reply to recursive dir and file

opendir only opens the directory for you, it does not change your current working directory. So you either need to do a chdir before doing any more tests on the files therein or prepend the directory name to the path.

Another possibility would be to use File::Find:

use File::Find; find (\&change_sas,"/cdw/home_dir/s006258/CSPAM"); sub change_sas { my $filename=$_; if (-f $filename && $filename=~m/\.sas\z/) { chmod 0644,$filename; print "Found $filename, changing\n"; } }

Or the *NIX find utility.

find /cdw/home_dir/s006258/CSPAM -name '*.sas' -exec chmod 644 {} \;

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

Replies are listed 'Best First'.
Re^2: recursive dir and file
by Aristotle (Chancellor) on Nov 13, 2005 at 00:05 UTC

    -exec should be avoided. It spawns one process per matched file. In almost every case, you want find -print0 | xargs -r0 instead, which will spawn only one process for as many files as will fit on its command line – usually, that means just one process.

    find /cdw/home_dir/s006258/CSPAM -name '*.sas' -print0 | xargs -r0 chm +od 644

    Makeshifts last the longest.

        Not only is it recent, it’s GNU find only. find -print0 | xargs -r0 works on any remotely recent Unixoid system.

        Makeshifts last the longest.