Kyle <k...@attitia.com> writes:

> I'm having a bit of grief with chmod and am hoping one of you gurus
> will set me straight pls.

You have a problem with argument globbing on Unix, not chmod, which
might explain why you are having trouble finding out what is going
wrong.

> I have a bunch of directories with a bunch of files (pictures) in
> each. I want to set directories to 775 and files to 664.
>
> I can do a chmod -R 775 *. But then if I do a chmod -R 664 *.jpg (and
> repeat for all other extensions), for some reason the chmod doesn't
> work.

Sure it does, but what actually happens is:

1. You enter 'chmod -R 644 *.jpg' into the shell.
2. The shell expands the '*.jpg' part into a list of files matching that
   pattern (implicitly in the current directory.)
3. The shell runs 'chmod -R 644 example1.jpg example2.jpg etc.jpg'
4. chmod recurses if any of the arguments are a directory, which none of
   them are because only *.jpg files were matched.

So, everything works as designed, but '-R' doesn't do quite what you
thought, and neither does the '*.jpg' argument.

Also, if you quote the glob you *still* don't get what you want, because
chmod (like almost all Unix commands) doesn't do internal globbing, it
expects external globbing, so you would get:

  ] chmod -R 644 '*.jpg'
  chmod: cannot access `*.jpg': No such file or directory

[...]

> What am I missing?

find(1), which is used to locate a list of files matching a given set of
criteria, allowing you to do something like this:

  chmod -R 644 `find -name '*.jpg'`

(Note the single-quotes around the glob pattern?  Without that the shell
 would expand the pattern, which would cause a syntax error for the find
 command, and not do what you want.)

There is a limit to the number of arguments you can pass to chmod,
though, so it is generally speaking better to structure that like this:

  find -name '*.jpg' | xargs chmod -R 644

That falls apart if any of your filenames have spaces in them, though,
since xargs splits on *any* whitespace; to work around that use:

  find -name '*.jpg' -print0 | xargs -0 chmod -R 644

See the manual pages for the fine detail, obviously.

Regards,
        Daniel
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to