Use chmod command selectively












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.










share|improve this question




















  • 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.
    – ctrl-alt-delor
    yesterday






  • 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.
    – ctrl-alt-delor
    yesterday












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
















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.










share|improve this question




















  • 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.
    – ctrl-alt-delor
    yesterday






  • 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.
    – ctrl-alt-delor
    yesterday












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














11












11








11


2





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.










share|improve this question















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.







command-line find chmod






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









chaos

35.1k773116




35.1k773116










asked yesterday









Kumarjit GhoshKumarjit Ghosh

694




694








  • 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.
    – ctrl-alt-delor
    yesterday






  • 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.
    – ctrl-alt-delor
    yesterday












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














  • 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.
    – ctrl-alt-delor
    yesterday






  • 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.
    – ctrl-alt-delor
    yesterday












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








6




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.
– ctrl-alt-delor
yesterday




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.
– ctrl-alt-delor
yesterday




3




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.
– ctrl-alt-delor
yesterday






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.
– ctrl-alt-delor
yesterday














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
yesterday




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
yesterday










2 Answers
2






active

oldest

votes


















20














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 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 {} +





share|improve this answer























  • Worked like charm, thank you. Regards, Kumarjit
    – Kumarjit Ghosh
    yesterday






  • 1




    I suspect using + instead of ; makes a big difference here.
    – Kevin
    yesterday










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












  • @ChrisReid Note that looping over a list of pathnames like that will break if any pathname contains a whitespace character.
    – Kusalananda
    yesterday










  • 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
    8 hours ago



















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





share|improve this answer

















  • 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
    – Matija Nalis
    yesterday










  • No need for xargs (just use -exec), and if you do use it you should use -print0/-0 like Matija says.
    – Kevin
    yesterday











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493181%2fuse-chmod-command-selectively%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









20














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 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 {} +





share|improve this answer























  • Worked like charm, thank you. Regards, Kumarjit
    – Kumarjit Ghosh
    yesterday






  • 1




    I suspect using + instead of ; makes a big difference here.
    – Kevin
    yesterday










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












  • @ChrisReid Note that looping over a list of pathnames like that will break if any pathname contains a whitespace character.
    – Kusalananda
    yesterday










  • 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
    8 hours ago
















20














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 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 {} +





share|improve this answer























  • Worked like charm, thank you. Regards, Kumarjit
    – Kumarjit Ghosh
    yesterday






  • 1




    I suspect using + instead of ; makes a big difference here.
    – Kevin
    yesterday










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












  • @ChrisReid Note that looping over a list of pathnames like that will break if any pathname contains a whitespace character.
    – Kusalananda
    yesterday










  • 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
    8 hours ago














20












20








20






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 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 {} +





share|improve this answer














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 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 {} +






share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









KusalanandaKusalananda

123k16232379




123k16232379












  • Worked like charm, thank you. Regards, Kumarjit
    – Kumarjit Ghosh
    yesterday






  • 1




    I suspect using + instead of ; makes a big difference here.
    – Kevin
    yesterday










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












  • @ChrisReid Note that looping over a list of pathnames like that will break if any pathname contains a whitespace character.
    – Kusalananda
    yesterday










  • 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
    8 hours ago


















  • Worked like charm, thank you. Regards, Kumarjit
    – Kumarjit Ghosh
    yesterday






  • 1




    I suspect using + instead of ; makes a big difference here.
    – Kevin
    yesterday










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












  • @ChrisReid Note that looping over a list of pathnames like that will break if any pathname contains a whitespace character.
    – Kusalananda
    yesterday










  • 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
    8 hours ago
















Worked like charm, thank you. Regards, Kumarjit
– Kumarjit Ghosh
yesterday




Worked like charm, thank you. Regards, Kumarjit
– Kumarjit Ghosh
yesterday




1




1




I suspect using + instead of ; makes a big difference here.
– Kevin
yesterday




I suspect using + instead of ; makes a big difference here.
– Kevin
yesterday












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
yesterday






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
yesterday














@ChrisReid Note that looping over a list of pathnames like that will break if any pathname contains a whitespace character.
– Kusalananda
yesterday




@ChrisReid Note that looping over a list of pathnames like that will break if any pathname contains a whitespace character.
– Kusalananda
yesterday












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
8 hours ago




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
8 hours ago













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





share|improve this answer

















  • 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
    – Matija Nalis
    yesterday










  • No need for xargs (just use -exec), and if you do use it you should use -print0/-0 like Matija says.
    – Kevin
    yesterday
















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





share|improve this answer

















  • 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
    – Matija Nalis
    yesterday










  • No need for xargs (just use -exec), and if you do use it you should use -print0/-0 like Matija says.
    – Kevin
    yesterday














1












1








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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









HenrikHenrik

3,6011419




3,6011419








  • 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
    – Matija Nalis
    yesterday










  • No need for xargs (just use -exec), and if you do use it you should use -print0/-0 like Matija says.
    – Kevin
    yesterday














  • 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
    – Matija Nalis
    yesterday










  • No need for xargs (just use -exec), and if you do use it you should use -print0/-0 like Matija says.
    – Kevin
    yesterday








1




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
– Matija Nalis
yesterday




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
– Matija Nalis
yesterday












No need for xargs (just use -exec), and if you do use it you should use -print0/-0 like Matija says.
– Kevin
yesterday




No need for xargs (just use -exec), and if you do use it you should use -print0/-0 like Matija says.
– Kevin
yesterday


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493181%2fuse-chmod-command-selectively%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1300-talet

1300-talet

Display a custom attribute below product name in the front-end Magento 1.9.3.8