develooper Front page | perl.beginners | Postings from February 2002

Re: How to copy and keep file permissions

Thread Previous | Thread Next
February 21, 2002 08:54
Re: How to copy and keep file permissions
Message ID:
>>>>> "Zentara" == Zentara  <> writes:

Zentara> On Wed, 20 Feb 2002 17:17:20 +0000, cemberly@UU.NET (Craig Eberly) wrote:
>> Hmm, I've tried this but it doesn't seem to change the file permissions at
>> all on the backup file.  I checked into it, and my stat command or  $mode in
>> this case is equal to 33261.   Is that a normal output for the mode in the
>> stat command?  I'm on a Solaris 8 system.

Zentara> The 33261 is file permissions and file type in decimal.
Zentara> Under linux, chmod accepts this value, maybe under solaris
Zentara> you have to to have the octal value?  

Zentara> Try this instead:

Operative word there is "try", followed by an overengineered solution.

You don't need to go from number to octal-string to number on this.
Just take the number from stat, hand it to chmod.  This works
everywhere (well, at least in Unix).

For example, clearing the x-bits on a file:

    my $mode = (stat($file))[2];
    $mode &= 07777; # mask off bits that aren't the mode (always do this)
    $mode &= ~0111; # mask off the x bits
    chmod $mode, $file or die;

Or setting the x-bits:

    my $mode = (stat($file))[2];
    $mode &= 07777; # mask off bits that aren't the mode (always do this)
    $mode |= 0111; # set the x-bits
    chmod $mode, $file or die;

No conversion to or from "octal" required.  It's a number already.

Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<> <URL:>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See for onsite and open-enrollment Perl training!

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About