Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I plug-in an USB stick (FAT) into my Mac or Ubuntu machine, all files have the executable bits set. After having copied the directory structure to my hard disk how do I remove the executable bits recursively just from the files and keep those on the directories?

share|improve this question
possible duplicate of How to chmod all directories except files (recursively)? – G-Man May 7 '15 at 19:03
up vote 82 down vote accepted

A single command variant (starting in the current directory):

chmod -R -x+X *


  • -R - operate recursively
  • -x - remove executable flags for all users
  • +X - set executable flags for all users if it is a directory

In this case the capital X applies only to directories because all executable flags were cleared by -x. Otherwise +X sets executable flag(s) also if the flag was originally set for any of user, group or others.

Note: I tested the command with GNU chmod (on Ubuntu). I am not sure about the BSD chmod which is present on Mac OS X.

share|improve this answer
seems not to work on OS X 10.7.5 - chmod -R -v -v -x+X * does not print anything. – Mike L. Nov 20 '12 at 14:41
Just figured out on OS X you have to do it separately in two commands: sudo chmod -R -x * && sudo chmod -R +X * – drvdijk Feb 21 '13 at 3:08
On Ubuntu 13.04 a minor tweak is necessary: chmod -R a-x+X * – Eero Aaltonen Aug 22 '13 at 12:37
@EeroAaltonen: Thank you for the note. This could happen if your umask does not allow x permission for all. Do you use default umask or did you change it? Could you please send output of umask command? Besides your solution there is also this possibility: chmod -R a-x,+X * which will set the x permission according to your umask. I will update the answer but I would like to check the behaviour in BSD / Mac OS X first. – pabouk Aug 26 '13 at 17:22
Use . instead of * if you want this to be applied to all files – John Magnolia Apr 11 '15 at 4:49

If you cd into the correct path first:

find . -type f -exec chmod -x {} \;


chmod -x $(find . -type f)

The find finds all files of type 'f' (which means regular file) in the path . and then calls chmod -x on each file. The {} gets substituted for the file name and the \; terminates the chmod command.

share|improve this answer
If your find supports it, use -exec ... \+ instead of -exec ... \; — it'll require fewer fork+execs. If it doesn't, use find ... -print0 | xargs -0 .... – ephemient Jun 8 '12 at 19:49
I've used this technique, but with "-perm +111" added to the find so it only chmod's ones that have the x-bit set: find . -type f -perm +111 -exec chmod -x {} \; – chrish May 28 '13 at 12:54
+1 @Matthijs The reason why this is better than pabouk's solution is that this command leaves directories alone, while pabouk's re-sets the executable bit in all directories. There might be some directories which have the executable bit not set, and pabouk's command sets it, while one might wish to leave them as they are. – MariusMatutiae Nov 9 '13 at 11:14
the 2nd approach will fail for paths that contain spaces. – MestreLion Sep 16 '15 at 18:50
@ephemient: if your find supports -print0 I'm pretty sure it will also support -exec – MestreLion Sep 16 '15 at 18:52

Under Linux and Unix in a terminal window or On Mac OS X, use this in

find . -type f -print0 | xargs -0 chmod -x
share|improve this answer
This is, in essence, not different from Matthijs P's answer from 2011. – slhck May 28 '13 at 11:22
Can you remember this command line? I can't. – Mike L. May 28 '13 at 11:48
See ephemient's comment below Matthijs's answer to see why this answer is useful. – PatrickT Jun 23 at 18:33

The chmod -x+X way did not work for me on ubuntu either, thus I wrote this minimal python script:

import os
for par, dirs, files in os.walk('.'):
    for d in dirs:
        os.chmod(par + '/' + d, 0o755)
    for f in files:
        os.chmod(par + '/' + f, 0o644)

If there might be any fancy extra stuff such as sockets in your filesystem, you may want to surround the last chmod with a try/catch.

share|improve this answer

You must log in to answer this question.

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