Help - Search - Members - Calendar
Full Version: Shell script question
Hydrogenaudio Forums > Misc. > Off-Topic
Nick E
When I need to move directories of files between different machines on a USB drive I have it formatted in FAT 32. So, of course, permissions aren't preserved. IIRC, you end up with all directories set to 777 and all files to 700.

I'd prefer to re-set the directories to 755 and the files to 644. I don't know that it really matters, but I prefer to, and it's a pain on an item-by-item basis. Of course, with a hierarchy you could recursively re-set everything to 755 from the top directory. But then how would you unset the executable bit on the files while leaving the directories alone?

I can understand that you must be able to select the files, because you can get them by extension, e.g.:

CODE
find . -name "*.ogg" -print


But how can you chmod them?

It seems to me this must be a simple enough thing to do, but I know an embarrassingly small amount about bash, so would someone be kind enough to put me right on this, please?
Yirkha
`find` has options like `-type f(ile)` or `-type d(irectory)`, but you won't need that.

`chmod -R a+rwX .` is what you want:
-R, --recursive change files and directories recursively
a (all users) is effectively user + group + others
r (read),
w (write),
X (execute only if the file is a directory or already has execute permission for some user)
Nick E
QUOTE(Yirkha @ Mar 20 2008, 04:05) *

`find` has options like `-type f(ile)` or `-type d(irectory)`, but you won't need that.

`chmod -R a+rwX .` is what you want:
-R, --recursive change files and directories recursively
a (all users) is effectively user + group + others
r (read),
w (write),
X (execute only if the file is a directory or already has execute permission for some user)


Thank you.

EDIT:

Darn it. I've just checked: on my Linux box files taken off a FAT32 volume do get set to 700. However, it looks like the Mac sets everything, files included, to 777 in the same situation ... at least it did just now.

So how would I get the executable bit off the files? (Besides cd-ing into each bottom-level directory and doing chmod 644 * that is. Incidentally -- thank you, Apple -- unlike with Nautilus it literally can't be done in the GUI with a Mac.)
Yirkha
QUOTE(Nick E @ Mar 20 2008, 11:33) *
So how would I get the executable bit off the files?

By something like `find . -type f | xargs chmod -x`, perhaps.
Nick E
QUOTE(Yirkha @ Mar 20 2008, 07:55) *

QUOTE(Nick E @ Mar 20 2008, 11:33) *
So how would I get the executable bit off the files?

By something like `find . -type f | xargs chmod -x`, perhaps.


Ah, that's great. So xargs takes a stream of filenames from find and uses them into calls to another program. And, apparently, with a -0 flag it can cope with whitespace, as well.

Thanks.
Yirkha
It's great to see that someone even reads the available documentation and plays with things himself.

There is actually also the method of writing it like this: `find (...) -exec chmod -x {} \;`, which might look more straightforward, but this way the chmod utility is launched for each filename separately. Xargs fills the arguments up to the command-line length limit each time and invokes the subprocess in chunks, so it has a performance gain.

And yes, xargs only concatenates each line of the input, so filenames with newlines, spaces, quotes and similar characters can break it. The `find (...) -print0 | xargs -0 (...)` method works, but be warned that it's not supported in all UNIX-compatible environments.
Nick E
Thanks again.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.