I have a directory structure with files and directories and I like to assign permissions so all the files and directories have read-write permissions for the user and read permissions for the group and, additionally, execution permissions to the directory.

I would like to achieve something like that:

$ ls -l
total 16
-rw-r----- 1 daniel daniel    0  5月 23 16:20 1
-rw-r----- 1 daniel daniel    0  5月 23 16:20 2
-rw-r----- 1 daniel daniel    0  5月 23 16:20 3
-rw-r----- 1 daniel daniel    0  5月 23 16:20 4
-rw-r----- 1 daniel daniel    0  5月 23 16:20 5
drwxr-x--- 2 daniel daniel 4096  5月 23 16:00 a
drwxr-x--- 2 daniel daniel 4096  5月 23 16:00 b
drwxr-x--- 2 daniel daniel 4096  5月 23 15:59 c
drwxr-x--- 2 daniel daniel 4096  5月 23 15:59 d

To give execution (search) permission to directories, but not to files, use:

chmod -R +X .

To assign all the permissions as in your example, use:

chmod -R u=rwX,g=rX,o= .

-R changes files and directories recursively while +X sets execute/search only if the file is a directory or already has execute permission for some user. r and w are of course for reading and writing, respectively.

Mode X (upper x) is documented in both the traditional manual page (man 1 chmod) and the info documentation from coreutils (info coreutils, node Conditional Executability). It also works on BSD and it seems to be a POSIX standard.

  • Yeah, right, I could never adjust to the fact that nowadays many commands (cp, chmod...) have the -R flag... Once upon a time find was the only instrument to do this recursively. – MariusMatutiae May 23 '14 at 8:44

If you want to do it recursively, i.e., to directories within directories within directories, the command to use is:

 find /path/to/starting/directory -type d -exec chmod +x {} \;

This locates all and only subdirectories (-type d flag) of the directory /path/to/starting/directory, and then performs the required change of execute permission to each one of them. The space before \; is mandatory.


Have you tried something like:

chmod +x $(ls -p | grep /), this adds the execution permission ton only directories

and if you would like to give execution permission to files and directories, just do :

chmod +x * and to delete permissions to execute to files try something like this :

chmod -x *.*

Hoping it helps

  • If using Bash, */ can also be used to match directories. – Cristian Ciupitu May 23 '14 at 8:35
  • 1
    Please never recommend parsing ls. This will fail even in the simplest of cases where the directory names contain whitespace. – terdon May 23 '14 at 15:00
  • @terdon, there's also Why not parse ls? – Cristian Ciupitu May 23 '14 at 23:23
  • @CristianCiupitu yes, I know. Mine is the third most upvoted answer there :). – terdon May 24 '14 at 2:17
  • Okay, sorry then and thanks for the advice :) – BigFoot May 24 '14 at 12:36

Try this:

find . -mindepth 1  -print0 | while IFS= read -r -d '' file; do
    if [ -d "$file" ]; then chmod 750 "$file";
    else chmod 640 "$file"; fi


  • -mindepth 1 : That's so find wont match the current directory, ..
  • -print0 : prints null-separated output. This ensures that we deal correctly with fioles containing newlines.
  • while IFS= : setting IFS to the empty string turns off word splitting. Necessary for file/directory names with whitespace.
  • read -r -d '' : this reads each line into $file but the -r ensures we don't treat \ specially (in case there are some in your file names) and -d '' sets the delimiter to the null string so we parse the output of find correctly and can deal with fiole names containing newlines.
  • if [ -d "$file" ]; then chmod 750 "$file"; : If the $file is a directory ([ -d "$file" ], set its permissions to drwxr-x---.
  • else chmod 640 "$file"; fi : if it's not a directory, set them to -rw-r-----.
chmod -R u=rwX,g=rX,o= .

The uppercase X means that it is only set if it's already set somewhere else. Since all your directories are already executable for the user, they will also be made executable for groups. Since files are not executable for the user, they will not be made executable for groups either.

Using chmod +X will have roughly the same effect, except that, when it gives execute rights, it will give execute rights to user, group and other.


Thanks for the answers. I found that in order to assign execution permissions only to directories, it is necessary to use the capital X permission.

So the command would be as follows:

chmod -R a-rwx,u=rwX,g=rX .


(It took me a while to find that, so I wanted to share with everyone. Now I guess that this question will be indexed on Google for the next person who wants to know)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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