Setting file permissions on multiple files

Mar 07, '01 12:23:33PM

Contributed by: robg

Last night, as I began experimenting with possible successors to geeklog, one package (phpnuke) had an instruction step that read:

"Set all files to 666 permission; set all directories to 777." Although this is relatively trivial for multiple identical items (chmod 777 *), it's a bit trickier when files and directories are mixed in the structure, with sub-directories and sub-files, and different settings for directories and files. The phpnuke package installs literally hundreds of files, and probably 25-50 subdirectories. I was reduced to mass changing everything in a folder (chmod 666 *), and then setting the directories by hand. It took a while!

I was sure there was an easier way, but had no idea what that way might be. A post to the MacNN forums provided the answer, courtesy of "Icampbell":

find . -type f | xargs chmod 666
find . -type d | xargs chmod 777
This does exactly what I needed it to do. 'xargs' is an interesting command, and well worth reading up on ('man xargs'). It basically executes the command specified (chmod in this case) for each item passed to it (the results of the 'find' command, routed via the pipe '|' symbol). It's fairly easy to see how powerful xargs can be, given its ability to act on a series of things passed to it. One note of caution in this example - the 'find' command will search down through the directory structure from where you start, so make sure you want to effect EVERYTHING in that path if you try something along these lines!

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20010307122333315