Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was trying to chmod folders and files with:

find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;

But I was wondering:

  1. How to do it in one line using find and excluding the directories . and ..

  2. Which method is better, find -exec or xargs?

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Sherlock got (1), for (2) xargs is generally preferable as it allows for more reasonable syntax and less shell invocation overhead. Be aware of the -print0 and -0 options to find and xargs respectively as paths with spaces and special characters can gum up the works.

For a one-time run, the difference really shouldn't matter unless you are chmodding a huge tree. If you do this often enough to formalize into a script, I'd use xargs.

share|improve this answer
add comment

Is using find or xargs mandatory?

If not, you can use:

chmod -R u=rwX,go=rX *
share|improve this answer
add comment

You can do this with the named arguments to chmod:

 chmod u=rwX,go=rX {}

The capital X is the key part here which changes the permission to executable only if it is a directory. See the manual page for more details.

share|improve this answer
add comment
find . -type d -exec chmod 755 {} + -or -type f -exec chmod 644 {} +

find ... -exec ... + is like find ... -exec ... \; except that the command is executed only once per large set of matching files.

Once upon a time, find OPTIONS... -exec COMMAND... \; had to act on one file at a time. So xargs was invented to group actions for efficiency. But xargs introduced its own share of trouble: it expects input that is quoted in a way that find cannot produce. So find OPTIONS... | xargs COMMAND... is no good unless you know that your file names do not contain any of '"\ or whitespace.

Then GNU invented find OPTIONS... -print0 | xargs -0 COMMAND..., which allows any character to appear in a file name. But it took a long time for anyone else to adopt it, and in the meantime Sun (I think) invented find OPTIONS... -exec COMMAND... +, which does the same grouping job as xargs, without the added complications (longer command line, limit to one per find command). Now find ... -exec ... + is standard (it's in Single Unix v3), and more widely available than xargs -0. Unless you have to maintain compatibility with an old Linux system, just forget about xargs.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.