The Computer Oracle

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

Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Ocean Floor

--

Chapters
00:00 Question
00:31 Accepted answer (Score 172)
01:38 Answer 2 (Score 49)
02:11 Answer 3 (Score 4)
02:28 Answer 4 (Score 2)
02:58 Thank you

--

Full question
https://superuser.com/questions/234647/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#chmod

#avk47



ACCEPTED ANSWER

Score 173


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




ANSWER 2

Score 49


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.




ANSWER 3

Score 4


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



ANSWER 4

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