Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

chmod 775 on all files

by Anonymous Monk
on Jul 29, 2003 at 14:57 UTC ( #278852=perlquestion: print w/replies, xml ) Need Help??

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

Anyway using File::Find to make all my directories and files writable and readable in my one directory on a unix workstation. Basically make everything 'chmod 775'. I have this one big directory (with many subdirectories) that has about 500 megabytes of data and need all files and directories writable.

Replies are listed 'Best First'.
Re: chmod 775 on all files
by shemp (Deacon) on Jul 29, 2003 at 15:00 UTC
    This answer doesnt use File::Find, but from a unix command prompt, you can issue:
    chmod -R 775 *
    This will recursively chmod everything you have permission to change. So from Perl, you could do that as a system command.
Re: chmod 775 on all files
by Abigail-II (Bishop) on Jul 29, 2003 at 15:07 UTC
    chmod 775 isn't quite making everything readable and writeable. It makes all files and directories readable and executable for everyone, and writeable for the owner and anyone who is in the same group as the group owing a file.

    Anyway, I wouldn't use a Perl solution for this problem - I'd use the -R option of chmod.


Re: chmod 775 on all files
by mbadolato (Hermit) on Jul 29, 2003 at 15:15 UTC
    I agree with the others that the simple chmod -R 775 from the command line would be easiest. But, if for some reason you need to use File::Find, I believe (I didn't refer back to the docs, or test this) the syntax is something to the effect of

    find({chmod(0775, $_)}, ['/path/to/your/base/dir']);

    I haven't used File::Find for a looooong time so I could be off with that. starting point though? :) YMMV

Re: chmod 775 on all files
by MidLifeXis (Monsignor) on Jul 29, 2003 at 17:05 UTC

    Or if you just want to turn on the write bit for user and group, use the symbolics...

    chmod -R ug+w .
      chmod -R ug+w .

      Actually, the effect of this command is filtered by the current umask setting, while chmod -R 775 . wouldn't


      Update: After Abigail-II's reply I double checked the man page, and he is actually right:

      A combination of the letters `ugoa' controls which users' access to the file will be changed: the user who owns it (u), other users in the file's group (g), other users not in the file's group (o), or all users (a). If none of these are given, the effect is as if `a' were given, but bits that are set in the umask are not affected.

      So, for example, chmod +rx and chmod a+rx behave differently, depending on the umask.

      What remains from what I wrote above and from Abigail's reply is that you should those ugly numerical file modes if you want to run on the safe side :-)

      The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
      --John M. Dlugosz
        Whether or not it's filtered by the current umask setting depends on the implementation of chmod. It certainly doesn't look at umask on the Redhat Linux I just tried it on.

        It does, however, pay attention to umask if I don't put a modifier before the +.

        A few years ago, during the PPT project, I implemented chmod in Perl. I looked at 5 different implementations (HP-UX, SunOS, Solaris, OpenBSD and GNU), and none of them behaved the same as any other implementation. None of the implementations acted as documented either. Of course, they all did the same for simple things, but they started deviating when giving more estoric parameters. Quick, what does your chmod do when doing:

        chmod =,o+s file_or_directory


Re: chmod 775 on all files
by sgifford (Prior) on Jul 29, 2003 at 18:17 UTC
    775 is "readable, writable, executable" for user and group, and "readable, executable" for other. Do you really want every single file to be executable?

    If you just want to make executable things that are directories or are already executable for somebody, something like:

    chmod -R u=rwX,g=rwX,o=rX
    will work.

    All of the solutions suggested so far have been to use the command-line chmod tool, because that's by far the easiest way to do it. If there's some reason you want to do this with Perl instead, let us know and somebody will be able to help you with that, too.

      chmod -R a=rX,ug+w even.

      The logic of the X chmod flag can easily be implemented using File::Find:

      use File::Find; find(sub { my ($mode) = (stat)[2] & 0777; # see perldoc -f stat my $new_mode = 0664; if ( -d _ or ($mode | 0111) ) { $new_mode |= 0111; } if ($new_mode != $mode) { chmod($new_mode, $_) or warn("Failed to set permissions on " ."$File::Find::name; $!"); } }, "dir");
      $h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";
Beware of 7 on dirs! [Re: chmod 775 on all files]
by bronto (Priest) on Jul 30, 2003 at 10:57 UTC

    Beware that giving 775 to a directory could allow members of the group owner to add and delete files into it! You don't seem to care, since you give full access to your files by the group owner (why?), but I think it's not advisable.

    I'd better use find than File::Find, and do at least a:

    find /home/directory -type f -print | xargs chmod 775 find /home/directory -type d -print | xargs chmod 755

    This way group owner's people can still have full access to files, but, for example, can't add files in a directory of yours, which is preferrable IMHO


    The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
    --John M. Dlugosz
Re: chmod 775 on all files
by Skeeve (Parson) on Jul 30, 2003 at 05:22 UTC
    As others said before 775 is also executable. If I want to make everything in my dir "rw" for me and my group and "r" for everyone else and all subdirectories "x" I don't use perl but 2 chmod-s with find and xargs:
    find /my/dir -type d | xargs chmod 775 find /my/dir -type f | xargs chmod 664
    Update: changed type 7 to type f

      I am always learning, but unless I am mistaken, I think the second find should be...

      find /my/dir -type f | xargs chmod 664

      Update:Removed strikeout on '7' from previous code to make slightly clearer. (cosmetic)

      Update:s/644/664/ - Quite right Skeeve. Typo.

        if you meant the change from 7 to f, you're right
        if you meant 644 instead of 664, you're wrong ;-)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://278852]
Approved by Corion
Front-paged by dragonchild
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2023-09-30 02:57 GMT
Find Nodes?
    Voting Booth?

    No recent polls found