7

I am trying to print the space occupied by directories (two levels down). The size listed is not accurate. For instance large_child_folder is 150 GB. How do I get the real directory size?

tree -L 2 -d --du -sh

.
├── [8.0K]  parent_folder
│   └── [4.0K]  large_child_folder
└── [8.0K]  another_parent_folder
    └── [4.0K]  another_large_child_folder

The answer @ Print size of directory content with tree command in tree 1.5? doesn't seem correct.

3 Answers 3

5

The following command will provide a human readable information, on the size of all directories which are up-to depth of 2 levels of directories below the current directory:

du --max-depth=2 -h

du - estimate file space usage - man

--max-depth=N print the total for a directory only if it is N or fewer levels below the command line argument

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

4

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
0

Command

$ tree --version;
# tree v2.1.1 © 1996 - 2023 by Steve Baker, Thomas Moore, Francesc Rocher,
# Florian Sesser, Kyosuke Tokoro
# -x - Stay on the current file-system only...
# -a - All files are printed... (those beginning with a dot `.')...
# -p - Print the file type and permissions for each file...
# -u - Print the username, or UID...
# -g - Print the group name, or GID...
# -h - Print the size of each file but in a more human readable way...
# -F - Append a `/' for directories, a `=' for socket files...
# -D - Print the date of the last modification time...
# --du - For each directory report its size as the accumulation of sizes...
# --dirsfirst - List directories before files...
# --charset - Set the character set to use when outputting...
# --sort - Sort the output by type instead of name...
# --timefmt - Prints... and formats the date according to the format string...
#
tree -xapughFD \
    --du --dirsfirst \
    --charset='ascii' --sort='size' --timefmt='%Y-%m-%d_%H-%M-%S' -- \
    . \
    | grep -P '^(?:\|\s{3}|\s{4}){0,1}(?:`|\|)\-\-';

In the grep regex, {0,1} (frankly, is similar to ?) may be used for the "depth". For example, {0,3} would be for the current and maximum of 3 directories into deep.

Example

$ cd /usr/share/ffmpeg/; pwd -P;
/usr/share/ffmpeg
$ tree -xahpugFD --du --charset=ascii --dirsfirst --sort=size --timefmt='%Y-%m-%d_%H-%M-%S' -- . | grep -P '^(?:\|\s{3}|\s{4}){0,1}(?:`|\|)\-\-';
|-- [drwxr-xr-x root     root     194K 2022-02-01_19-47-55]  examples/
|   |-- [-rw-r--r-- root     root      28K 2019-04-04_10-39-16]  transcode_aac.c
|   |-- [-rw-r--r-- root     root      21K 2019-04-04_10-39-16]  muxing.c
|   |-- [-rw-r--r-- root     root      20K 2019-04-04_10-39-16]  transcoding.c
|   |-- [-rw-r--r-- root     root      19K 2019-04-04_10-39-16]  decoding_encoding.c
|   |-- [-rw-r--r-- root     root      15K 2019-04-04_10-39-16]  demuxing_decoding.c
|   |-- [-rw-r--r-- root     root      14K 2019-04-04_10-39-16]  qsvdec.c
|   |-- [-rw-r--r-- root     root      12K 2019-04-04_10-39-16]  filter_audio.c
|   |-- [-rw-r--r-- root     root     9.9K 2019-04-04_10-39-16]  filtering_audio.c
|   |-- [-rw-r--r-- root     root     9.0K 2019-04-04_10-39-16]  filtering_video.c
|   |-- [-rw-r--r-- root     root     7.8K 2019-04-04_10-39-16]  resampling_audio.c
|   |-- [-rw-r--r-- root     root     5.6K 2019-04-04_10-39-16]  extract_mvs.c
|   |-- [-rw-r--r-- root     root     5.5K 2019-04-04_10-39-16]  avio_dir_cmd.c
|   |-- [-rw-r--r-- root     root     5.5K 2019-04-04_10-39-16]  remuxing.c
|   |-- [-rw-r--r-- root     root     5.1K 2019-04-04_10-39-16]  http_multiclient.c
|   |-- [-rw-r--r-- root     root     4.9K 2019-04-04_10-39-16]  scaling_video.c
|   |-- [-rw-r--r-- root     root     4.0K 2019-04-04_10-39-16]  avio_reading.c
|   |-- [-rw-r--r-- root     root     1.9K 2019-04-04_10-39-16]  metadata.c
|   |-- [-rw-r--r-- root     root     1.7K 2019-04-04_10-39-16]  Makefile
|   `-- [-rw-r--r-- root     root      888 2019-04-04_10-39-16]  README
|-- [-rw-r--r-- root     root      19K 2019-04-04_10-39-16]  ffprobe.xsd
|-- [-rw-r--r-- root     root      227 2019-04-04_10-39-16]  libvpx-1080p50_60.ffpreset
|-- [-rw-r--r-- root     root      227 2019-04-04_10-39-16]  libvpx-1080p.ffpreset
|-- [-rw-r--r-- root     root      227 2019-04-04_10-39-16]  libvpx-720p50_60.ffpreset
|-- [-rw-r--r-- root     root      227 2019-04-04_10-39-16]  libvpx-720p.ffpreset
`-- [-rw-r--r-- root     root      219 2019-04-04_10-39-16]  libvpx-360p.ffpreset

Source: https://unix.stackexchange.com/a/769124/133353

You must log in to answer this question.

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