我正在尝试打印目录占用的空间(向下两级)。列出的尺寸不准确。例如large_child_folder
150GB。如何获取真实的目录大小?
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
答案 @在tree 1.5中使用tree命令打印目录内容的大小?似乎不正确。
答案1
以下命令将提供人类可读的信息,有关所有目录的大小,这些目录的深度最多为当前目录下 2 级目录的深度:
du --max-depth=2 -h
--max-depth=N 仅当目录低于命令行参数 N 或更少级别时才打印目录的总数
-h, --人类可读格式的打印尺寸(例如,1K 234M 2G)
答案2
答案3
命令
$ 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}(?:`|\|)\-\-';
在grep
正则表达式中,{0,1}
(坦率地说,类似于?
)可以用于“深度”。例如,{0,3}
将当前目录和最多 3 个目录放入深层。
例子
$ 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