1

I am trying to list out from the servers, and I was wondering if it is possible to be done through any terminal coding (in which I am pretty weak in it now)

Example, in the main directory there are tons of sub-folders (level 2) and within them are also tons of sub folders (level 3 deep?)

As such, I was wondering if normal coding in the terminal will gives me:

  • which user is occupying the most space (level 3 deep), while displaying the folders as well
  • the top 10 people that is occupying the most space (level 2 deep)

Main directory:

|- sub_lvl_2_A
    |- sub_lvl_3_A
|- sub_lvl_2_B
    |- sub_lvl_3_B
    |- sub_lvl_3_C
2

The du command recursively finds the estimated size of directories. It won't sort the output by size, though.

(You need to be root for this to work)

Really, you just alter the argument to its -d (max depth) option:

To print the size of each user's home directory in descending order by size:

sudo du -d 1 /home | sort -gr

To print the sizes of folders within each user's home directory (level 3) (warning, this is bound to be really long, you might want to pipe it to less):

sudo du -d 2 /home

(if output is long):

sudo du -d 2 /home | less

If you want to sort the subdirectories of each user by size, then you would have to run du individually for each user:

sudo du -d 1 ~user1 | sort -gr
sudo du -d 1 ~user2 | sort -gr

...and so on.

If you want to work from a different starting point, just change the last argument of du (/home above) to the directory you want to work from. Changing the argument to -d will change how many levels it recurses through.

| improve this answer | |
  • Thanks for getting back to me. Do you know if there are ways to sort it by users? – xenas Jul 14 '14 at 14:59
  • sudo du -d 1 /home | sort -g will sort the home folders in ascending order. – hal7df Jul 14 '14 at 15:02
  • sudo du -d 1 /home | sort -gr will sort the home folders in descending (large > small) order. – hal7df Jul 14 '14 at 15:05
  • Without being piped to sort, du will output the directories in the order it finds them (do, in this case, by user). However, this means that you can't sort the folders within directories by size all in one go - in that case, you would have to run du individually for each user and sort from there (you would only need to do this for your 3rd level scenario). – hal7df Jul 14 '14 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.