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

                                                  Creating 100m^2 grid automatically using QGIS?Creating grid constrained within polygon in QGIS?Createing polygon layer from point data using QGIS?Creating vector grid using QGIS?Creating grid polygons from coordinates using R or PythonCreating grid from spatio temporal point data?Creating fields in attributes table using other layers using QGISCreate .shp vector grid in QGISQGIS Creating 4km point grid within polygonsCreate a vector grid over a raster layerVector Grid Creates just one grid

                                                  What is this called? Old film camera viewer?What makes a good film camera?What to do with an old film camera?What should one look for when buying a used film camera?What is the value and age of this pre-1967 Ricoh 35 mm camera?DSLR recommendation, question about old Canon 35mm film Camera & lensesCan anyone identify the silver rangefinder-style camera in this advertisement?What kind of a Polaroid 600-camera is this?Will an old film camera still work even when not used in a very long time?What is this camera / Can I develop the film?How to fit an action camera into antique (bellows) housing?What to check when buying used and old film bodies?

                                                  Why is this plane circling around the Lucknow airport every day?Why do aircraft on Flight Radar 24 jump around randomly sometimes?What airport has this walkway over a taxiway?How does Chicago O'Hare's tower sequence aircraft at peak capacity?Which airport is featured in this Delta commercial?After a crash, for how long is the airport closed?Can a passenger plane stand still in the air, or hover at a fixed location above a ground?What are those trucks towing around, and why?What is this airport outside of Cairo, Egypt?Which US airport has the lowest circling MDH?What is this airport video?