97

In C-Shell, how can I get the same output as du -sh ./* but without listing the files in the root dir, i.e. just a list of subdirectories in ./ and the sizes of all their contents?

2
  • I don't get it. I tried sh and csh and except for ordering the output is the same. (I have to admit that I am actually using bash and tcsh.)
    – Shi
    Aug 12, 2011 at 22:02
  • @Shi I should clarify: the comment about C-Shell is just to specify what I'm using. I'm seeking another command or options that will give me the same result, but without listing the sizes of the files in ./
    – foglerit
    Aug 12, 2011 at 22:13

2 Answers 2

164

Add a trailing slash, like:

du -sh ./*/
4
  • 2
    This is one of the fastest Stack Exchange fixes I've had. +2 if I could.
    – Matthew
    Jan 3, 2016 at 0:08
  • 1
    Note: if the -s is dropped, it becomes recursive. Note: piping to sort -h will sort by the human-readable size (-h flag was introduced in GNU sort in 2009). Nov 22, 2017 at 11:21
  • @Evgeni, thanks for the mention about dropping -s makes it recursive. For some reason that was hard to find, but you had it. Jun 30, 2020 at 14:50
  • 1
    Show also the hidden directory : du -shc .[^.]* /* | sort -hr
    – avimimoun
    May 30, 2021 at 23:03
28

Duplicate answer as above just adding sort and flag to display size in a human-readable format

du -sh */ | sort -hr

Outputs:

44G     workspace/
24G     Downloads/
6.2G    Videos/
1.5G    Pictures/
189M    Music/
12M     Documents/
8.0K    Postman/
8.0K    Desktop/

You may also like to add a threshold

du -sh */ -t 100M | sort -hr

Outputs:

44G     workspace/
24G     Downloads/
6.2G    Videos/
1.5G    Pictures/
189M    Music/

man page for du and sort

DU(1)                                                                                        

NAME
       du - estimate file space usage

SYNOPSIS
       du [OPTION]... [FILE]...
       du [OPTION]... --files0-from=F

DESCRIPTION
       Summarize disk usage of the set of FILEs, recursively for directories.

       -h, --human-readable
              print sizes in human readable format (e.g., 1K 234M 2G)

       -s, --summarize
              display only a total for each argument

       -t, --threshold=SIZE
          exclude entries smaller than SIZE if positive, or entries greater than SIZE if negative


SORT(1)                                                                                          

NAME
       sort - sort lines of text files

SYNOPSIS
       sort [OPTION]... [FILE]...
       sort [OPTION]... --files0-from=F

DESCRIPTION
       Write sorted concatenation of all FILE(s) to standard output.

       -h, --human-numeric-sort
              compare human readable numbers (e.g., 2K 1G)

       -r, --reverse
              reverse the result of comparisons
2
  • 2
    Please note that using | sort -hr the output will be buffered, whereas df by itself outputs a stream as it works.
    – DustWolf
    Sep 11, 2020 at 10:38
  • good to know :+1: Thanks Sep 11, 2020 at 10:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .