How to get a list of all owners of files in a directory

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
11
down vote

favorite












I am currently trying to fix my quota system. My problem is that I cannot determine if all files in a directory are owned by the same user. If possible is there a way to list the different owners of files in a directory (recursively).



e.g get-owners-of DIRNAME







share|improve this question


















  • 1




    So do you want to list all owners only, or all files with their owners, or all files owned by anyone other than a specific user?
    – Byte Commander
    Apr 21 at 13:15














up vote
11
down vote

favorite












I am currently trying to fix my quota system. My problem is that I cannot determine if all files in a directory are owned by the same user. If possible is there a way to list the different owners of files in a directory (recursively).



e.g get-owners-of DIRNAME







share|improve this question


















  • 1




    So do you want to list all owners only, or all files with their owners, or all files owned by anyone other than a specific user?
    – Byte Commander
    Apr 21 at 13:15












up vote
11
down vote

favorite









up vote
11
down vote

favorite











I am currently trying to fix my quota system. My problem is that I cannot determine if all files in a directory are owned by the same user. If possible is there a way to list the different owners of files in a directory (recursively).



e.g get-owners-of DIRNAME







share|improve this question














I am currently trying to fix my quota system. My problem is that I cannot determine if all files in a directory are owned by the same user. If possible is there a way to list the different owners of files in a directory (recursively).



e.g get-owners-of DIRNAME









share|improve this question













share|improve this question




share|improve this question








edited Apr 22 at 21:09









dessert

19.8k55594




19.8k55594










asked Apr 21 at 13:08









Jack7076

75117




75117







  • 1




    So do you want to list all owners only, or all files with their owners, or all files owned by anyone other than a specific user?
    – Byte Commander
    Apr 21 at 13:15












  • 1




    So do you want to list all owners only, or all files with their owners, or all files owned by anyone other than a specific user?
    – Byte Commander
    Apr 21 at 13:15







1




1




So do you want to list all owners only, or all files with their owners, or all files owned by anyone other than a specific user?
– Byte Commander
Apr 21 at 13:15




So do you want to list all owners only, or all files with their owners, or all files owned by anyone other than a specific user?
– Byte Commander
Apr 21 at 13:15










4 Answers
4






active

oldest

votes

















up vote
19
down vote



accepted










stat -c %U * 


will list owners of all files.



This can be sorted and duplicates removed by piping it into sort -u:



stat -c %U * | sort -u


As pointed out by steeldriver, this is not recursive. I missed that this was asked for. It can be made recursive by enabling globstar:



shopt -s globstar
stat -c %U **/* | sort -u


Alltogether, steeldriver's answer is probably better and should be the accepted answer here :)






share|improve this answer






















  • Won't that go over the command-line length, if there is a large number of files in the search? If so, then @steeldriver 's answer is better.
    – CSM
    Apr 21 at 17:00










  • @CSM it will. Which is why I say that steeldrivers answer is a better one in many cases.
    – vidarlo
    Apr 21 at 17:01






  • 2




    @CSM I guess if ARG_MAX is an issue you could do printf '%s' **/* | xargs -0 stat -c %U (since printf is a builtin, it shouldn't have the same length limitation)
    – steeldriver
    Apr 21 at 17:09

















up vote
20
down vote













You can use find to print the user (owner) and group and then extract the uniq combinations e.g.



$ sudo find /var -printf '%u:%gn' | sort -t: -u
_apt:root
avahi-autoipd:avahi-autoipd
clamav:adm
clamav:clamav
colord:colord
daemon:daemon
lightdm:lightdm
lp:lp
man:root
root:adm
root:crontab
root:lp
root:mail
root:mlocate
root:root
root:shadow
root:staff
root:syslog
root:utmp
root:whoopsie
speech-dispatcher:root
statd:nogroup
steeldriver:crontab
steeldriver:lightdm
steeldriver:steeldriver
syslog:adm
systemd-timesync:systemd-timesync
testuser:crontab





share|improve this answer
















  • 1




    To evaluate directory content only (and not the root directory/-ies of the search itself) add -mindepth 1 before -printf. And I wouldn't include sudo in the example when OP doesn't appear to work in a context where it's required.
    – David Foerster
    Apr 21 at 22:54











  • Does -t: make a difference in this context?
    – kasperd
    Apr 22 at 22:55










  • @kasperd good point - probably not (it might affect the sort order - but we're not really interested in that)
    – steeldriver
    Apr 23 at 1:06

















up vote
5
down vote













You may find it more efficient to directly search for the files not owned by the user ...



find /directory ! -user username -printf "%u %pn" 





share|improve this answer



























    up vote
    4
    down vote













    DIY method via Python:



    #!/usr/bin/env python3
    import sys,os,pwd
    for f in sys.argv[1:]:
    username = pwd.getpwuid(os.stat(f).st_uid).pw_name
    print( ":".join([f,username]) )


    This iterates over all filenames listed on command-line, gets UID of the file's owner, and using pwd module gets the username of the owner. After that, filename and username joined for pretty printing and separated via colon. Works as so:



    $ ./get_owners.py /etc/* 
    /etc/acpi:root
    /etc/adduser.conf:root
    /etc/alternatives:root
    . . .





    share|improve this answer




















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "89"
      ;
      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',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1026934%2fhow-to-get-a-list-of-all-owners-of-files-in-a-directory%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      19
      down vote



      accepted










      stat -c %U * 


      will list owners of all files.



      This can be sorted and duplicates removed by piping it into sort -u:



      stat -c %U * | sort -u


      As pointed out by steeldriver, this is not recursive. I missed that this was asked for. It can be made recursive by enabling globstar:



      shopt -s globstar
      stat -c %U **/* | sort -u


      Alltogether, steeldriver's answer is probably better and should be the accepted answer here :)






      share|improve this answer






















      • Won't that go over the command-line length, if there is a large number of files in the search? If so, then @steeldriver 's answer is better.
        – CSM
        Apr 21 at 17:00










      • @CSM it will. Which is why I say that steeldrivers answer is a better one in many cases.
        – vidarlo
        Apr 21 at 17:01






      • 2




        @CSM I guess if ARG_MAX is an issue you could do printf '%s' **/* | xargs -0 stat -c %U (since printf is a builtin, it shouldn't have the same length limitation)
        – steeldriver
        Apr 21 at 17:09














      up vote
      19
      down vote



      accepted










      stat -c %U * 


      will list owners of all files.



      This can be sorted and duplicates removed by piping it into sort -u:



      stat -c %U * | sort -u


      As pointed out by steeldriver, this is not recursive. I missed that this was asked for. It can be made recursive by enabling globstar:



      shopt -s globstar
      stat -c %U **/* | sort -u


      Alltogether, steeldriver's answer is probably better and should be the accepted answer here :)






      share|improve this answer






















      • Won't that go over the command-line length, if there is a large number of files in the search? If so, then @steeldriver 's answer is better.
        – CSM
        Apr 21 at 17:00










      • @CSM it will. Which is why I say that steeldrivers answer is a better one in many cases.
        – vidarlo
        Apr 21 at 17:01






      • 2




        @CSM I guess if ARG_MAX is an issue you could do printf '%s' **/* | xargs -0 stat -c %U (since printf is a builtin, it shouldn't have the same length limitation)
        – steeldriver
        Apr 21 at 17:09












      up vote
      19
      down vote



      accepted







      up vote
      19
      down vote



      accepted






      stat -c %U * 


      will list owners of all files.



      This can be sorted and duplicates removed by piping it into sort -u:



      stat -c %U * | sort -u


      As pointed out by steeldriver, this is not recursive. I missed that this was asked for. It can be made recursive by enabling globstar:



      shopt -s globstar
      stat -c %U **/* | sort -u


      Alltogether, steeldriver's answer is probably better and should be the accepted answer here :)






      share|improve this answer














      stat -c %U * 


      will list owners of all files.



      This can be sorted and duplicates removed by piping it into sort -u:



      stat -c %U * | sort -u


      As pointed out by steeldriver, this is not recursive. I missed that this was asked for. It can be made recursive by enabling globstar:



      shopt -s globstar
      stat -c %U **/* | sort -u


      Alltogether, steeldriver's answer is probably better and should be the accepted answer here :)







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 21 at 14:15

























      answered Apr 21 at 13:17









      vidarlo

      7,12342140




      7,12342140











      • Won't that go over the command-line length, if there is a large number of files in the search? If so, then @steeldriver 's answer is better.
        – CSM
        Apr 21 at 17:00










      • @CSM it will. Which is why I say that steeldrivers answer is a better one in many cases.
        – vidarlo
        Apr 21 at 17:01






      • 2




        @CSM I guess if ARG_MAX is an issue you could do printf '%s' **/* | xargs -0 stat -c %U (since printf is a builtin, it shouldn't have the same length limitation)
        – steeldriver
        Apr 21 at 17:09
















      • Won't that go over the command-line length, if there is a large number of files in the search? If so, then @steeldriver 's answer is better.
        – CSM
        Apr 21 at 17:00










      • @CSM it will. Which is why I say that steeldrivers answer is a better one in many cases.
        – vidarlo
        Apr 21 at 17:01






      • 2




        @CSM I guess if ARG_MAX is an issue you could do printf '%s' **/* | xargs -0 stat -c %U (since printf is a builtin, it shouldn't have the same length limitation)
        – steeldriver
        Apr 21 at 17:09















      Won't that go over the command-line length, if there is a large number of files in the search? If so, then @steeldriver 's answer is better.
      – CSM
      Apr 21 at 17:00




      Won't that go over the command-line length, if there is a large number of files in the search? If so, then @steeldriver 's answer is better.
      – CSM
      Apr 21 at 17:00












      @CSM it will. Which is why I say that steeldrivers answer is a better one in many cases.
      – vidarlo
      Apr 21 at 17:01




      @CSM it will. Which is why I say that steeldrivers answer is a better one in many cases.
      – vidarlo
      Apr 21 at 17:01




      2




      2




      @CSM I guess if ARG_MAX is an issue you could do printf '%s' **/* | xargs -0 stat -c %U (since printf is a builtin, it shouldn't have the same length limitation)
      – steeldriver
      Apr 21 at 17:09




      @CSM I guess if ARG_MAX is an issue you could do printf '%s' **/* | xargs -0 stat -c %U (since printf is a builtin, it shouldn't have the same length limitation)
      – steeldriver
      Apr 21 at 17:09












      up vote
      20
      down vote













      You can use find to print the user (owner) and group and then extract the uniq combinations e.g.



      $ sudo find /var -printf '%u:%gn' | sort -t: -u
      _apt:root
      avahi-autoipd:avahi-autoipd
      clamav:adm
      clamav:clamav
      colord:colord
      daemon:daemon
      lightdm:lightdm
      lp:lp
      man:root
      root:adm
      root:crontab
      root:lp
      root:mail
      root:mlocate
      root:root
      root:shadow
      root:staff
      root:syslog
      root:utmp
      root:whoopsie
      speech-dispatcher:root
      statd:nogroup
      steeldriver:crontab
      steeldriver:lightdm
      steeldriver:steeldriver
      syslog:adm
      systemd-timesync:systemd-timesync
      testuser:crontab





      share|improve this answer
















      • 1




        To evaluate directory content only (and not the root directory/-ies of the search itself) add -mindepth 1 before -printf. And I wouldn't include sudo in the example when OP doesn't appear to work in a context where it's required.
        – David Foerster
        Apr 21 at 22:54











      • Does -t: make a difference in this context?
        – kasperd
        Apr 22 at 22:55










      • @kasperd good point - probably not (it might affect the sort order - but we're not really interested in that)
        – steeldriver
        Apr 23 at 1:06














      up vote
      20
      down vote













      You can use find to print the user (owner) and group and then extract the uniq combinations e.g.



      $ sudo find /var -printf '%u:%gn' | sort -t: -u
      _apt:root
      avahi-autoipd:avahi-autoipd
      clamav:adm
      clamav:clamav
      colord:colord
      daemon:daemon
      lightdm:lightdm
      lp:lp
      man:root
      root:adm
      root:crontab
      root:lp
      root:mail
      root:mlocate
      root:root
      root:shadow
      root:staff
      root:syslog
      root:utmp
      root:whoopsie
      speech-dispatcher:root
      statd:nogroup
      steeldriver:crontab
      steeldriver:lightdm
      steeldriver:steeldriver
      syslog:adm
      systemd-timesync:systemd-timesync
      testuser:crontab





      share|improve this answer
















      • 1




        To evaluate directory content only (and not the root directory/-ies of the search itself) add -mindepth 1 before -printf. And I wouldn't include sudo in the example when OP doesn't appear to work in a context where it's required.
        – David Foerster
        Apr 21 at 22:54











      • Does -t: make a difference in this context?
        – kasperd
        Apr 22 at 22:55










      • @kasperd good point - probably not (it might affect the sort order - but we're not really interested in that)
        – steeldriver
        Apr 23 at 1:06












      up vote
      20
      down vote










      up vote
      20
      down vote









      You can use find to print the user (owner) and group and then extract the uniq combinations e.g.



      $ sudo find /var -printf '%u:%gn' | sort -t: -u
      _apt:root
      avahi-autoipd:avahi-autoipd
      clamav:adm
      clamav:clamav
      colord:colord
      daemon:daemon
      lightdm:lightdm
      lp:lp
      man:root
      root:adm
      root:crontab
      root:lp
      root:mail
      root:mlocate
      root:root
      root:shadow
      root:staff
      root:syslog
      root:utmp
      root:whoopsie
      speech-dispatcher:root
      statd:nogroup
      steeldriver:crontab
      steeldriver:lightdm
      steeldriver:steeldriver
      syslog:adm
      systemd-timesync:systemd-timesync
      testuser:crontab





      share|improve this answer












      You can use find to print the user (owner) and group and then extract the uniq combinations e.g.



      $ sudo find /var -printf '%u:%gn' | sort -t: -u
      _apt:root
      avahi-autoipd:avahi-autoipd
      clamav:adm
      clamav:clamav
      colord:colord
      daemon:daemon
      lightdm:lightdm
      lp:lp
      man:root
      root:adm
      root:crontab
      root:lp
      root:mail
      root:mlocate
      root:root
      root:shadow
      root:staff
      root:syslog
      root:utmp
      root:whoopsie
      speech-dispatcher:root
      statd:nogroup
      steeldriver:crontab
      steeldriver:lightdm
      steeldriver:steeldriver
      syslog:adm
      systemd-timesync:systemd-timesync
      testuser:crontab






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 21 at 13:19









      steeldriver

      62.8k1197165




      62.8k1197165







      • 1




        To evaluate directory content only (and not the root directory/-ies of the search itself) add -mindepth 1 before -printf. And I wouldn't include sudo in the example when OP doesn't appear to work in a context where it's required.
        – David Foerster
        Apr 21 at 22:54











      • Does -t: make a difference in this context?
        – kasperd
        Apr 22 at 22:55










      • @kasperd good point - probably not (it might affect the sort order - but we're not really interested in that)
        – steeldriver
        Apr 23 at 1:06












      • 1




        To evaluate directory content only (and not the root directory/-ies of the search itself) add -mindepth 1 before -printf. And I wouldn't include sudo in the example when OP doesn't appear to work in a context where it's required.
        – David Foerster
        Apr 21 at 22:54











      • Does -t: make a difference in this context?
        – kasperd
        Apr 22 at 22:55










      • @kasperd good point - probably not (it might affect the sort order - but we're not really interested in that)
        – steeldriver
        Apr 23 at 1:06







      1




      1




      To evaluate directory content only (and not the root directory/-ies of the search itself) add -mindepth 1 before -printf. And I wouldn't include sudo in the example when OP doesn't appear to work in a context where it's required.
      – David Foerster
      Apr 21 at 22:54





      To evaluate directory content only (and not the root directory/-ies of the search itself) add -mindepth 1 before -printf. And I wouldn't include sudo in the example when OP doesn't appear to work in a context where it's required.
      – David Foerster
      Apr 21 at 22:54













      Does -t: make a difference in this context?
      – kasperd
      Apr 22 at 22:55




      Does -t: make a difference in this context?
      – kasperd
      Apr 22 at 22:55












      @kasperd good point - probably not (it might affect the sort order - but we're not really interested in that)
      – steeldriver
      Apr 23 at 1:06




      @kasperd good point - probably not (it might affect the sort order - but we're not really interested in that)
      – steeldriver
      Apr 23 at 1:06










      up vote
      5
      down vote













      You may find it more efficient to directly search for the files not owned by the user ...



      find /directory ! -user username -printf "%u %pn" 





      share|improve this answer
























        up vote
        5
        down vote













        You may find it more efficient to directly search for the files not owned by the user ...



        find /directory ! -user username -printf "%u %pn" 





        share|improve this answer






















          up vote
          5
          down vote










          up vote
          5
          down vote









          You may find it more efficient to directly search for the files not owned by the user ...



          find /directory ! -user username -printf "%u %pn" 





          share|improve this answer












          You may find it more efficient to directly search for the files not owned by the user ...



          find /directory ! -user username -printf "%u %pn" 






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 21 at 18:47









          rrauenza

          28113




          28113




















              up vote
              4
              down vote













              DIY method via Python:



              #!/usr/bin/env python3
              import sys,os,pwd
              for f in sys.argv[1:]:
              username = pwd.getpwuid(os.stat(f).st_uid).pw_name
              print( ":".join([f,username]) )


              This iterates over all filenames listed on command-line, gets UID of the file's owner, and using pwd module gets the username of the owner. After that, filename and username joined for pretty printing and separated via colon. Works as so:



              $ ./get_owners.py /etc/* 
              /etc/acpi:root
              /etc/adduser.conf:root
              /etc/alternatives:root
              . . .





              share|improve this answer
























                up vote
                4
                down vote













                DIY method via Python:



                #!/usr/bin/env python3
                import sys,os,pwd
                for f in sys.argv[1:]:
                username = pwd.getpwuid(os.stat(f).st_uid).pw_name
                print( ":".join([f,username]) )


                This iterates over all filenames listed on command-line, gets UID of the file's owner, and using pwd module gets the username of the owner. After that, filename and username joined for pretty printing and separated via colon. Works as so:



                $ ./get_owners.py /etc/* 
                /etc/acpi:root
                /etc/adduser.conf:root
                /etc/alternatives:root
                . . .





                share|improve this answer






















                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  DIY method via Python:



                  #!/usr/bin/env python3
                  import sys,os,pwd
                  for f in sys.argv[1:]:
                  username = pwd.getpwuid(os.stat(f).st_uid).pw_name
                  print( ":".join([f,username]) )


                  This iterates over all filenames listed on command-line, gets UID of the file's owner, and using pwd module gets the username of the owner. After that, filename and username joined for pretty printing and separated via colon. Works as so:



                  $ ./get_owners.py /etc/* 
                  /etc/acpi:root
                  /etc/adduser.conf:root
                  /etc/alternatives:root
                  . . .





                  share|improve this answer












                  DIY method via Python:



                  #!/usr/bin/env python3
                  import sys,os,pwd
                  for f in sys.argv[1:]:
                  username = pwd.getpwuid(os.stat(f).st_uid).pw_name
                  print( ":".join([f,username]) )


                  This iterates over all filenames listed on command-line, gets UID of the file's owner, and using pwd module gets the username of the owner. After that, filename and username joined for pretty printing and separated via colon. Works as so:



                  $ ./get_owners.py /etc/* 
                  /etc/acpi:root
                  /etc/adduser.conf:root
                  /etc/alternatives:root
                  . . .






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 21 at 16:41









                  Sergiy Kolodyazhnyy

                  64.9k9129282




                  64.9k9129282



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1026934%2fhow-to-get-a-list-of-all-owners-of-files-in-a-directory%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      GRUB: Fatal! inconsistent data read from (0x84) 0+xxxxxx

                      `kcmshell` modules relation with `/usr/share/applications`

                      How to enroll fingerprints to Ubuntu 17.10 with VFS491