View Full Version : how to chmod all files in folder at once?
jdparsons
02-15-2002, 07:39 PM
I am trying to install ikonboard, but it seems that I have to set the permissions for all 5000 files individually after uploading. I tried to set the ftp upload permissions to 755, but I get a "server unmask command not understood" from the server.
So, I then wrote a perl script to chmod all the files in the folder at once:
#!/usr/bin/perl
$permissionNum = "0755";
$dirtoopen = "../board/";
opendir (DIR, "$dirtoopen") or die "This is not the correct directory";
@dirdata = readdir(DIR);
closedir (DIR);
$changed = chmod 0755, @dirdata;
print "Content-type: text/html\n\n";
print "$changed files changed to $permissionNum";
I get "0 files changed to 0755" every time.
I want a way to chmod all the files in a folder at once.
Aragorn450
02-15-2002, 08:15 PM
Well, first of all... I'd sugest printing out that list of entries to make sure that they are all there... (which I'm guessing you already did...).
Secondly, I'd make sure you only get entries that do not start with a '.' So.. I'd suggest changing your readdir line to:
@dirdata = grep !/^\./, readdir DIR;
Secondly, I might try dong your failure as follows so that you can see each individual file that fails (which might be a lot, might not... dunno :))
@cannot = grep {not chmod 0755, $_} @dirdata;
die "$0: could not chmod @cannot\n" if @cannot;
But... there's nothing obviously wront with your script (however, I've never tried that myself so there might be something I'm missing... lol).
Charlie
jdparsons
02-16-2002, 09:35 PM
I made progress today with the script, but two problems keep it from working the way I want it to:
1. The chmod number in the script chmods the files to a (seemingly) random permission. For example: chmod to 0111 in the script translated into a file with 157 permissions. 0222 went to 339. 0333 went to 515. After about an hour of guess and check, I found that chmod-ing to 0511 in the script made all the files 777. This is only a problem if I want to publically distribute this script, though.
2. The script can only chmod files in the same folder that the script resides in. I have a for-loop that searches for directories, opens them and tries to chmod all the files in the directory, but those files always come back in the @cannot array...
Despite these difficulties, running this script in every directory is quicker than chmod-ing every file individually. Still, for future use with large packages of scripts, I would like to work these problems out. Thanks for the help.
Aragorn450
02-16-2002, 09:57 PM
I made progress today with the script, but two problems keep it from working the way I want it to:
1. The chmod number in the script chmods the files to a (seemingly) random permission. For example: chmod to 0111 in the script translated into a file with 157 permissions. 0222 went to 339. 0333 went to 515. After about an hour of guess and check, I found that chmod-ing to 0511 in the script made all the files 777. This is only a problem if I want to publically distribute this script, though.
Yea... I'm wondering if maybe you should remove the 0, so in other words make it 777 instead of 0777. That might fix that problem.
2. The script can only chmod files in the same folder that the script resides in. I have a for-loop that searches for directories, opens them and tries to chmod all the files in the directory, but those files always come back in the @cannot array...
Heh, yep... that's because the current directory is still where you are running the script from. So you'll need to either CD to the subdirectory, or keep track of the full path from where you are. Also, instead of a for loop, I might suggest a function that gets called recursively (in other words it calls itself over and over again, taking a path to get the files from as a parameter). I'd send you an example, but... I can't connect to my work computer right now to get access to something similar I did a while ago... sorry ;)
Hope that helps.
Charlie
jdparsons
02-17-2002, 03:08 AM
I found out what was causing the trouble with issue #1. I had $permissionNum = "0777"; I changed it to $permissionNum = 0777; and it worked fine.
As for issue #2, I tried that chdir trick and it also worked, but the permissions were set only for one sub-folder. I realize that switching to the recursive function you suggest will probably fix this, but as you can see by the time stamp of this post, it must wait until morning (actually, later today)... ;)
Thanks again for all your help! This is a great way to learn perl outside of a classroom...
Could someone help me in installing ikonboard? How do you setup the config? Thanks :D
vBulletin® v3.7.0, Copyright ©2000-2010, Jelsoft Enterprises Ltd.