11

I want to set 755 permission on all files and sub-directories under a specific directory, but I want to execute chmod 755 only for those components which does not have 755 permission.

find /main_directory/ -exec chmod 755 {} \;

If the find command returns a long list, this will take a lot of time. I know that I can use the stat command to check the Octal file level permission of each component and then use if-else to toggle the file permission, but is there any single line approach using find and xargs to first check what permission the file/directory has, and then use chmod to change it to 755 if it is set to something else.

3
  • 6
    This may be premature optimisation, and it may not even make if faster. Doing all of those checks may slow it down. Testing to ensure that it is faster, will only pay off, if you have to do this a lot. Jan 8, 2019 at 9:51
  • 3
    You probably don't want to give execute permissions to all files. This will create a security risk. (this is one of the virus vectors on Microsoft's Windows: everything is executable). In symbolic mode you can say u+rw,go+r,go-w,ugo+X — note the capital. Jan 8, 2019 at 9:53
  • I agree with @ctrl-alt-delor but would suggest something closer to what you asked, u=rwX,go=rX which should achieve the same thing.
    – penguin359
    Jan 9, 2019 at 1:13

2 Answers 2

21

If you want to change permissions to 755 on both files and directories, there's no real benefit to using find (from a performance point of view at least), and you could just do

chmod -R 755 /main_directory

If you really want to use find to avoid changing permissions on things that already has 755 permissions (to avoid updating their ctime timestamp), then you should also test for the current permissions on each directory and file:

find /main_directory ! -perm 0755 -exec chmod 755 {} +

The -exec ... {} + will collect as many pathnames as possible that passes the ! -perm 0755 test, and execute chmod on all of them at once.

Usually, one would want to change permissions on files and directories separately, so that not all files are executable:

find /main_directory   -type d ! -perm 0755 -exec chmod 755 {} +
find /main_directory ! -type d ! -perm 0644 -exec chmod 644 {} +
5
  • Worked like charm, thank you. Regards, Kumarjit
    – user316397
    Jan 8, 2019 at 13:11
  • 1
    I suspect using + instead of ; makes a big difference here.
    – Kevin
    Jan 8, 2019 at 18:25
  • This is the speedy way -- avoid a check. I would further use find to make a list of only files you have permission to change this will avoid chmod error-ing out on files it cannot change. <pre> <code> # rwx, rw, wx, w only find . -user $(whoami) -perm /002 ! -perm 0755 -exec chmod 0755 {} \; # break into two operations find . -user $(whoami) -perm /002 ! -perm 0755 > /tmp/chfiles.txt for file in $(</tmp/chfiles.txt) do chmod --quiet 0755 "${file}" done </code></pre>
    – Chris Reid
    Jan 8, 2019 at 20:26
  • @ChrisReid Note that looping over a list of pathnames like that will break if any pathname contains a whitespace character.
    – Kusalananda
    Jan 8, 2019 at 20:30
  • I guess you need to change the Internal Field Separator (IFS) to '\n' in a script. These are environment settings, the way the shell interprets them is dependent on the values of those settings. I test scripts on dummy files before using them to ferret out blunderous distasters like renaming files or overwriting them. I like the shell printf way of setting IFS. IFS=$(printf "\n") # very readable
    – Chris Reid
    Jan 9, 2019 at 22:28
1

As find have gotten the -exec ... + syntax, there's not much point in using xargs, but as you ask for it:

find /main_directory -not -perm 0755 | xargs chmod 755
2
  • 1
    Also note that xargs without -0 (in combination with -print0 in find; which is GNU extension) can be pretty problematic security-wise if potential attacker can create filenames which include whitespaces or other special characters in them. So better stick to -exec when you can Jan 8, 2019 at 17:08
  • No need for xargs (just use -exec), and if you do use it you should use -print0/-0 like Matija says.
    – Kevin
    Jan 8, 2019 at 18:24

You must log in to answer this question.