Use chmod command selectively












3














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
























  • 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
    1 hour ago










  • 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
    1 hour ago


















3














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
























  • 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
    1 hour ago










  • 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
    1 hour ago
















3












3








3







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









chaos

35.1k773116




35.1k773116










asked 2 hours ago









Kumarjit GhoshKumarjit Ghosh

252




252












  • 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
    1 hour ago










  • 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
    1 hour ago




















  • 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
    1 hour ago










  • 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
    1 hour ago


















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
1 hour ago




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
1 hour ago












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
1 hour ago






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
1 hour ago












2 Answers
2






active

oldest

votes


















6














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































    0














    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





















      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









      6














      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




























        6














        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


























          6












          6








          6






          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 1 hour ago

























          answered 2 hours ago









          KusalanandaKusalananda

          123k16230375




          123k16230375

























              0














              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


























                0














                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
























                  0












                  0








                  0






                  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 1 hour ago









                  HenrikHenrik

                  3,5831419




                  3,5831419






























                      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

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      How to ignore python UserWarning in pytest?

                      Alexandru Averescu