Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Setting file permissions on multiple files UNIX
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!
  • Currently 4.17 / 5
  You rated: 4 / 5 (6 votes cast)

Setting file permissions on multiple files | 5 comments | Create New Account
Click here to return to the 'Setting file permissions on multiple files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Easier still...
Authored by: sharumpe on Mar 11, '01 04:14:45PM

This is kinda over-engineered for this particular example.

Try this instead:

chmod -R 755 *

This will give the owner rwx and the group and world r-x in on every file in the current directory and all subdirectories.

If the command you are using doesn't support the -R (for recursive) flag (many don't). then you can also do this:

cat `find . -type f`

Backticks (in tcsh, at least) execute the enclosed command. This example will run the find command, which returns the names of all 'normal' files in the current subdirectory and all it's children, and gives those filenames to cat, which will print the contents out (this is a silly example, but does the trick).

In any case, for chmod (and chown) the -R command will do the trick with less keystrokes.

Mr. Sharumpe

[ Reply to This | # ]
Usless use of cat . . .
Authored by: Anonymous on Mar 13, '01 07:04:59PM

As you become more experienced with UNIX you
will (perhaps) hear of 'the useless use of cat' argument.
The previous was a good example of it. Why not just:

% chmod -R 777 .
# chmod EVERYTHING, with recursion, to 777 from the current location, then
% find . -type f -exec chmod 666 {} \\;
# find all FILES chmod to 666 [ the '\\;' is required, read about it with 'man find']
# foreach file find puts the filename where the '{}' curly braces are and executes the
# command. Very elegant and powerful. BTW: it took 4 slashes in this editor to print a
# single backslash like this '\'

[ Reply to This | # ]
chmod -R a+rwX
Authored by: Anonymous on Mar 14, '01 09:25:40AM

chmod -R a+rwX

-R Recursive. Change subdirectories and files in those subdirectories.
a Give these permissions to All users (as opposed to u=yourself, g=your group, o=others, everyone else)
+ Grant the following permissions (- to take permissions away)
r Read permission
w Write permission
X Permission to eXecute a program or open a directory, if one user already has this permission. (Lowercase x sets execute permission no matter what)

So regular files get rw permission for everyone (aka 666), and directories, which are presumably set +x for the person that created them, are set rwx for everyone (aka 777).

[ Reply to This | # ]
Another way to execute files found by find
Authored by: Anonymous on Apr 05, '01 02:33:11AM

You can also use the -exec switch in find. For example, to turn off the write bit
on all your *.jpg files:

find . -name "*.jpg" -exec chmod -w {} \; (this should end in a forward-slash-semicolon)

Another example - search source code for your username

find . -name "*.c" -exec grep `whoami` {} \; (this should end in a forward-slash-semicolon)

[ Reply to This | # ]
Setting file permissions on multiple files
Authored by: guspos on May 05, '10 01:56:16AM

Thank you robg!
an optimized version would be:

find . -type d -print0 | xargs -0 chmod 755
find . -type f -print0 | xargs -0 chmod 644

This works also if you have files or directories with spaces.

[ Reply to This | # ]