1

Is there a concise linux command (that will work on OSX) to change permissions on folders and all of their contents, but leave files in the current directory untouched? For example:

/parent/folder1 <-change permissions
/parent/folder2 <-change permissions
/parent/folder3 <-change permissions
/parent/folder3/file1 <-change permissions
/parent/folder3/file2 <-change permissions
/parent/file1 <-do not change permissions
/parent/file2 <-do not change permissions

6 Answers 6

8

You would need to run 2 commands I believe. This is one way to do it:

# find . -mindepth 1 -type d | xargs chmod 700
# find . -mindepth 2 | xargs chmod 700

The first does directories at the current directory level and deeper. The second does all files and directories deeper than the current directory.

2
  • 1
    Be careful with xargs, as it will most likely fail whenever the files/folders have spaces in their names. (it's possible to solve this passing some parameters to xargs... but is it really worth the trouble? It's easier to find -exec) Apr 28, 2010 at 13:17
  • What Denilson said. Especially on macs, many files have spaces in them.
    – Sirch
    Apr 21, 2013 at 8:50
4

This will change the permissions of every directory in the current directory and all files and folders within them, I think that's what you want but be careful because of the recursion...

find . -maxdepth 1 -type d -exec chmod -R 700 {} \;
1
  • This wont change permissions on files in those directories.
    – Sirch
    Apr 21, 2013 at 8:43
3

You may use find for this or maybe, with a loop, for:

for directory in parent/*; do if [ -d $directory ]; then chmod -R 700 $directory; fi ;done
10
  • That only handles the directories. He wants to do files and directories if they are deeper than the PWD.
    – djhowell
    Oct 19, 2009 at 16:36
  • Please note that chmod with -R flag, change permission in all directories and content recursively. Oct 19, 2009 at 16:42
  • Missed that, my mistake.
    – djhowell
    Oct 19, 2009 at 16:46
  • boucle, n 1: a fabric of uneven yarn that has an uneven knobby effect - Huh? Oct 19, 2009 at 19:13
  • @Dennis Williamson: it seems that you don't like it :\ Oct 19, 2009 at 20:37
1

There is one trivial case where chmod can do this on its own. If you set mode +X (note, capital, not lowercase), it will only apply to directories. This is commonly used for cases where you want to make all files in a directory tree group or world readable. You would do something like this:

chmod a+rX somedir

This will add read and execute permission to somedir and all directories beneath it, but just read permission to regular files beneath it. There unfortunately is no corresponding mode for read/write permissions, though.

1
  • Nice. I didnt know about the X option. OP wanted to chmon on files in lower directories, this doesnt do that though.
    – Sirch
    Apr 21, 2013 at 8:52
0
find  . -type d -mindepth 1 -print -exec chmod 755 {}/* \;

Find every subdirectory of the current directory (-type d and -mindepth 1), and run chmod for all files in each directory found.

{} refers to the directory find is evaluating, and /* tells chmod to operate on just the files in that directory.

0

Here's the one-liner for the general case:

find /parent -maxdepth 1 -type d -print0 | xargs -0 chmod -R 700

The -print0 and -0 arguments use NUL instead of newline as a record separator, so this is safe for filenames with spaces, newlines, and so on. And unlike -exec, it batches filenames into fewer invocations of chmod.

Bonus:

In cases where you know you don't have hidden directories at the top level of /parent (like /parent/.foo) it's even easier:

chmod -R 700 /parent/*/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .