Current location: Hot Scripts Forums » Programming Languages » Perl » Change file owner and/or permission with perl


Change file owner and/or permission with perl

Reply
  #1 (permalink)  
Old 10-01-06, 11:42 AM
SISCharlie SISCharlie is offline
New Member
 
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Change file owner and/or permission with perl

I need perl code to change the permission of files that I ftp to a vps server. I'm moving from a shared server which didn't seem to care about file ownership or file permissions to a vps server which does. I transfer too many files too ofter to make changes manually so I would like to have a set of instructions to at least change the permissions that I can use when needed.

I've tried the below code without any success:

Code:
  my $address_of_file_in           =  WWWHOME . '/w1/';
  print "<br>88772 address_of_file_in = $address_of_file_in\n";

   my $comments_dir = $address_of_file_in;
   my $mode = oct("0666");
   chdir $comments_dir or die "Cannot change to $comments_dir. $!"; 
   my @files=glob("w6*.htm"); 

   for (<@files>) { 

     print "<br>$_\n";
#     chmod 0666, $_; 
     CORE::chmod($mode, $_); 
   }
The files are printed but permissions aren't changed.

Can anyone help?
Thanks
Charlie

Last edited by nico_swd; 10-01-06 at 12:01 PM.
Reply With Quote
  #2 (permalink)  
Old 10-01-06, 12:20 PM
mickalo's Avatar
mickalo mickalo is offline
Newbie Coder
 
Join Date: Apr 2005
Location: N.W. Iowa
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by SISCharlie
I need perl code to change the permission of files that I ftp to a vps server. I'm moving from a shared server which didn't seem to care about file ownership or file permissions to a vps server which does. I transfer too many files too ofter to make changes manually so I would like to have a set of instructions to at least change the permissions that I can use when needed.

I've tried the below code without any success:

Code:
  my $address_of_file_in           =  WWWHOME . '/w1/';
  print "<br>88772 address_of_file_in = $address_of_file_in\n";

   my $comments_dir = $address_of_file_in;
   my $mode = oct("0666");
   chdir $comments_dir or die "Cannot change to $comments_dir. $!"; 
   my @files=glob("w6*.htm"); 

   for (<@files>) { 

     print "<br>$_\n";
#     chmod 0666, $_; 
     CORE::chmod($mode, $_); 
   }
The files are printed but permissions aren't changed.

Can anyone help?
Thanks
Charlie
is the script attempting to change the file settings owned by the same user who own the files?

in order to change the chmod setting, you must 1) provide the FULL path to the file, and 2) the script attempting to change the setting must be owned by the same userid as the files in questions.

Mickalo
__________________
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing
Providing Internet Solutions that work!
Custom Perl Programming & MySQL designs
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Reply With Quote
  #3 (permalink)  
Old 10-01-06, 12:35 PM
SISCharlie SISCharlie is offline
New Member
 
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
The owner of the files to change is "charlie"
the script is run by user "www"

Can the user be changed from "www" to "charlie"
Reply With Quote
  #4 (permalink)  
Old 10-01-06, 12:41 PM
mickalo's Avatar
mickalo mickalo is offline
Newbie Coder
 
Join Date: Apr 2005
Location: N.W. Iowa
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by SISCharlie
The owner of the files to change is "charlie"
the script is run by user "www"

Can the user be changed from "www" to "charlie"
apparently your scripts are running as nobody ... instead of the user/group assigned. Do you have suexec enable or compiled with Perl?

you could trying changing the owner/group of script in question and try it. When you do change the chmod setting, besure to include the full path to the file, IE: chmod 0666, "full_path_to_file";

Mickalo
__________________
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing
Providing Internet Solutions that work!
Custom Perl Programming & MySQL designs
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Reply With Quote
  #5 (permalink)  
Old 10-01-06, 01:24 PM
Drunken Perl Coder Drunken Perl Coder is offline
Wannabe Coder
 
Join Date: Aug 2006
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
Mickalo,

he is using chdir() to change to the directory where the files are so he should not need to use the full path.


SISCharlie,

try like this:

Code:
#!/usr/bin/perl -w

my $address_of_file_in  =  '/home/user/w1/';
print "Content-type: text/html\n\n";
print "<br>88772 address_of_file_in = $address_of_file_in\n";
my $mode = 0644;
chdir($address_of_file_in) or die "Cannot change to $address_of_file_in. $!"; 
my @files = <w6*.htm>; 
for (@files) { 
   print "<br>$_\n";
   chmod $mode, $_ or  print "Can't chmod $_: $!";
}
change the value of $address_of_file_in to your directory. There should be no reason to use 0666 for your html files. 0644 should work and is safer.
Reply With Quote
  #6 (permalink)  
Old 10-01-06, 01:43 PM
mickalo's Avatar
mickalo mickalo is offline
Newbie Coder
 
Join Date: Apr 2005
Location: N.W. Iowa
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, didn't notice the chdir() function.

Mickalo
__________________
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing
Providing Internet Solutions that work!
Custom Perl Programming & MySQL designs
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Reply With Quote
  #7 (permalink)  
Old 10-01-06, 07:02 PM
SISCharlie SISCharlie is offline
New Member
 
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
The entire program is included below followed by the result of the run of the code.
Notice the user is "www" - yet the file is owned by "charlie"

The reason I want to change the file to 666 is so that it can be deleted or updated by a perl script.




Code:
#!/usr/bin/perl -w
   print "Content-Type:text/plain\n\n";
   print "Start of test05\n";

   print "I am running as UID/user: ", (getpwuid($<))[0], "\n"; 


   use constant WWWHOME              =>  "$ENV{DOCUMENT_ROOT}";

   my $address_of_file_in           =  WWWHOME . '/w1/';

   print "<br>88772 address_of_file_in = $address_of_file_in\n";
   my $comments_dir = $address_of_file_in;
   my $mode = oct("0666");
   chdir $comments_dir or die "Cannot change to $comments_dir. $!"; 
   my @files = glob("w6100*.htm");
   for (<@files>) { 

     print "<br>$_\n";
     CORE::chmod($mode, $_); 
   } 

   print "<br>8888 End of test05.pl\n";
------------------------------------------------------------------------

The output of this code follows:

Start of test05
I am running as UID/user: www
<br>88772 address_of_file_in = /usr/local/apache/htdocs/w1/
<br>w61000.htm
<br>8888 End of test05.pl

Last edited by nico_swd; 10-02-06 at 02:51 AM.
Reply With Quote
  #8 (permalink)  
Old 10-01-06, 07:04 PM
SISCharlie SISCharlie is offline
New Member
 
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Do you have suexec enable or compiled with Perl?

What is suexec?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with my largest perl script. Grabbing data from a file. Sunnmann Perl 2 04-23-08 03:27 AM
Perl Script to parse 1 xml file into many flsh Perl 0 08-09-04 10:21 AM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 04:28 PM
Script to change file extensions JavaHead Script Requests 2 09-13-03 09:44 AM
Question with chown - Change file owner ! Help pls kevin PHP 2 07-09-03 04:36 AM


All times are GMT -5. The time now is 04:45 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.