View Full Version : CHMOD multiple files/directories at once.
SneakyDave
02-16-2001, 08:57 AM
Is there a way to chmod a list of directories and the contents of those directories all at once?
I have about 75 directories with an average of 20 files in them each that I need to CHMOD, and I really didn't want to have to go through each one.
Thanks in advance.
0degree
02-16-2001, 09:42 AM
One way of changing multiple file permissions is with CNC.[nbsp][nbsp]Just check all the files you want permissions changed for and then access the drop-down menu and select "Change File Permissions".
Tony.
JoeRT
02-16-2001, 10:33 AM
What FTP program are you using?[nbsp][nbsp]I use WS_FTP and it will do as many files as I want at once.[nbsp][nbsp]I'm told CuteFTP has the same option.
----------------
Joe Torsitano
www.weatherforyou.com (http://www.weatherforyou.com)
SneakyDave
02-16-2001, 10:54 AM
Well, in WS_FTP Pro (classic edition), I can select all the files in a directory, and CHMOD them, but I'll have to do that 75 times (once for each directory).
I was trying to think of a way that I could do it in telnet with one command.
Charles Capps
02-16-2001, 09:08 PM
chmod -R 644 dir dir dir...
-R being recursive.[nbsp][nbsp]It will change the permissions on the directories you tell it to, as well as all the files within, without the need to wait for WS_FTP to do its thing.[nbsp][nbsp]:)
SneakyDave
02-18-2001, 11:08 AM
That works great! Now another question.
I created a Perl script with over a thousand CHMOD statements in it, from using the ls command, and creating a text file, each statement looks like this:
CHMOD -R 644 /big/dom/xsitename/directory/dir1
CHMOD -R 644 /big/dom/xsitename/directory/dir2
CHMOD -R 644 /big/dom/xsitename/directory/dir3
CHMOD -R 644 /big/dom/xsitename/directory/dir4
For various reasons some of those directory names have an @ sign in them or a backslash space, i.c. "xsitename/directory/foo\ bar".
Those are probably invalid anyway, but my CHMOD command chokes on them.
I tried to escape the @ sign with \@, but it didn't like that.
How would I CHMOD those @ directories or backslash directories in a Perl script?
Justin
02-18-2001, 11:43 AM
You need to escape the @ symbol for Perl, and you will probably need to quote the pathname argument for the shell... however, having a Perl script with a buch of chmod commands shouldn't be necessary. Use opendir/readdir/closedir and iterate through the directories instead. Also note that Perl's chmod function takes the arguments slightly differently than the shell command, you'll want to reference the Perl docs for more specifics.
Also, always be sure to precede the octal value with a zero -- eg, use 'chmod 0644 file' rather than just 644, because in certain cases the interpretation will be different than expected. 0644 is the proper way to specify the permissions in octal format, and it's a good habit to get into (to avoid learning the hard way...)
You could probably do the entire process in one command in bash if you are creative -- something involving a 'for' loop, an 'ls -R' and 'chmod', all wrapped together nicely. Without rebooting into Linux I can't provide an example, but I know it can be done... ;)
------------------
Justin Nelson
SFE Software (http://www.sfesoftware.com)
mike_w
02-18-2001, 04:48 PM
If you have a text file listing the directories that you want to chmod then you can do something like the following with perl. Use single quotes to handle any odd named directories -
#!/usr/bin/perl
$directory_list = '/home/mike/directory_list.txt';[nbsp][nbsp]# change to path to your directory list
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]# each directory in list must be the
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]# absolute path for that directory
open(FILE, "< $directory_list") or die "Couldn't open $directory_list: $!\n";
while (<FILE>) {
[nbsp][nbsp][nbsp][nbsp]chomp;
[nbsp][nbsp][nbsp][nbsp]`chmod -R 755 '$_'`;
}
close(FILE);
If you need to go through directories recursively, for whatever reason, you can avoid opendir by using a sub like this -
sub recursive {
[nbsp][nbsp][nbsp][nbsp]your_function();[nbsp][nbsp] # do whatever you want to do here
[nbsp][nbsp][nbsp][nbsp]foreach $dir (<*>) {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]if (-d $dir) {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]chdir $dir;
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]recursive();
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]chdir "..";
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]}
[nbsp][nbsp][nbsp][nbsp]}
}
SneakyDave
02-18-2001, 05:38 PM
Thanks Mike, I actually used a script I wrote in PHP to do the same thing. I forgot that the PHP binaries were installed on the server! That would have saved me some time.
The only problem is with the "error" directories, things like /big/dom/xsitename/directories/Sneaky\ Dave
It still doesn't like that backslash, but I'm not going to spend much time on it, maybe I'll try to escape those backslases also.
SneakyDave
02-19-2001, 12:54 AM
Thanks Justin, I was going to try an opendir/readdir type of script, but I figured it was be just as quick as to: "ls -ad >/. directories.txt", and massage the list a little bit to include a CHMOD command.
I'd better go RTFM on the Perl documentation.
Tibbits
02-27-2001, 12:52 AM
Speaking as someone who just made a large part of my website non-world readable and stopped scripts working.. be careful. This goes doubly is there are any .htaccess files lurking around..
Tibbits
02-27-2001, 12:53 AM
eek, double post!
[This message has been edited by Tibbits (edited 02-26-01@1:18 pm)]
vBulletin® v3.6.8, Copyright ©2000-2013, Jelsoft Enterprises Ltd.