Change a file's permissions? : chmod « File « Perl






Change a file's permissions?

      
    #!/usr/local/bin/perl -w

    use Getopt::Long;

    my $ret = GetOptions ("f|filename:s", "p|permission:s");
    my $filename = $opt_f || die "Usage: $0 -f filename -p Permission\n";
    my $newPerm = $opt_p || die "Usage: $0 -f filename -p Permission\n";

    # Does the file exist?
    if (! -e $filename)
    {
       print "The file $filename does not exist.\n";
       exit;
    }

    # Translate the string mode to an octal value
    my $mode = oct($newPerm);

    # Change the permissions of the file.
    if ((chmod $mode, $filename) != 1)
    {
       print "Error: Could not change permissions on $filename : $!\n";
    }

   
    
    
    
    
    
  








Related examples in the same category

1.The chmod function modifies the read, write, and execute permissions on a file.
2.Apply chomp to all elements of an array
3.The chmod Function (UNIX)
4.The chmod Function (Windows)