Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Chmod function in Perl

Status
Not open for further replies.

Ramnarayan

Programmer
Jan 15, 2003
56
US
Hi,

I need to change the permissions of all the files and folders in a directory recursively. Particularly I don't want to use !system("chmod")

$batch = "003"
chmod (0777, '$batch');

Also, I am not able to figure how I can change the permissions of files and directories recursively and separetely.

All files in one directory has to have a permission of 644 and directories to have 755. Can some one help me out with a script so that I can know better.

Thanks a lot once again

 
Take a look at this thread: thread219-447155 was for recursively copying a directory structure of files, but instead of creating new directories and copying files, you could run chmod() in each case. Shouldn't take much to alter it. Let me know what you think. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Hi ICRF,

However there is a small problem. Your script was able to work for the below pattern of directories:

opendir DIR, 190 or die "Unable to open source directory '190' for reading: $!";
my @dirList = readdir DIR;
print @dirList;
closedir DIR

This is working for the below directory structure

190/aa
190/ab
190/ac
190/ad

But this is not working for further subdirectories under aa, ab, ac, and also files inside further subdirectories like below:

190/aa/1902/aaa/*.tif
190/ab/1903/aab/*.tif
190/ac/1904/aac/*.tif
190/ad/1905/aad/*.tif


Please let me know what should be done!
 
It recursively calls itself at the beginning of the last foreach block if it finds the current file is a directory. It should traverse through as deep a directory structure as you can make.

Try this, slightly altered:
Code:
sub recursiChmod
{
	my ($source, $filePermissions, $directoryPermissions) = @_;
	
	print "$source\n";
	
	# if not a directory, chmod like a file
	if(! -d $source)
	{
		die "Errors chmoding file '$source': $!" unless chmod($filePermissions, $source);
		return;
	}
	
	# if source is not a file, chmod it like a directory
	chmod($directoryPermissions, $source) or die "Errors chmoding directory '$source': $!";
	
	# open the source and read the list
	opendir DIR, $source or die "Unable to open source directory '$source' for reading: $!";
	my @dirList = readdir DIR;
	closedir DIR;
	
	# the first two entries in the list are going to be '.' and '..' for current and parent directory
	@dirList = @dirList[2..$#dirList];
	
	foreach my $entry (@dirList)
	{
		# if the entry is a directory, call the recusiCopy again with updated source and destination
		&recursiChmod("$source/$entry", $filePermissions, $directoryPermissions) if(-d "$source/$entry");
		
		# if the entry is a file, attempt to chmod it, or die trying
		chmod($filePermissions, "$source/$entry") or die "Errors chmoding file '$source/$entry': $!" if(! -d "$source/$entry");
	}
}
I haven't been able to test it at all, since chmod's functionality in Win2k isn't that grand. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Thanks a million ICRF.. It worked like magic. thank you somuch once again.
 
Glad to help, that's what we're here for. Pass on the magic. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top