List the current folder folder's sizes with the terminal?

142

56

I want a list of the folders from the current directory or one that I specify with their size.

I have tried with du but I only get the size of the directories I specify (du . ./f1), and ls doesn't show the size of the folders.

How do I do this without any scripting ?

kevin

Posted 2010-08-03T17:58:26.630

Reputation: 1 654

not to sound like a jerk, but 'man du' ;) – Jakub – 2010-08-03T19:47:11.020

5Thanks ;-) but I've already tried and couldn't find anything. And try Googling du! – kevin – 2010-08-05T06:39:36.357

Answers

223

If you want to show all the directories in the current directory:

$ du -sh */
788K    foo/
500K    bar/
931K    baz/

To show them starting from another directory:

$ du -sh /path/to/dir/*/
48K     /path/to/dir/dir1/
4.0K    /path/to/dir/dir2/
6.7M    /path/to/dir/dir3/
20K     /path/to/dir/dir4/
8.0K    /path/to/dir/dir5/
44K     /path/to/dir/dir6/

If you want to make sure directories with names starting with a dot are included do shopt -s dotglob first.

Paused until further notice.

Posted 2010-08-03T17:58:26.630

Reputation: 86 075

Thanks all great answers but this one is the simplest one.

I forgot the ending / in my tests. – kevin – 2010-08-05T06:43:27.957

1Where -s means summarise, display the total and -h means human readable – wranvaud – 2017-10-30T12:53:08.460

3Sort folders by size: du -s */ | sort -n. – Anton Tarasenko – 2018-11-01T16:28:06.827

1

@AntonTarasenko: Sort human readable sizes

– Paused until further notice. – 2018-11-01T20:58:33.913

Including hidden (dot) directories: https://askubuntu.com/a/356915/176799

– Anton Tarasenko – 2018-11-04T10:52:09.310

1

@AntonTarasenko: shopt -s dotglob doc

– Paused until further notice. – 2018-11-04T15:25:21.567

-n does not sorts properly use du -sh */ | sort -hr instead – Deepak Mahakale – 2020-02-14T14:19:11.303

19

On a Mac, the --max-depth option is supplanted by -d [depth]. So, to see a human readable listing of your root drive plus 2 levels deep use the following:

du -hd 2 /* 

Note: this command will expose the top two directory levels off your root. This includes traversing one level into your Volumes, and will list the summary sizes of each top-level directory in each of your attached volumes. Depending on what you have attached, this command could take some time to complete.

jadik

Posted 2010-08-03T17:58:26.630

Reputation: 299

14

Another aproach is the --max-depth option.

du -h --max-depth=1 .

Will list all directories and files under the current folder with size.

Depth 2 would list one more level of folders.

matthias krull

Posted 2010-08-03T17:58:26.630

Reputation: 2 394

4On Mac, it's du -hd 2 . for 2 levels of depth. – Ryan – 2014-08-02T02:12:41.793

Ah yes. And probably on FreeBSD as well. Thanks for the info. – matthias krull – 2014-08-07T11:25:58.563

5

Try:

$ du -s ./f1

or

$ du -sh ./f1

for more friendly readable sizes.

Doug Harris

Posted 2010-08-03T17:58:26.630

Reputation: 23 578

4

Building on the accepted answer, this command will show you the sizes of the folders in the directory, and will also list them by size for you to interpret easier:

$ du -sh */ | sort -rn

Ethan

Posted 2010-08-03T17:58:26.630

Reputation: 161

2

Worth to mention the NCurses Disk Usage shell command.

Ncdu is a disk usage analyzer with an ncurses interface. It is designed to find space hogs on a remote server where you don't have an entire graphical setup available, but it is a useful tool even on regular desktop systems. Ncdu aims to be fast, simple and easy to use, and should be able to run in any minimal POSIX-like environment with ncurses installed.

Ray

Posted 2010-08-03T17:58:26.630

Reputation: 249

2

$ du --max-depth=1 /var/www/ | sort -n -r

Andre Mesquita

Posted 2010-08-03T17:58:26.630

Reputation: 121

3When adding an answer to an older question with existing answers it is good practice to explain how your answer is different and include some explanation so that it isn't a command only answer. – Jason Aller – 2015-06-30T16:41:11.040

3Can you expand yourt answer to explain what the parameters do? – fixer1234 – 2015-06-30T18:59:01.037

1

Here is a POSIX script that will work with:

  • A file
  • Files
  • A directory
  • Directories
#!/bin/sh
ls -ARgo "$@" | awk '{q += $3} END {print q}'

Source

Steven Penny

Posted 2010-08-03T17:58:26.630

Reputation: 7 294

1

On Mac, you can install the GNU (Linux) implementation of du with Homebrew (brew install coreutils). Then for example:

gdu folder -shL --exclude=.git

where

  • gdu is the name given to the GNU implementation of du (by default Homebrew does not hide /usr/bin/du);
  • s produces a grand total for the folder specified (omit if you want to see the breakdown);
  • h outputs human-readable sizes;
  • L follows symlinks;
  • --exclude=.git excludes the git directory within the specified folder (this is just an example).

You can ignore more folders by adding --exclude=blah. You can also specify several folders at once (ie gdu folder1 folder2 ...), and in that case, you can combine all the subtotals into a single size using option c.

Jonathan H

Posted 2010-08-03T17:58:26.630

Reputation: 111