Easiest way to chown the contents of a directory? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionSeems that chown is allowed to non root uservsftpd, 553 Could not create file. How do I chown two different users?Clarification: permission and owernship by the apache process of web rootChown for every created file in folderHow to grant a user rights to change ownership of files/directories in a directoryApplying the chmod and chown commands dynamically to the output of find commandHow do I change the ownership of files within current directory, sub-directories, and sub-directories of sub-directories?What is the application of file ownership (chown) in linux?Systemd Permission Issue With ChownRecursively delete empty directories with a dotfile / directory exclusion?

How to answer "Have you ever been terminated?"

Selecting the same column from Different rows Based on Different Criteria

How to react to hostile behavior from a senior developer?

Overriding an object in memory with placement new

Should I discuss the type of campaign with my players?

Short Story with Cinderella as a Voo-doo Witch

Why do people hide their license plates in the EU?

Dating a Former Employee

Why is "Consequences inflicted." not a sentence?

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

Why did the IBM 650 use bi-quinary?

3 doors, three guards, one stone

Can I cast Passwall to drop an enemy into a 20-foot pit?

What is the meaning of the new sigil in Game of Thrones Season 8 intro?

What's the purpose of writing one's academic biography in the third person?

ListPlot join points by nearest neighbor rather than order

How to find out what spells would be useless to a blind NPC spellcaster?

What is the role of the transistor and diode in a soft start circuit?

Why was the term "discrete" used in discrete logarithm?

Why didn't this character "real die" when they blew their stack out in Altered Carbon?

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

51k Euros annually for a family of 4 in Berlin: Is it enough?

What LEGO pieces have "real-world" functionality?

Is there a (better) way to access $wpdb results?



Easiest way to chown the contents of a directory?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionSeems that chown is allowed to non root uservsftpd, 553 Could not create file. How do I chown two different users?Clarification: permission and owernship by the apache process of web rootChown for every created file in folderHow to grant a user rights to change ownership of files/directories in a directoryApplying the chmod and chown commands dynamically to the output of find commandHow do I change the ownership of files within current directory, sub-directories, and sub-directories of sub-directories?What is the application of file ownership (chown) in linux?Systemd Permission Issue With ChownRecursively delete empty directories with a dotfile / directory exclusion?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I often use



ls -ld .
# remember current user and group
chown -R user.group .
chown remembered_user.remembered_group .


Isn't there an easier way to recursively chown all files and directories in a directory but not the directory itself? It should include hidden files and work no matter how many files there are.










share|improve this question






























    3















    I often use



    ls -ld .
    # remember current user and group
    chown -R user.group .
    chown remembered_user.remembered_group .


    Isn't there an easier way to recursively chown all files and directories in a directory but not the directory itself? It should include hidden files and work no matter how many files there are.










    share|improve this question


























      3












      3








      3








      I often use



      ls -ld .
      # remember current user and group
      chown -R user.group .
      chown remembered_user.remembered_group .


      Isn't there an easier way to recursively chown all files and directories in a directory but not the directory itself? It should include hidden files and work no matter how many files there are.










      share|improve this question
















      I often use



      ls -ld .
      # remember current user and group
      chown -R user.group .
      chown remembered_user.remembered_group .


      Isn't there an easier way to recursively chown all files and directories in a directory but not the directory itself? It should include hidden files and work no matter how many files there are.







      linux chown






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 10 '14 at 3:06







      AndreKR

















      asked Apr 10 '14 at 2:10









      AndreKRAndreKR

      382621




      382621




















          6 Answers
          6






          active

          oldest

          votes


















          4














          You can use find to avoid the argument list being too long, while still passing as many arguments to chown in one go as possible (using + instead of ;). -prune allows you to remove some unneeded arguments to chown (it won't descend directories, it will just use chown -R on them):



          find . ! -iname . -prune -exec chown -R user:group +





          share|improve this answer






























            1














            Why not just run chown inside the directory recursively.



            $ chown -R user.group *


            Example



            . directory before.



            $ ll
            total 20
            drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir1
            drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir2
            drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir3
            drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir4
            drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir5


            Change just the contents of dir.



            $ chown -R saml.wheel *


            Contents after.



            $ ll
            total 20
            drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir1
            drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir2
            drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir3
            drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir4
            drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir5


            Meanwhile the directory, . is left intact.



            $ ls -ld .
            drwxrwxr-x. 7 saml saml 4096 Apr 9 22:29 .


            If there are hidden files, make sure to set up your shell to include them in the glob: shopt -s dotglob in bash, FIGNORE='@(.|..)' in ksh93, setopt dot_glob in zsh.



            Alternatively, add a pattern that matches them (beware that in shells other than zsh, .* would match . and ..):



            chown -R saml.wheel * .[!.]* ..?*


            Or in zsh, simply



            chown -R saml.wheel *(D)





            share|improve this answer

























            • argument list too long

              – AndreKR
              Apr 10 '14 at 3:03






            • 1





              And it doesn't affect hidden files either. Not an answer, sorry.

              – AndreKR
              Apr 10 '14 at 3:04











            • @AndreKR - without knowing anymore about the nature of your directory this was a valid A.

              – slm
              Apr 10 '14 at 11:28











            • @AndreKR - Gilles added how to include the dotted files/directories using chown -R.

              – slm
              Apr 10 '14 at 21:45











            • It was in the question: It should include hidden files and work no matter how many files there are.

              – AndreKR
              Apr 10 '14 at 22:58


















            1














            You can use the following:



            find . -mindepth 1 -print0 | xargs -0 chown user:group


            Edit: The problem with your example above is that you are operating on the current folder (.). The mindepth section of my example excludes the current folder from the list of files/directories that are going to be modified.



            Print0 changes the find output to be null-terminated, and xargs operates on each line (or null-terminated string, with -0) and runs the specified command with your input at the end (in this case, each file name). You could change the xargs command to be more explicit like this:



            xargs -0I chown user:group 


            That way, you can put find's output anywhere you want in your command.






            share|improve this answer
































              0














              In zsh:



              chown user:group **/*(D)


              The glob qualifier D means that dot files will be matched.



              You can make chown do the recursive traversal instead of the shell:



              chown -R user:group *(D)


              If you're worried that there are so many files that the command line will be too long, load the chown builtin from the zsh/files module.



              zmodload zsh/files
              chown user:group **/*(D)


              If you don't want to make chown refer to the builtin, you can load only the zf_* names:



              zmodload -Fm zsh/files b:zf_*
              zf_chown user:group **/*(D)


              When you need to call an external command (which isn't the case here) on a long list of files that might not fit in the command line limit, you can use the zargs function:



              autoload -U zargs
              zargs -- **/*(D) -- chown user:group --





              share|improve this answer






























                0














                Chris Down’s answer could still fail if there are a lot of files in the first directory.



                find . ! -iname . -exec chown user:group ;


                In most cases Chris Down’s answer probably won’t fail and will probably be faster than this answer, but I wanted to answer your question exactly as you asked it.



                Also, here’s a slightly different command that worked better for my case. I don’t like changing directories in my scripts, so I wrote the command like this instead:



                find /foo/bar/baz ! -samefile /foo/bar/baz -exec chown user:group ;





                share|improve this answer






























                  -1














                  Following is the command to change ownership of directories and its sub-dirs and all files in it recursively.



                  $ chown -R <username> <folder_to_change_ownership>


                  Step 1: Find the username by running the following command



                  $ whoami


                  this will output the username(your username will be the one you set)



                  manojselvin


                  Step 2: Copy the username from the previous step and replace in the command as follows



                  $ chown -R manojselvin myfolder


                  this command will change the ownership of all directories and sub-directories to the username mentioned. In this case it's "manojselvin"



                  Step 3: To check whether the ownership has been changed run the following command where the dir is located for eg. myfolder in this case.



                  $ ls -la


                  this will output the directories owner details as below



                  drwxr-xr-x 2 manojselvin manojselvin 4096 Jun 18 16:00 myfolder


                  as you can see the ownership is now "manojselvin" which is the username we used in this case. for others it will display their username or group name which has been set






                  share|improve this answer























                  • This changes the owner of the directory itself, too.

                    – AndreKR
                    Jun 18 '18 at 12:36











                  • yes it does @AndreKR

                    – Manoj Selvin
                    Jun 19 '18 at 6:43











                  • It says in the question: "but not the directory itself". That's the whole point of the question.

                    – AndreKR
                    Jun 19 '18 at 8:30












                  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%2f124013%2feasiest-way-to-chown-the-contents-of-a-directory%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  4














                  You can use find to avoid the argument list being too long, while still passing as many arguments to chown in one go as possible (using + instead of ;). -prune allows you to remove some unneeded arguments to chown (it won't descend directories, it will just use chown -R on them):



                  find . ! -iname . -prune -exec chown -R user:group +





                  share|improve this answer



























                    4














                    You can use find to avoid the argument list being too long, while still passing as many arguments to chown in one go as possible (using + instead of ;). -prune allows you to remove some unneeded arguments to chown (it won't descend directories, it will just use chown -R on them):



                    find . ! -iname . -prune -exec chown -R user:group +





                    share|improve this answer

























                      4












                      4








                      4







                      You can use find to avoid the argument list being too long, while still passing as many arguments to chown in one go as possible (using + instead of ;). -prune allows you to remove some unneeded arguments to chown (it won't descend directories, it will just use chown -R on them):



                      find . ! -iname . -prune -exec chown -R user:group +





                      share|improve this answer













                      You can use find to avoid the argument list being too long, while still passing as many arguments to chown in one go as possible (using + instead of ;). -prune allows you to remove some unneeded arguments to chown (it won't descend directories, it will just use chown -R on them):



                      find . ! -iname . -prune -exec chown -R user:group +






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Apr 10 '14 at 3:36









                      Chris DownChris Down

                      81.9k15190204




                      81.9k15190204























                          1














                          Why not just run chown inside the directory recursively.



                          $ chown -R user.group *


                          Example



                          . directory before.



                          $ ll
                          total 20
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir1
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir2
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir3
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir4
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir5


                          Change just the contents of dir.



                          $ chown -R saml.wheel *


                          Contents after.



                          $ ll
                          total 20
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir1
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir2
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir3
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir4
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir5


                          Meanwhile the directory, . is left intact.



                          $ ls -ld .
                          drwxrwxr-x. 7 saml saml 4096 Apr 9 22:29 .


                          If there are hidden files, make sure to set up your shell to include them in the glob: shopt -s dotglob in bash, FIGNORE='@(.|..)' in ksh93, setopt dot_glob in zsh.



                          Alternatively, add a pattern that matches them (beware that in shells other than zsh, .* would match . and ..):



                          chown -R saml.wheel * .[!.]* ..?*


                          Or in zsh, simply



                          chown -R saml.wheel *(D)





                          share|improve this answer

























                          • argument list too long

                            – AndreKR
                            Apr 10 '14 at 3:03






                          • 1





                            And it doesn't affect hidden files either. Not an answer, sorry.

                            – AndreKR
                            Apr 10 '14 at 3:04











                          • @AndreKR - without knowing anymore about the nature of your directory this was a valid A.

                            – slm
                            Apr 10 '14 at 11:28











                          • @AndreKR - Gilles added how to include the dotted files/directories using chown -R.

                            – slm
                            Apr 10 '14 at 21:45











                          • It was in the question: It should include hidden files and work no matter how many files there are.

                            – AndreKR
                            Apr 10 '14 at 22:58















                          1














                          Why not just run chown inside the directory recursively.



                          $ chown -R user.group *


                          Example



                          . directory before.



                          $ ll
                          total 20
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir1
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir2
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir3
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir4
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir5


                          Change just the contents of dir.



                          $ chown -R saml.wheel *


                          Contents after.



                          $ ll
                          total 20
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir1
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir2
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir3
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir4
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir5


                          Meanwhile the directory, . is left intact.



                          $ ls -ld .
                          drwxrwxr-x. 7 saml saml 4096 Apr 9 22:29 .


                          If there are hidden files, make sure to set up your shell to include them in the glob: shopt -s dotglob in bash, FIGNORE='@(.|..)' in ksh93, setopt dot_glob in zsh.



                          Alternatively, add a pattern that matches them (beware that in shells other than zsh, .* would match . and ..):



                          chown -R saml.wheel * .[!.]* ..?*


                          Or in zsh, simply



                          chown -R saml.wheel *(D)





                          share|improve this answer

























                          • argument list too long

                            – AndreKR
                            Apr 10 '14 at 3:03






                          • 1





                            And it doesn't affect hidden files either. Not an answer, sorry.

                            – AndreKR
                            Apr 10 '14 at 3:04











                          • @AndreKR - without knowing anymore about the nature of your directory this was a valid A.

                            – slm
                            Apr 10 '14 at 11:28











                          • @AndreKR - Gilles added how to include the dotted files/directories using chown -R.

                            – slm
                            Apr 10 '14 at 21:45











                          • It was in the question: It should include hidden files and work no matter how many files there are.

                            – AndreKR
                            Apr 10 '14 at 22:58













                          1












                          1








                          1







                          Why not just run chown inside the directory recursively.



                          $ chown -R user.group *


                          Example



                          . directory before.



                          $ ll
                          total 20
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir1
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir2
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir3
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir4
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir5


                          Change just the contents of dir.



                          $ chown -R saml.wheel *


                          Contents after.



                          $ ll
                          total 20
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir1
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir2
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir3
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir4
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir5


                          Meanwhile the directory, . is left intact.



                          $ ls -ld .
                          drwxrwxr-x. 7 saml saml 4096 Apr 9 22:29 .


                          If there are hidden files, make sure to set up your shell to include them in the glob: shopt -s dotglob in bash, FIGNORE='@(.|..)' in ksh93, setopt dot_glob in zsh.



                          Alternatively, add a pattern that matches them (beware that in shells other than zsh, .* would match . and ..):



                          chown -R saml.wheel * .[!.]* ..?*


                          Or in zsh, simply



                          chown -R saml.wheel *(D)





                          share|improve this answer















                          Why not just run chown inside the directory recursively.



                          $ chown -R user.group *


                          Example



                          . directory before.



                          $ ll
                          total 20
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir1
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir2
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir3
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir4
                          drwxrwxr-x. 2 saml saml 4096 Apr 9 22:30 dir5


                          Change just the contents of dir.



                          $ chown -R saml.wheel *


                          Contents after.



                          $ ll
                          total 20
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir1
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir2
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir3
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir4
                          drwxrwxr-x. 2 saml wheel 4096 Apr 9 22:30 dir5


                          Meanwhile the directory, . is left intact.



                          $ ls -ld .
                          drwxrwxr-x. 7 saml saml 4096 Apr 9 22:29 .


                          If there are hidden files, make sure to set up your shell to include them in the glob: shopt -s dotglob in bash, FIGNORE='@(.|..)' in ksh93, setopt dot_glob in zsh.



                          Alternatively, add a pattern that matches them (beware that in shells other than zsh, .* would match . and ..):



                          chown -R saml.wheel * .[!.]* ..?*


                          Or in zsh, simply



                          chown -R saml.wheel *(D)






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Apr 10 '14 at 21:42









                          Gilles

                          548k13011131631




                          548k13011131631










                          answered Apr 10 '14 at 2:32









                          slmslm

                          256k71544690




                          256k71544690












                          • argument list too long

                            – AndreKR
                            Apr 10 '14 at 3:03






                          • 1





                            And it doesn't affect hidden files either. Not an answer, sorry.

                            – AndreKR
                            Apr 10 '14 at 3:04











                          • @AndreKR - without knowing anymore about the nature of your directory this was a valid A.

                            – slm
                            Apr 10 '14 at 11:28











                          • @AndreKR - Gilles added how to include the dotted files/directories using chown -R.

                            – slm
                            Apr 10 '14 at 21:45











                          • It was in the question: It should include hidden files and work no matter how many files there are.

                            – AndreKR
                            Apr 10 '14 at 22:58

















                          • argument list too long

                            – AndreKR
                            Apr 10 '14 at 3:03






                          • 1





                            And it doesn't affect hidden files either. Not an answer, sorry.

                            – AndreKR
                            Apr 10 '14 at 3:04











                          • @AndreKR - without knowing anymore about the nature of your directory this was a valid A.

                            – slm
                            Apr 10 '14 at 11:28











                          • @AndreKR - Gilles added how to include the dotted files/directories using chown -R.

                            – slm
                            Apr 10 '14 at 21:45











                          • It was in the question: It should include hidden files and work no matter how many files there are.

                            – AndreKR
                            Apr 10 '14 at 22:58
















                          argument list too long

                          – AndreKR
                          Apr 10 '14 at 3:03





                          argument list too long

                          – AndreKR
                          Apr 10 '14 at 3:03




                          1




                          1





                          And it doesn't affect hidden files either. Not an answer, sorry.

                          – AndreKR
                          Apr 10 '14 at 3:04





                          And it doesn't affect hidden files either. Not an answer, sorry.

                          – AndreKR
                          Apr 10 '14 at 3:04













                          @AndreKR - without knowing anymore about the nature of your directory this was a valid A.

                          – slm
                          Apr 10 '14 at 11:28





                          @AndreKR - without knowing anymore about the nature of your directory this was a valid A.

                          – slm
                          Apr 10 '14 at 11:28













                          @AndreKR - Gilles added how to include the dotted files/directories using chown -R.

                          – slm
                          Apr 10 '14 at 21:45





                          @AndreKR - Gilles added how to include the dotted files/directories using chown -R.

                          – slm
                          Apr 10 '14 at 21:45













                          It was in the question: It should include hidden files and work no matter how many files there are.

                          – AndreKR
                          Apr 10 '14 at 22:58





                          It was in the question: It should include hidden files and work no matter how many files there are.

                          – AndreKR
                          Apr 10 '14 at 22:58











                          1














                          You can use the following:



                          find . -mindepth 1 -print0 | xargs -0 chown user:group


                          Edit: The problem with your example above is that you are operating on the current folder (.). The mindepth section of my example excludes the current folder from the list of files/directories that are going to be modified.



                          Print0 changes the find output to be null-terminated, and xargs operates on each line (or null-terminated string, with -0) and runs the specified command with your input at the end (in this case, each file name). You could change the xargs command to be more explicit like this:



                          xargs -0I chown user:group 


                          That way, you can put find's output anywhere you want in your command.






                          share|improve this answer





























                            1














                            You can use the following:



                            find . -mindepth 1 -print0 | xargs -0 chown user:group


                            Edit: The problem with your example above is that you are operating on the current folder (.). The mindepth section of my example excludes the current folder from the list of files/directories that are going to be modified.



                            Print0 changes the find output to be null-terminated, and xargs operates on each line (or null-terminated string, with -0) and runs the specified command with your input at the end (in this case, each file name). You could change the xargs command to be more explicit like this:



                            xargs -0I chown user:group 


                            That way, you can put find's output anywhere you want in your command.






                            share|improve this answer



























                              1












                              1








                              1







                              You can use the following:



                              find . -mindepth 1 -print0 | xargs -0 chown user:group


                              Edit: The problem with your example above is that you are operating on the current folder (.). The mindepth section of my example excludes the current folder from the list of files/directories that are going to be modified.



                              Print0 changes the find output to be null-terminated, and xargs operates on each line (or null-terminated string, with -0) and runs the specified command with your input at the end (in this case, each file name). You could change the xargs command to be more explicit like this:



                              xargs -0I chown user:group 


                              That way, you can put find's output anywhere you want in your command.






                              share|improve this answer















                              You can use the following:



                              find . -mindepth 1 -print0 | xargs -0 chown user:group


                              Edit: The problem with your example above is that you are operating on the current folder (.). The mindepth section of my example excludes the current folder from the list of files/directories that are going to be modified.



                              Print0 changes the find output to be null-terminated, and xargs operates on each line (or null-terminated string, with -0) and runs the specified command with your input at the end (in this case, each file name). You could change the xargs command to be more explicit like this:



                              xargs -0I chown user:group 


                              That way, you can put find's output anywhere you want in your command.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 8 hours ago









                              Rui F Ribeiro

                              42.1k1484142




                              42.1k1484142










                              answered Apr 10 '14 at 2:22









                              jkt123jkt123

                              42126




                              42126





















                                  0














                                  In zsh:



                                  chown user:group **/*(D)


                                  The glob qualifier D means that dot files will be matched.



                                  You can make chown do the recursive traversal instead of the shell:



                                  chown -R user:group *(D)


                                  If you're worried that there are so many files that the command line will be too long, load the chown builtin from the zsh/files module.



                                  zmodload zsh/files
                                  chown user:group **/*(D)


                                  If you don't want to make chown refer to the builtin, you can load only the zf_* names:



                                  zmodload -Fm zsh/files b:zf_*
                                  zf_chown user:group **/*(D)


                                  When you need to call an external command (which isn't the case here) on a long list of files that might not fit in the command line limit, you can use the zargs function:



                                  autoload -U zargs
                                  zargs -- **/*(D) -- chown user:group --





                                  share|improve this answer



























                                    0














                                    In zsh:



                                    chown user:group **/*(D)


                                    The glob qualifier D means that dot files will be matched.



                                    You can make chown do the recursive traversal instead of the shell:



                                    chown -R user:group *(D)


                                    If you're worried that there are so many files that the command line will be too long, load the chown builtin from the zsh/files module.



                                    zmodload zsh/files
                                    chown user:group **/*(D)


                                    If you don't want to make chown refer to the builtin, you can load only the zf_* names:



                                    zmodload -Fm zsh/files b:zf_*
                                    zf_chown user:group **/*(D)


                                    When you need to call an external command (which isn't the case here) on a long list of files that might not fit in the command line limit, you can use the zargs function:



                                    autoload -U zargs
                                    zargs -- **/*(D) -- chown user:group --





                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      In zsh:



                                      chown user:group **/*(D)


                                      The glob qualifier D means that dot files will be matched.



                                      You can make chown do the recursive traversal instead of the shell:



                                      chown -R user:group *(D)


                                      If you're worried that there are so many files that the command line will be too long, load the chown builtin from the zsh/files module.



                                      zmodload zsh/files
                                      chown user:group **/*(D)


                                      If you don't want to make chown refer to the builtin, you can load only the zf_* names:



                                      zmodload -Fm zsh/files b:zf_*
                                      zf_chown user:group **/*(D)


                                      When you need to call an external command (which isn't the case here) on a long list of files that might not fit in the command line limit, you can use the zargs function:



                                      autoload -U zargs
                                      zargs -- **/*(D) -- chown user:group --





                                      share|improve this answer













                                      In zsh:



                                      chown user:group **/*(D)


                                      The glob qualifier D means that dot files will be matched.



                                      You can make chown do the recursive traversal instead of the shell:



                                      chown -R user:group *(D)


                                      If you're worried that there are so many files that the command line will be too long, load the chown builtin from the zsh/files module.



                                      zmodload zsh/files
                                      chown user:group **/*(D)


                                      If you don't want to make chown refer to the builtin, you can load only the zf_* names:



                                      zmodload -Fm zsh/files b:zf_*
                                      zf_chown user:group **/*(D)


                                      When you need to call an external command (which isn't the case here) on a long list of files that might not fit in the command line limit, you can use the zargs function:



                                      autoload -U zargs
                                      zargs -- **/*(D) -- chown user:group --






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Apr 11 '14 at 0:15









                                      GillesGilles

                                      548k13011131631




                                      548k13011131631





















                                          0














                                          Chris Down’s answer could still fail if there are a lot of files in the first directory.



                                          find . ! -iname . -exec chown user:group ;


                                          In most cases Chris Down’s answer probably won’t fail and will probably be faster than this answer, but I wanted to answer your question exactly as you asked it.



                                          Also, here’s a slightly different command that worked better for my case. I don’t like changing directories in my scripts, so I wrote the command like this instead:



                                          find /foo/bar/baz ! -samefile /foo/bar/baz -exec chown user:group ;





                                          share|improve this answer



























                                            0














                                            Chris Down’s answer could still fail if there are a lot of files in the first directory.



                                            find . ! -iname . -exec chown user:group ;


                                            In most cases Chris Down’s answer probably won’t fail and will probably be faster than this answer, but I wanted to answer your question exactly as you asked it.



                                            Also, here’s a slightly different command that worked better for my case. I don’t like changing directories in my scripts, so I wrote the command like this instead:



                                            find /foo/bar/baz ! -samefile /foo/bar/baz -exec chown user:group ;





                                            share|improve this answer

























                                              0












                                              0








                                              0







                                              Chris Down’s answer could still fail if there are a lot of files in the first directory.



                                              find . ! -iname . -exec chown user:group ;


                                              In most cases Chris Down’s answer probably won’t fail and will probably be faster than this answer, but I wanted to answer your question exactly as you asked it.



                                              Also, here’s a slightly different command that worked better for my case. I don’t like changing directories in my scripts, so I wrote the command like this instead:



                                              find /foo/bar/baz ! -samefile /foo/bar/baz -exec chown user:group ;





                                              share|improve this answer













                                              Chris Down’s answer could still fail if there are a lot of files in the first directory.



                                              find . ! -iname . -exec chown user:group ;


                                              In most cases Chris Down’s answer probably won’t fail and will probably be faster than this answer, but I wanted to answer your question exactly as you asked it.



                                              Also, here’s a slightly different command that worked better for my case. I don’t like changing directories in my scripts, so I wrote the command like this instead:



                                              find /foo/bar/baz ! -samefile /foo/bar/baz -exec chown user:group ;






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Aug 5 '18 at 16:30









                                              JacksonJackson

                                              23629




                                              23629





















                                                  -1














                                                  Following is the command to change ownership of directories and its sub-dirs and all files in it recursively.



                                                  $ chown -R <username> <folder_to_change_ownership>


                                                  Step 1: Find the username by running the following command



                                                  $ whoami


                                                  this will output the username(your username will be the one you set)



                                                  manojselvin


                                                  Step 2: Copy the username from the previous step and replace in the command as follows



                                                  $ chown -R manojselvin myfolder


                                                  this command will change the ownership of all directories and sub-directories to the username mentioned. In this case it's "manojselvin"



                                                  Step 3: To check whether the ownership has been changed run the following command where the dir is located for eg. myfolder in this case.



                                                  $ ls -la


                                                  this will output the directories owner details as below



                                                  drwxr-xr-x 2 manojselvin manojselvin 4096 Jun 18 16:00 myfolder


                                                  as you can see the ownership is now "manojselvin" which is the username we used in this case. for others it will display their username or group name which has been set






                                                  share|improve this answer























                                                  • This changes the owner of the directory itself, too.

                                                    – AndreKR
                                                    Jun 18 '18 at 12:36











                                                  • yes it does @AndreKR

                                                    – Manoj Selvin
                                                    Jun 19 '18 at 6:43











                                                  • It says in the question: "but not the directory itself". That's the whole point of the question.

                                                    – AndreKR
                                                    Jun 19 '18 at 8:30
















                                                  -1














                                                  Following is the command to change ownership of directories and its sub-dirs and all files in it recursively.



                                                  $ chown -R <username> <folder_to_change_ownership>


                                                  Step 1: Find the username by running the following command



                                                  $ whoami


                                                  this will output the username(your username will be the one you set)



                                                  manojselvin


                                                  Step 2: Copy the username from the previous step and replace in the command as follows



                                                  $ chown -R manojselvin myfolder


                                                  this command will change the ownership of all directories and sub-directories to the username mentioned. In this case it's "manojselvin"



                                                  Step 3: To check whether the ownership has been changed run the following command where the dir is located for eg. myfolder in this case.



                                                  $ ls -la


                                                  this will output the directories owner details as below



                                                  drwxr-xr-x 2 manojselvin manojselvin 4096 Jun 18 16:00 myfolder


                                                  as you can see the ownership is now "manojselvin" which is the username we used in this case. for others it will display their username or group name which has been set






                                                  share|improve this answer























                                                  • This changes the owner of the directory itself, too.

                                                    – AndreKR
                                                    Jun 18 '18 at 12:36











                                                  • yes it does @AndreKR

                                                    – Manoj Selvin
                                                    Jun 19 '18 at 6:43











                                                  • It says in the question: "but not the directory itself". That's the whole point of the question.

                                                    – AndreKR
                                                    Jun 19 '18 at 8:30














                                                  -1












                                                  -1








                                                  -1







                                                  Following is the command to change ownership of directories and its sub-dirs and all files in it recursively.



                                                  $ chown -R <username> <folder_to_change_ownership>


                                                  Step 1: Find the username by running the following command



                                                  $ whoami


                                                  this will output the username(your username will be the one you set)



                                                  manojselvin


                                                  Step 2: Copy the username from the previous step and replace in the command as follows



                                                  $ chown -R manojselvin myfolder


                                                  this command will change the ownership of all directories and sub-directories to the username mentioned. In this case it's "manojselvin"



                                                  Step 3: To check whether the ownership has been changed run the following command where the dir is located for eg. myfolder in this case.



                                                  $ ls -la


                                                  this will output the directories owner details as below



                                                  drwxr-xr-x 2 manojselvin manojselvin 4096 Jun 18 16:00 myfolder


                                                  as you can see the ownership is now "manojselvin" which is the username we used in this case. for others it will display their username or group name which has been set






                                                  share|improve this answer













                                                  Following is the command to change ownership of directories and its sub-dirs and all files in it recursively.



                                                  $ chown -R <username> <folder_to_change_ownership>


                                                  Step 1: Find the username by running the following command



                                                  $ whoami


                                                  this will output the username(your username will be the one you set)



                                                  manojselvin


                                                  Step 2: Copy the username from the previous step and replace in the command as follows



                                                  $ chown -R manojselvin myfolder


                                                  this command will change the ownership of all directories and sub-directories to the username mentioned. In this case it's "manojselvin"



                                                  Step 3: To check whether the ownership has been changed run the following command where the dir is located for eg. myfolder in this case.



                                                  $ ls -la


                                                  this will output the directories owner details as below



                                                  drwxr-xr-x 2 manojselvin manojselvin 4096 Jun 18 16:00 myfolder


                                                  as you can see the ownership is now "manojselvin" which is the username we used in this case. for others it will display their username or group name which has been set







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Jun 18 '18 at 10:34









                                                  Manoj SelvinManoj Selvin

                                                  1092




                                                  1092












                                                  • This changes the owner of the directory itself, too.

                                                    – AndreKR
                                                    Jun 18 '18 at 12:36











                                                  • yes it does @AndreKR

                                                    – Manoj Selvin
                                                    Jun 19 '18 at 6:43











                                                  • It says in the question: "but not the directory itself". That's the whole point of the question.

                                                    – AndreKR
                                                    Jun 19 '18 at 8:30


















                                                  • This changes the owner of the directory itself, too.

                                                    – AndreKR
                                                    Jun 18 '18 at 12:36











                                                  • yes it does @AndreKR

                                                    – Manoj Selvin
                                                    Jun 19 '18 at 6:43











                                                  • It says in the question: "but not the directory itself". That's the whole point of the question.

                                                    – AndreKR
                                                    Jun 19 '18 at 8:30

















                                                  This changes the owner of the directory itself, too.

                                                  – AndreKR
                                                  Jun 18 '18 at 12:36





                                                  This changes the owner of the directory itself, too.

                                                  – AndreKR
                                                  Jun 18 '18 at 12:36













                                                  yes it does @AndreKR

                                                  – Manoj Selvin
                                                  Jun 19 '18 at 6:43





                                                  yes it does @AndreKR

                                                  – Manoj Selvin
                                                  Jun 19 '18 at 6:43













                                                  It says in the question: "but not the directory itself". That's the whole point of the question.

                                                  – AndreKR
                                                  Jun 19 '18 at 8:30






                                                  It says in the question: "but not the directory itself". That's the whole point of the question.

                                                  – AndreKR
                                                  Jun 19 '18 at 8:30


















                                                  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.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f124013%2feasiest-way-to-chown-the-contents-of-a-directory%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







                                                  -chown, linux

                                                  Popular posts from this blog

                                                  Word for a person who has no opinion about whether god existsWord for having a definite opinion while simultaneously withholding judgment?What's the opposite of “newcomer? Is ”veteran" OK?What do you call an “atheist” who might believe in an afterlife?What's a word for someone who wants to voice opinions but not have them challenged?Word for someone who dismisses contrary opinions as irrational?Somone who thinks they are overly special/out of the ordinaryIs there a word, phrase or idiom for “a person who is incapable of thinking about the future”?The belief that a god is human-likeA word for a non-famous person/thing you have heard a lot aboutAdjective for a person who enjoys taking care of their appearance

                                                  What was this official D&D 3.5e Lovecraft-flavored rulebook?What was this set of RPG tools called?As a first-time DM should I let my players play complex character classes and roles?Nymph's Kiss and the RelationshipWhat was the name of this Cleric Prestige Class that shapes metal with its bare hands?Are the 3.5e Dragonlance books third party or official works?What's up with the domain Vile Darkness?What was this 80s book about RPGs?What was the name of this Werewolf band?What book had Rituals to “upgrade” animal companions to keep them viable at higher levels?What was this RPG that had rules for player-owned businesses?

                                                  2017 IndyCar Series Contents Series news Teams and drivers Schedule Season summary Footnotes References External links Navigation menu"INDYCAR: Initial 2018 bodywork concepts unveiled"the original"IndyCar confirms switch to Performance Friction brakes in 2017""AJ Foyt Racing will switch to Chevy"the original"Carlos Munoz, Conor Daly will drive for AJ Foyt Racing""Zach Veach's Indy 500 Debut Confirmed with Foyt""No mass exodus from Honda after Ganassi switch""Ex-F1 driver Sato joins Andretti Autosport for 2017 IndyCar season""IndyCar's Ryan Hunter-Reay, sponsor DHL paired through 2020""hhgregg and Andretti Autosport announce partnership for key races in 2016""INDYCAR: Rossi re-signs with Andretti"the original"McLaren Formula 1 - Fernando Alonso to race at Indy 500 with McLaren, Honda and Andretti Autosport""Shank will finally take part in Indy 500 with Harvey, Andretti | MotorSportsTalk""Andretti adds Jack Harvey to Indy 500 field""Ganassi switches to Honda power for 2017""INDYCAR: Chilton returns to Ganassi"the original"IndyCar silly season: Who's going where in 2017?""INDYCAR: Kanaan, NTT Data return to Ganassi"the original"Kimball to remain at Ganassi for 2017""Coyne confirms Bourdais for 2017 IndyCar season""Davison to sub for Bourdais in Indy 500"the original"Gutierrez confirmed for Detroit IndyCar debut""Gutierrez returns with Coyne for rest of 2017 season""Vautier to drive for Coyne at Texas"the original"INDYCAR: Coyne confirms Jones for 2017"the original"Pippa Mann returns to Coyne for Indy 500""Karam, Dreyer & Reinbold teaming up again for Indianapolis 500""Pigot to return to Ed Carpenter Racing""Hildebrand confirmed as full-time Ed Carpenter driver""Veach to replace injured Hildebrand at Barber"the originalNew Team Harding Racing Enters Chaves for 101st Indianapolis 500"Juncos Racing Announces Entry in 101st Running of the Indianapolis 500 :: Juncos Racing""Juncos confirms Pigot for Indy 500""Saavedra confirmed in Juncos' second 500 entry"the original"Lazier confirms Indy 500 run after son's USF2000 debut"the original"Claman DeMelo to race for RLLR at Sonoma"the original"Rahal signs Servia and ace engineer for 2017""IndyCar: Aleshin returns with Schmidt"the original"Aleshin replaced by Saavedra for Toronto""Jack Harvey will pilot SPM No. 7 car at Watkins Glen, Sonoma""Jay Howard confirmed in Tony Stewart's supported SPM Indy entry""INDYCAR: Newgarden to wave the flag at Penske"the original"Pagenaud opts for No. 1 in 2017"the original"Penske confirms Newgarden for 2017""Montoya to stay with Team Penske in 2017""Target leaving IndyCar after 27 seasons with Chip Ganassi""Cavin: IndyCar could see complete driver/team shakeup in 2017""End of the road for KV Racing?""KV Racing confirms closure, equipment sold to Juncos""Juncos confirms IndyCar Series entry"the original"Juncos readies IndyCar program, aims for '17 500"the original"Harding Racing to add Texas, Pocono to schedule"the original"Sato signs with Andretti Autosport for 2017""INDYCAR: Aleshin in Doubt at SPM"the original"Long Beach notebook: JR Hildebrand breaks hand""Hildebrand cleared to return at Phoenix"the original"Bourdais to undergo surgery on multiple fractures""Aleshin loses Schmidt Peterson IndyCar ride""Saavedra in at SPM for Pocono, Gateway"the original"Bourdais to make return at Gateway"the original"The IndyCar Grand Prix no longer is sponsored by Angie's List""2017 IndyCar Series rulebook""2017 Verizon IndyCar Series Official Rulebook"Official websiteeeeee