32

I'm wondering if we can combine the honesty of 'du' with the indented formatting of 'tree'. If I want a listing of the sizes of directories:

du -hx -d2

...displays two levels deep and all the size summaries are honest, but there's no indenting of subdirs. On the other hand:

tree --du -shaC -L 2

...indents and colorizes nicely however the reported sizes are a lie. To get the real sizes one must:

tree --du -shaC

...which is to say that you only get the true sizes if you let 'tree' show you the entire directory structure. I'd like to be able to always have correct size summaries regardless of how many levels of subdirs I want to actually display. I often do this:

tree -du -shaC | grep "\[01;34m"

... which prunes out everything but directories, and indents them nicely ... but there's no easy way to limit the display to just a given number levels (without the summaries lying). Is there a way? Perhaps I've missed the correct switches ...

terdon
  • 234,489
  • 66
  • 447
  • 667
Ray Andrews
  • 2,125
  • 4
  • 20
  • 37
  • 1
    I wish there was a tool that visualizes `du` output (i.e. doesn't need to do its own scanning, but you can just save the ouptut of `du` and pipe it into a graphical tool). – Sridhar Sarnobat May 08 '20 at 21:11
  • The last command is missing a dash (should be `--du` not `-du`)! If you could edit it that'd be great! – Shayan Mar 12 '22 at 18:02

5 Answers5

16

Also checkout ncdu: http://dev.yorhel.nl/ncdu

Its page also lists other "similar projects":

gt5 - Quite similar to ncdu, but a different approach.

tdu - Another small ncurses-based disk usage visualization utility.

TreeSize - GTK, using a treeview.

Baobab - GTK, using pie-charts, a treeview and a treemap. Comes with GNOME.

GdMap - GTK, with a treemap display.

Filelight - KDE, using pie-charts.

QDirStat - KDE, with a treemap display.

QDiskUsage - Qt, using pie-charts.

xdiskusage - FLTK, with a treemap display.

fsv - 3D visualization.

Philesight - Web-based clone of Filelight.

Mbo42
  • 103
  • 5
11

You don't need to grep for the colour code, the -d option is list directories only.

This seems to do what you want:

$ tree --du -d -shaC | grep -Ev '(  *[^ ]* ){2}\['
.
├── [  18]  dir1
├── [  30]  dir2
├── [ 205]  junk
│   ├── [  18]  dir1
│   ├── [  30]  dir2
│   └── [  76]  dir3
├── [ 119]  merge
└── [  20]  stuff

 4.4K used in 10 directories

The grep command removes all lines that have (one or more spaces followed by a non-space followed by a space) twice, followed by a [.

If you want a depth of 1, change the bound count inside the {} curly braces to {1} rather than {2}. same if you want a depth of 3, change it to {3}.

You can turn this into a shell function, like so:

mytreedu() {
  local depth=''

  while getopts "L:" opt ; do
      case "$opt" in
          L) depth="$OPTARG" ;;
      esac
  done

  shift "$((OPTIND-1))"

  if [ -z "$depth" ] ; then
      tree --du -d -shaC "$@"
  else   
      local PATTERN='(  *[^ ]* ){'"$depth"'}\['
      tree --du -d -shaC "$@" | grep -Ev "$PATTERN"
  fi
}

This uses getopts to "steal" any -L option and its argument from the tree command line, if there is one. If there isn't a -L n option on the command line, then that works too.

All other options and args are passed to the tree command.

The local PATTERN=... line isn't really necessary. I only did it like that to make sure that it would fit on one line and not word-wrap here on U&L. The regular expression could and probably should just go directly on the tree | grep ... line.

Run it like this:

mytreedu 

or

mytreedu -L 2 /path/to/dir/
terdon
  • 234,489
  • 66
  • 447
  • 667
cas
  • 1
  • 7
  • 119
  • 185
  • 3
    I love the code, but repeat that you can't use the '-d' because if you do, the size summaries are incorrect, or at least they are here. The size will be reported always as '4096' which is the size of the entry for the dir itself, but not the size of all it's contents. – Ray Andrews Oct 25 '15 at 15:41
  • ... should have said '4096' for each directory under the current directory ... but you don't get the sizes of the dir including it's files. – Ray Andrews Oct 25 '15 at 15:48
  • you only mentioned `-L` as being a problem, didn't mention `-d` at all. Now that I look more closely at the numbers reported, neither `tree --du` nor `tree --du -d` report sizes that in any way resemble those reported by `du`. – cas Oct 25 '15 at 19:47
  • Point being that anything less than a 100% display will not give you correct sizes. You could limit via '-d' or '-L 2' or whatever else--it it isn't shown, it isn't counted in the size. – Ray Andrews Oct 26 '15 at 00:17
  • `tree --du` doesn't seem to give correct sizes for directories anyway, with or without `-d` or `-L`. I have no idea what the numbers are supposed to be, but they're unrelated to what `du` reports. – cas Oct 26 '15 at 00:30
  • This does not correctly summarize the cumulative space taken by all files, as `du` does. – sancho.s ReinstateMonicaCellio Jul 13 '20 at 10:38
8

You can use dutree

enter image description here

  • coloured output, according to the LS_COLORS environment variable.
  • display the file system tree
  • ability to aggregate small files
  • ability to exclude files or directories
  • ability to compare different directories
  • fast, written in Rust
MrPaulch
  • 105
  • 4
nachoparker
  • 879
  • 11
  • 7
3

There isn't any perfect command tool to do this, But I found two ways that are closly.

  • shows both folders and files' size, but not showing in a tree mode.

    du -ah --max-depth=1 /var/log

  • shows in tree mode but only files' size, the folders are in counts

    tree -ah /var/log -L 1

Valiant Jiang
  • 251
  • 2
  • 3
0

Inspired by cas, I'm now doing this:

treee ()
{
    integer levels=$(( ($1 + 1) * 4 ))
    tree --du -shaC | grep "\[01;34m" | grep -Ev "^[^\[]{$levels}\[*"
    du -sh .
}
Ray Andrews
  • 2,125
  • 4
  • 20
  • 37
  • if you're going to throw away all the getopts stuff, you should at least still have `"$@"` immediately after the `-shaC`. otherwise that function is hard-coded to work for the current directory only. – cas Oct 25 '15 at 19:50
  • Once we got the thing working, I was going to ask you about that: please elaborate. Right about "$@" of course, but so far I only ever use it in the current dir, so haven't noticed that yet. All this 'getopts' stuff is new to me, I'd like to know what you are thinking there. – Ray Andrews Oct 26 '15 at 00:14
  • One of the benefits of using `getopts` is that options can appear in any order on the command line. The initial version of the `mytree` function I wrote used "$1" just as yours did, so the depth argument **had to be** the first argument, and it was not optional. I decided that wasn't good enough so used the bash-builtin `getopts` to process the `-L` option. This allowed the `-L n` option to appear anywhere on the command line. It also allowed it to be completely optional. – cas Oct 26 '15 at 00:24
  • another way of looking at it is that `getopts` allows you to write scripts that take real options and arguments (rather than just args in hard-coded positions like $1 $2 $3 etc), just like most other programs on your system. And if you use the `getopt` program (note that's without an `s`) from the `util-linux` package you can use both short single-letter options (e.g. `-l`) and long options (like `--long`) just like GNU programs. – cas Oct 26 '15 at 00:26
  • Ah ... it comes back to me now. Yes, I actually used that once. Must take another look at it. Thanks. – Ray Andrews Oct 26 '15 at 06:54