16

I would like to see the total disk usage for myself on a particular file system. I executed the command

du -h ~my_user_name

However, this lists every directory owned by my_user_name. I would like to get the sum total of all of this information. What is the appropriate option to pass? I tried

du -h -c ~my_user_name

but that did not work.

crisron
  • 139
  • 2
  • 11
Alex
  • 609
  • 2
  • 7
  • 15

3 Answers3

14

Passing -s to du will restrict the output to only the items specified on the command line.

du -sh ~
Ignacio Vazquez-Abrams
  • 111,361
  • 10
  • 201
  • 247
11

Du will only show you the totals per folder, not per user.

That might work if you want the total size of, say, /home/example_user/ and if only that example_user has files in that folder. If other users have files in them then this will not yield size of all files owned by you, but the total size of all files in that folder.

To get the information per user, either:

  1. If you have quota's enabled, use those commands.
  2. Use find to walk though all the directories you want to count your files in. Use the uid to only select your files and keep an associative array in awk to count the totals.

find /path/to/search/ -user username_whos_files_to_count -type f -printf "%s\n" | awk '{t+=$1}END{print t}'

Note, this uses a GNU find specific extension.

  • The first command searches past all files and directories in /path/to/search/.
  • -type f makes sure you only select files, otherwise you are also counting the size of directories. (Try making an empty folder. It will probably use 4k diskspace).
  • -user username_whos_files_to_count only selects the results from one user
  • -printf "%s\n" will print the size.

If you just run the first part of this, you will get a list of numbers. Those are the file sizes. (Everything else is stripped, only the size is printed thanks to the %s print command.)

We can then add all those numbers to get a summary. In the example, this is done with awk.

Richlv
  • 363
  • 3
  • 12
Hennes
  • 64,768
  • 7
  • 111
  • 168
  • +1 good point, thanks! the answer below actually was exactly what i wanted. the user/folder distinction doesn't matter that much in my case – Alex May 18 '13 at 20:48
  • Nice. `du -sch` is an easy command often used. Tracking down who owned what when things were mixed is a lot harder though it can be done as a one liner. I used it once, but I had trouble reconstructing it today. – Hennes May 18 '13 at 20:55
  • Helped me a lot, Perfect Explanation – Babin Lonston Mar 29 '14 at 08:07
  • Thanks! This answer saved me. Had trouble figuring out why "du" and "quota" show different results in a user's home folder. Apparently other files are owned by other people. After changing ownership to the home user, (https://superuser.com/a/648166/223634), "du" and "quota" now show the same size on home folder as expected. – alds Jul 23 '20 at 20:20
2

To find all the use by a specific user, a good command is:

find -user $USER -type f -exec du -chs {} +

You can further modify depending on specific needs, for example I often want to summarize use by folder, and the following works well:

find . -maxdepth 1 -user $USER -type d ! -path . -exec du -chs {} +

This finds only directories on one level, limits by user, excludes the parent directory, and prints each directory and a summary at the end.

asherkhb
  • 76
  • 2