jgiller has asked for the wisdom of the Perl Monks concerning the following question:

I am sure you've helped someone on this one before...

I have a directory of directories that contain files. I need to change the permissions on any files encountered in this filesystem starting from the top but leave the directories alone. The part I am unclear on is how to avoid directories and most importantly how to drill to the last level in a directory that might have multiple subdirs.

Thanks!

Joe

GrandFather changed title from "Easy one probably" to "Changing file permissions recursively" and added paragraph tags

Replies are listed 'Best First'.
Re: Changing file permissions recursively
by Roy Johnson (Monsignor) on Oct 24, 2005 at 21:25 UTC
    In Unix:
    find . -type f -print | xargs chmod 700
    In Perl, you're looking for the file test operator -f. The file test operators are documented with their related function, stat.

    Caution: Contents may have been coded under pressure.
      Golf?
      find . -type f -exec chmod 700 {}+ \;


      Evan Carroll
      www.EvanCarroll.com
Re: Changing file permissions recursively
by mulander (Monk) on Oct 24, 2005 at 21:29 UTC
    If you want to do it in Perl then I'm pretty sure that you will be glad to know that File::Find is here ;)
    check perldoc File::Find
    NAME File::Find - Traverse a directory tree. SYNOPSIS use File::Find; find(\&wanted, @directories_to_search); sub wanted { ... } use File::Find; finddepth(\&wanted, @directories_to_search); sub wanted { ... } use File::Find; find({ wanted => \&process, follow => 1 }, '.');
    The rest is in the perldoc ;)
Re: Changing file permissions recursively
by sh1tn (Priest) on Oct 24, 2005 at 21:57 UTC
    UNIX style:
    perl -MFile::Find -e 'find(sub{ -f and do_something_with($File::Find::name)}, ".")'
    DOS style:
    perl -MFile::Find -e "find(sub{ -f and do_something_with($File::Find::name)}, '.')"


Re: Changing file permissions recursively
by EvanCarroll (Chaplain) on Oct 24, 2005 at 21:30 UTC
    Use File::Find, and ! -d


    Evan Carroll
    www.EvanCarroll.com