Getting disk usage for each user












5














When I type sudo du -sh ~student1 I get the output I expect (see screenshot). However when I try to use the script below:



#!/bin/bash

for user in "$@"; do
sudo du -sh ~$user
done


to do the same thing, I get an error that the directory doesn't exist. Here is a screenshot.





(I know I can do sudo du -sh student1 student2..., and that the bash script is useless but I still don't understand why it doesn't work.)



I had a look at
Total disk usage for a particular user but it wasn't quite what I was looking for.










share|improve this question





























    5














    When I type sudo du -sh ~student1 I get the output I expect (see screenshot). However when I try to use the script below:



    #!/bin/bash

    for user in "$@"; do
    sudo du -sh ~$user
    done


    to do the same thing, I get an error that the directory doesn't exist. Here is a screenshot.





    (I know I can do sudo du -sh student1 student2..., and that the bash script is useless but I still don't understand why it doesn't work.)



    I had a look at
    Total disk usage for a particular user but it wasn't quite what I was looking for.










    share|improve this question



























      5












      5








      5







      When I type sudo du -sh ~student1 I get the output I expect (see screenshot). However when I try to use the script below:



      #!/bin/bash

      for user in "$@"; do
      sudo du -sh ~$user
      done


      to do the same thing, I get an error that the directory doesn't exist. Here is a screenshot.





      (I know I can do sudo du -sh student1 student2..., and that the bash script is useless but I still don't understand why it doesn't work.)



      I had a look at
      Total disk usage for a particular user but it wasn't quite what I was looking for.










      share|improve this question















      When I type sudo du -sh ~student1 I get the output I expect (see screenshot). However when I try to use the script below:



      #!/bin/bash

      for user in "$@"; do
      sudo du -sh ~$user
      done


      to do the same thing, I get an error that the directory doesn't exist. Here is a screenshot.





      (I know I can do sudo du -sh student1 student2..., and that the bash script is useless but I still don't understand why it doesn't work.)



      I had a look at
      Total disk usage for a particular user but it wasn't quite what I was looking for.







      linux bash user






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 26 '17 at 11:33









      Sathyajith Bhat

      52.6k29154252




      52.6k29154252










      asked Oct 26 '17 at 11:17









      kg98

      283




      283






















          3 Answers
          3






          active

          oldest

          votes


















          4














          The shell does a single pass on expanding parameters, so $user is expanded, but the preceding ~ isn't then expanded. In order to reparse the input line, use eval:



          ...
          eval sudo du -sh ~$user
          ...





          share|improve this answer

















          • 1




            True. It's because tilde expansion is done before variable expansion in bash, then neither sudo nor du expands tilde.
            – Kamil Maciorowski
            Oct 26 '17 at 11:40












          • Note that ~ is expanded in the user shell, so if permissions prevent the expansion of a particular user here, then the use of sudo won't override this.
            – AFH
            Oct 26 '17 at 12:07






          • 1




            @xenoid - The default option for a user's home directory is indeed /home/$user, but this can be overridden at account creation time, or modified later. It is also not the default for root, at least not on Ubuntu and (presumably) other Debian-derived distributions. If you use sudo outside the script, you will be running in a different environment from the calling shell, which has other implications: for instance, the script may not be in the $PATH which sudo uses.
            – AFH
            Oct 26 '17 at 13:29






          • 1




            @xenoid - Not true: the semicolon in the parameter will delimit the sudo command, and the rest of the command will run with user privileges. I am willing to accept that there may be places where eval could have unintended consequences, but this is not one of them. In any case, a user who has sudo privileges doesn't need to use something as obscure as this to do dangerous things, and users with sudo denied won't be able to run the script anyway.
            – AFH
            Oct 26 '17 at 16:21






          • 1




            I don't see the problem. If sudo is permitted, it doesn't matter if you run it on the same line in a script or as a separate command afterwards. I have answered your every comment and you have not proposed a workable alternative. This conversation is getting nowhere, so I am not going to continue it. Thank you for your comments, but we'll have to agree to differ.
            – AFH
            Oct 26 '17 at 22:47



















          0














          find . -printf "%u  %sn" | awk '{user[$1]+=$2}; END{ for( i in user) print i " " user[i]}'


          the same function



          find . -user user1 -type f -printf "%sn" | awk '{t+=$1}END{print t}'





          share|improve this answer























          • Welcome on Superuser. We are always happy to see some explanation of an answer. Maybe you could explain what happens.
            – davidbaumann
            Dec 4 at 11:43



















          0














          As mentioned by @AFH, the tilde (~) isn't expanded, and you could use eval to handle this...



          Or you could use getent to get a user's home directory:



          $ getent passwd attie | cut -d: -f6
          /home/attie


          This way it would be easier to detect an error (rather than a mysterious variable expansion issue).



          You could even use this to enumerate user directories:



          $ getent passwd | cut -d: -f6 | grep -E '^/home/'
          /home/attie
          /home/bill





          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fsuperuser.com%2fquestions%2f1262625%2fgetting-disk-usage-for-each-user%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            The shell does a single pass on expanding parameters, so $user is expanded, but the preceding ~ isn't then expanded. In order to reparse the input line, use eval:



            ...
            eval sudo du -sh ~$user
            ...





            share|improve this answer

















            • 1




              True. It's because tilde expansion is done before variable expansion in bash, then neither sudo nor du expands tilde.
              – Kamil Maciorowski
              Oct 26 '17 at 11:40












            • Note that ~ is expanded in the user shell, so if permissions prevent the expansion of a particular user here, then the use of sudo won't override this.
              – AFH
              Oct 26 '17 at 12:07






            • 1




              @xenoid - The default option for a user's home directory is indeed /home/$user, but this can be overridden at account creation time, or modified later. It is also not the default for root, at least not on Ubuntu and (presumably) other Debian-derived distributions. If you use sudo outside the script, you will be running in a different environment from the calling shell, which has other implications: for instance, the script may not be in the $PATH which sudo uses.
              – AFH
              Oct 26 '17 at 13:29






            • 1




              @xenoid - Not true: the semicolon in the parameter will delimit the sudo command, and the rest of the command will run with user privileges. I am willing to accept that there may be places where eval could have unintended consequences, but this is not one of them. In any case, a user who has sudo privileges doesn't need to use something as obscure as this to do dangerous things, and users with sudo denied won't be able to run the script anyway.
              – AFH
              Oct 26 '17 at 16:21






            • 1




              I don't see the problem. If sudo is permitted, it doesn't matter if you run it on the same line in a script or as a separate command afterwards. I have answered your every comment and you have not proposed a workable alternative. This conversation is getting nowhere, so I am not going to continue it. Thank you for your comments, but we'll have to agree to differ.
              – AFH
              Oct 26 '17 at 22:47
















            4














            The shell does a single pass on expanding parameters, so $user is expanded, but the preceding ~ isn't then expanded. In order to reparse the input line, use eval:



            ...
            eval sudo du -sh ~$user
            ...





            share|improve this answer

















            • 1




              True. It's because tilde expansion is done before variable expansion in bash, then neither sudo nor du expands tilde.
              – Kamil Maciorowski
              Oct 26 '17 at 11:40












            • Note that ~ is expanded in the user shell, so if permissions prevent the expansion of a particular user here, then the use of sudo won't override this.
              – AFH
              Oct 26 '17 at 12:07






            • 1




              @xenoid - The default option for a user's home directory is indeed /home/$user, but this can be overridden at account creation time, or modified later. It is also not the default for root, at least not on Ubuntu and (presumably) other Debian-derived distributions. If you use sudo outside the script, you will be running in a different environment from the calling shell, which has other implications: for instance, the script may not be in the $PATH which sudo uses.
              – AFH
              Oct 26 '17 at 13:29






            • 1




              @xenoid - Not true: the semicolon in the parameter will delimit the sudo command, and the rest of the command will run with user privileges. I am willing to accept that there may be places where eval could have unintended consequences, but this is not one of them. In any case, a user who has sudo privileges doesn't need to use something as obscure as this to do dangerous things, and users with sudo denied won't be able to run the script anyway.
              – AFH
              Oct 26 '17 at 16:21






            • 1




              I don't see the problem. If sudo is permitted, it doesn't matter if you run it on the same line in a script or as a separate command afterwards. I have answered your every comment and you have not proposed a workable alternative. This conversation is getting nowhere, so I am not going to continue it. Thank you for your comments, but we'll have to agree to differ.
              – AFH
              Oct 26 '17 at 22:47














            4












            4








            4






            The shell does a single pass on expanding parameters, so $user is expanded, but the preceding ~ isn't then expanded. In order to reparse the input line, use eval:



            ...
            eval sudo du -sh ~$user
            ...





            share|improve this answer












            The shell does a single pass on expanding parameters, so $user is expanded, but the preceding ~ isn't then expanded. In order to reparse the input line, use eval:



            ...
            eval sudo du -sh ~$user
            ...






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 26 '17 at 11:34









            AFH

            13.9k31938




            13.9k31938








            • 1




              True. It's because tilde expansion is done before variable expansion in bash, then neither sudo nor du expands tilde.
              – Kamil Maciorowski
              Oct 26 '17 at 11:40












            • Note that ~ is expanded in the user shell, so if permissions prevent the expansion of a particular user here, then the use of sudo won't override this.
              – AFH
              Oct 26 '17 at 12:07






            • 1




              @xenoid - The default option for a user's home directory is indeed /home/$user, but this can be overridden at account creation time, or modified later. It is also not the default for root, at least not on Ubuntu and (presumably) other Debian-derived distributions. If you use sudo outside the script, you will be running in a different environment from the calling shell, which has other implications: for instance, the script may not be in the $PATH which sudo uses.
              – AFH
              Oct 26 '17 at 13:29






            • 1




              @xenoid - Not true: the semicolon in the parameter will delimit the sudo command, and the rest of the command will run with user privileges. I am willing to accept that there may be places where eval could have unintended consequences, but this is not one of them. In any case, a user who has sudo privileges doesn't need to use something as obscure as this to do dangerous things, and users with sudo denied won't be able to run the script anyway.
              – AFH
              Oct 26 '17 at 16:21






            • 1




              I don't see the problem. If sudo is permitted, it doesn't matter if you run it on the same line in a script or as a separate command afterwards. I have answered your every comment and you have not proposed a workable alternative. This conversation is getting nowhere, so I am not going to continue it. Thank you for your comments, but we'll have to agree to differ.
              – AFH
              Oct 26 '17 at 22:47














            • 1




              True. It's because tilde expansion is done before variable expansion in bash, then neither sudo nor du expands tilde.
              – Kamil Maciorowski
              Oct 26 '17 at 11:40












            • Note that ~ is expanded in the user shell, so if permissions prevent the expansion of a particular user here, then the use of sudo won't override this.
              – AFH
              Oct 26 '17 at 12:07






            • 1




              @xenoid - The default option for a user's home directory is indeed /home/$user, but this can be overridden at account creation time, or modified later. It is also not the default for root, at least not on Ubuntu and (presumably) other Debian-derived distributions. If you use sudo outside the script, you will be running in a different environment from the calling shell, which has other implications: for instance, the script may not be in the $PATH which sudo uses.
              – AFH
              Oct 26 '17 at 13:29






            • 1




              @xenoid - Not true: the semicolon in the parameter will delimit the sudo command, and the rest of the command will run with user privileges. I am willing to accept that there may be places where eval could have unintended consequences, but this is not one of them. In any case, a user who has sudo privileges doesn't need to use something as obscure as this to do dangerous things, and users with sudo denied won't be able to run the script anyway.
              – AFH
              Oct 26 '17 at 16:21






            • 1




              I don't see the problem. If sudo is permitted, it doesn't matter if you run it on the same line in a script or as a separate command afterwards. I have answered your every comment and you have not proposed a workable alternative. This conversation is getting nowhere, so I am not going to continue it. Thank you for your comments, but we'll have to agree to differ.
              – AFH
              Oct 26 '17 at 22:47








            1




            1




            True. It's because tilde expansion is done before variable expansion in bash, then neither sudo nor du expands tilde.
            – Kamil Maciorowski
            Oct 26 '17 at 11:40






            True. It's because tilde expansion is done before variable expansion in bash, then neither sudo nor du expands tilde.
            – Kamil Maciorowski
            Oct 26 '17 at 11:40














            Note that ~ is expanded in the user shell, so if permissions prevent the expansion of a particular user here, then the use of sudo won't override this.
            – AFH
            Oct 26 '17 at 12:07




            Note that ~ is expanded in the user shell, so if permissions prevent the expansion of a particular user here, then the use of sudo won't override this.
            – AFH
            Oct 26 '17 at 12:07




            1




            1




            @xenoid - The default option for a user's home directory is indeed /home/$user, but this can be overridden at account creation time, or modified later. It is also not the default for root, at least not on Ubuntu and (presumably) other Debian-derived distributions. If you use sudo outside the script, you will be running in a different environment from the calling shell, which has other implications: for instance, the script may not be in the $PATH which sudo uses.
            – AFH
            Oct 26 '17 at 13:29




            @xenoid - The default option for a user's home directory is indeed /home/$user, but this can be overridden at account creation time, or modified later. It is also not the default for root, at least not on Ubuntu and (presumably) other Debian-derived distributions. If you use sudo outside the script, you will be running in a different environment from the calling shell, which has other implications: for instance, the script may not be in the $PATH which sudo uses.
            – AFH
            Oct 26 '17 at 13:29




            1




            1




            @xenoid - Not true: the semicolon in the parameter will delimit the sudo command, and the rest of the command will run with user privileges. I am willing to accept that there may be places where eval could have unintended consequences, but this is not one of them. In any case, a user who has sudo privileges doesn't need to use something as obscure as this to do dangerous things, and users with sudo denied won't be able to run the script anyway.
            – AFH
            Oct 26 '17 at 16:21




            @xenoid - Not true: the semicolon in the parameter will delimit the sudo command, and the rest of the command will run with user privileges. I am willing to accept that there may be places where eval could have unintended consequences, but this is not one of them. In any case, a user who has sudo privileges doesn't need to use something as obscure as this to do dangerous things, and users with sudo denied won't be able to run the script anyway.
            – AFH
            Oct 26 '17 at 16:21




            1




            1




            I don't see the problem. If sudo is permitted, it doesn't matter if you run it on the same line in a script or as a separate command afterwards. I have answered your every comment and you have not proposed a workable alternative. This conversation is getting nowhere, so I am not going to continue it. Thank you for your comments, but we'll have to agree to differ.
            – AFH
            Oct 26 '17 at 22:47




            I don't see the problem. If sudo is permitted, it doesn't matter if you run it on the same line in a script or as a separate command afterwards. I have answered your every comment and you have not proposed a workable alternative. This conversation is getting nowhere, so I am not going to continue it. Thank you for your comments, but we'll have to agree to differ.
            – AFH
            Oct 26 '17 at 22:47













            0














            find . -printf "%u  %sn" | awk '{user[$1]+=$2}; END{ for( i in user) print i " " user[i]}'


            the same function



            find . -user user1 -type f -printf "%sn" | awk '{t+=$1}END{print t}'





            share|improve this answer























            • Welcome on Superuser. We are always happy to see some explanation of an answer. Maybe you could explain what happens.
              – davidbaumann
              Dec 4 at 11:43
















            0














            find . -printf "%u  %sn" | awk '{user[$1]+=$2}; END{ for( i in user) print i " " user[i]}'


            the same function



            find . -user user1 -type f -printf "%sn" | awk '{t+=$1}END{print t}'





            share|improve this answer























            • Welcome on Superuser. We are always happy to see some explanation of an answer. Maybe you could explain what happens.
              – davidbaumann
              Dec 4 at 11:43














            0












            0








            0






            find . -printf "%u  %sn" | awk '{user[$1]+=$2}; END{ for( i in user) print i " " user[i]}'


            the same function



            find . -user user1 -type f -printf "%sn" | awk '{t+=$1}END{print t}'





            share|improve this answer














            find . -printf "%u  %sn" | awk '{user[$1]+=$2}; END{ for( i in user) print i " " user[i]}'


            the same function



            find . -user user1 -type f -printf "%sn" | awk '{t+=$1}END{print t}'






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 4 at 12:50









            davidbaumann

            1,842722




            1,842722










            answered Dec 4 at 10:28









            Joniale

            1




            1












            • Welcome on Superuser. We are always happy to see some explanation of an answer. Maybe you could explain what happens.
              – davidbaumann
              Dec 4 at 11:43


















            • Welcome on Superuser. We are always happy to see some explanation of an answer. Maybe you could explain what happens.
              – davidbaumann
              Dec 4 at 11:43
















            Welcome on Superuser. We are always happy to see some explanation of an answer. Maybe you could explain what happens.
            – davidbaumann
            Dec 4 at 11:43




            Welcome on Superuser. We are always happy to see some explanation of an answer. Maybe you could explain what happens.
            – davidbaumann
            Dec 4 at 11:43











            0














            As mentioned by @AFH, the tilde (~) isn't expanded, and you could use eval to handle this...



            Or you could use getent to get a user's home directory:



            $ getent passwd attie | cut -d: -f6
            /home/attie


            This way it would be easier to detect an error (rather than a mysterious variable expansion issue).



            You could even use this to enumerate user directories:



            $ getent passwd | cut -d: -f6 | grep -E '^/home/'
            /home/attie
            /home/bill





            share|improve this answer


























              0














              As mentioned by @AFH, the tilde (~) isn't expanded, and you could use eval to handle this...



              Or you could use getent to get a user's home directory:



              $ getent passwd attie | cut -d: -f6
              /home/attie


              This way it would be easier to detect an error (rather than a mysterious variable expansion issue).



              You could even use this to enumerate user directories:



              $ getent passwd | cut -d: -f6 | grep -E '^/home/'
              /home/attie
              /home/bill





              share|improve this answer
























                0












                0








                0






                As mentioned by @AFH, the tilde (~) isn't expanded, and you could use eval to handle this...



                Or you could use getent to get a user's home directory:



                $ getent passwd attie | cut -d: -f6
                /home/attie


                This way it would be easier to detect an error (rather than a mysterious variable expansion issue).



                You could even use this to enumerate user directories:



                $ getent passwd | cut -d: -f6 | grep -E '^/home/'
                /home/attie
                /home/bill





                share|improve this answer












                As mentioned by @AFH, the tilde (~) isn't expanded, and you could use eval to handle this...



                Or you could use getent to get a user's home directory:



                $ getent passwd attie | cut -d: -f6
                /home/attie


                This way it would be easier to detect an error (rather than a mysterious variable expansion issue).



                You could even use this to enumerate user directories:



                $ getent passwd | cut -d: -f6 | grep -E '^/home/'
                /home/attie
                /home/bill






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 4 at 12:56









                Attie

                10.9k32444




                10.9k32444






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


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

                    But avoid



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

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


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





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


                    Please pay close attention to the following guidance:


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

                    But avoid



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

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


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




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1262625%2fgetting-disk-usage-for-each-user%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Terni

                    A new problem with tex4ht and tikz

                    Sun Ra