chmod: How to recursively add execute permissions only to files which already have execute permission

25,380

Solution 1

Use find:

find . -perm /u+x -execdir chmod a+x {} \;

Solution 2

You can use find to get all those files:

find . -type f -perm -o+rx -print0 | xargs -0 chmod a+x

Update: add -print0 to preserve space in filenames

Share:
25,380
Clinton
Author by

Clinton

Updated on July 20, 2022

Comments

  • Clinton
    Clinton almost 2 years

    I noticed:

    chmod -R a+x adds execute permissions to all files, not just those who are currently executable.

    Is there a way to add execute permissions only to those files who already have an execute set for the user permission?

  • Mat
    Mat almost 13 years
    Don't do it that way, it screws up with spaces in the filenames. If you want to pipe to xargs, use the -print0 find option and the -0 xargs option.
  • jw013
    jw013 almost 13 years
    Use -execdir; it's safer than -exec. Also since chmod accepts multiple files in one command line, + instead of \; may have better performance.
  • codeling
    codeling over 10 years
    thanks for mentioning the + mode! The -execdir instead of -exec squashes the gained performance benefit again, though; and since find delivers full file paths anyway, does it matter much? the fastest command for me was ... -exec chmod <mode> {} +