How to remove executable bit recursively from files (not directories)

74

38

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?

Mike L.

Posted 2011-01-18T09:15:06.780

Reputation: 4 539

possible duplicate of How to chmod all directories except files (recursively)?

– G-Man Says 'Reinstate Monica' – 2015-05-07T19:03:16.093

Answers

136

With GNU chmod (on Ubuntu) single command variant (starting in the current directory):

chmod -R -x+X .

Explanation:

  • -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.

With BSD chmod (which is present on Mac OS X) you have to do it separately in two commands:

sudo chmod -R -x * && sudo chmod -R +X *

(If you want to include hidden files in the main directory as well, you likely need to change * to . (point), but it is untested.)

pabouk

Posted 2011-01-18T09:15:06.780

Reputation: 5 358

This is just sad... :( I like the find variant in the answer below for it's style of combining simple tools that do One Thing Well. – mikezter – 2014-08-05T18:00:12.243

2Use . instead of * if you want this to be applied to all files – John Magnolia – 2015-04-11T04:49:35.247

@mikezter ...but find is a nightmare to learn how to use. – Kyle Strand – 2015-08-04T18:02:41.000

@pabouk: mind John's advise: by default * will not include hidden files in the starting directory. so . is preferable. – MestreLion – 2015-09-16T18:48:32.467

For the BSD chmod version, it seems to me that the second sudo is unneeded: sudo chmod -R -x * && chmod -R +X * works (the executable flags added on directories allow chmod to operate inside them next -- the reverse being the reason the first sudo is required indeed). – Socce – 2018-02-21T17:16:55.193

1On Ubuntu 13.04 a minor tweak is necessary: chmod -R a-x+X * – Eero Aaltonen – 2013-08-22T12:37:43.867

@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 – 2013-08-26T17:22:27.663

@pabouk oopsie! It was actually the CentOs box where I had set my umask to 0007. – Eero Aaltonen – 2013-08-28T12:15:48.810

46

If you cd into the correct path first:

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

or

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.

Matthijs P

Posted 2011-01-18T09:15:06.780

Reputation: 1 153

the 2nd approach will fail for paths that contain spaces. – MestreLion – 2015-09-16T18:50:38.013

@ephemient: if your find supports -print0 I'm pretty sure it will also support -exec – MestreLion – 2015-09-16T18:52:47.537

6If 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 – 2012-06-08T19:49:08.040

5I'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 – 2013-05-28T12:54:54.427

4+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 – 2013-11-09T11:14:54.587

3

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

find . -type f -print0 | xargs -0 chmod -x

user227317

Posted 2011-01-18T09:15:06.780

Reputation: 31

See ephemient's comment below Matthijs's answer to see why this answer is useful. – PatrickT – 2016-06-23T18:33:42.710

1This is, in essence, not different from Matthijs P's answer from 2011. – slhck – 2013-05-28T11:22:09.760

Can you remember this command line? I can't. – Mike L. – 2013-05-28T11:48:18.050

2

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

#!/usr/bin/python3
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.

mic_e

Posted 2011-01-18T09:15:06.780

Reputation: 259