views:

797

answers:

4

Can someone please give me a recursive command that will go through a directory and make all normal files permission 644 and all sub directories 755?

I am really getting tired of doing this manually every time I have to install something on my host. I don't know enough BASH (Shell?) command to do this.

+4  A: 

First line changes file permissions, and the second changes directory permissions in the active directory and its subdirectories.

find . -type f -print0 | xargs -0 chmod 644
find . -type d -print0 | xargs -0 chmod 755
Kaarel
Simple and direct. Well done.
Jon Ericson
Or at least it was before the `-print0` and `-0` business. People always recommend that and I know why, but it annoys me.
Jon Ericson
Awesome, worked like a treat. You have saved me time and boredom beyond words!! :P
Abs
+8  A: 

There is X option for that.

chmod a+X * -R

This will give execute bit only to directories, not files. To set 644, 755, respectively with one command, use:

chmod a=rX,u+w <files/dirs> -R
ypnos
You learn something new every day. Useful!
Jon Ericson
+2  A: 

Using symbolic mode names instead of raw numeric permissions:

chmod -R u=rwX,go=rX somedir

The X permission flag only sets directories or already executable files as executable, the -R flag means "recursive" and applies the permissions to all the contents of the somedir.

sth
A: 

No, there is no command to recursively change the permissions. If there were such a command, it would violate the Unix mantra: Do One Thing And Do It Well.

However, there are two commands: one for recursing (find), and one for changing permissions (chmod).

So, the magic command line is:

find . -type d -exec chmod 0755 '{}' + -or -type f -exec chmod 0644 '{}' +
Jörg W Mittag
Wrong. And what about reading other answers first?
ypnos
What is wrong? Did you even *try* that command? It works on every single machine I worked on, it is compliant with POSIX, it is compliant with the Single Unix Specification. Unlike all the other answers which rely on non-standard proprietary extensions that are not available on many platforms ...
Jörg W Mittag
... or they don't solve the problem that the OP states.
Jörg W Mittag
Wrong: to say "there is no command" because on GNU systems, there is. OP asked for "Bash" which is also GNU. My main concern however was that you didn't refer to others while indirectly calling them wrong. Would've been more helpful to explain the POSIX matter so OP understands how it affects him.
ypnos