一个显示磁盘使用情况的工具。(Linux、MacOS 和 Windows)

@Ta 2023-01-26 162点击
一个显示磁盘使用情况的工具。(Linux、MacOS 和 Windows)
GitHub: https://github.com/chenquan/diskusage
image.png(45.43 KB)

$ diskusage -h
A tool for showing disk usage.

GitHub: https://github.com/chenquan/diskusage
Issues: https://github.com/chenquan/diskusage/issues

Usage:
  diskusage [flags]

Examples:
1.The maximum display unit is GB: diskusage -u G
2.Only files named doc or docx are counted:
  a.diskusage -t doc,docx
  b.diskusage -f ".+\.(doc|docx)$"
3.Supports color output to pipeline:
  a.diskusage -c always | less -R
  b.diskusage -c always | more
4.Displays a 2-level tree structure: diskusage -d 2
5.Specify the directory /usr: diskusage --dir /usr
6.Export disk usage to file: diskusage > diskusage.txt

Flags:
  -a, --all             display all directories, otherwise only display folders whose usage size is not 0
  -c, --color string    set color output mode. optional: auto, always, ignore (default "auto")
  -d, --depth int       shows the depth of the tree directory structure (default 1)
      --dir string      directory path (default "./")
  -f, --filter string   regular expressions are used to filter files
  -h, --help            help for diskusage
  -l, --limit int       limit the number of files and directories displayed (default 9223372036854775807)
  -t, --type strings    only count certain types of files  (default all)
  -u, --unit string     displayed units. optional: B(Bytes), K(KB), M(MB), G(GB), T(TB) (default "M")
  -v, --version         version for diskusage
  -w, --worker int      number of workers searching the directory (default 32)


回复列表(6|隐藏机器人聊天)
  • @Ta / 2023-01-27 / /

    @alpha-time,啥时候显示红色,啥时候显示绿色呢?

  • @Ta / 2023-01-27 / /
    被锁定
    层主 @alpha-time 于 2023-01-27 13:03 删除了该楼层。
  • @Ta / 2023-01-27 / /
    @无名啊
    文件夹的大小和占用率是红色
    文件夹的名称是绿色

    喜欢的话,给个star哦
  • @Ta / 2023-01-27 / /
    @卷心菜,喜欢的话给个star哦
  • @Ta / 2023-01-28 / /

    @alpha-time,看着有趣,用命令行小工具 jq 也写了个

    命令行 jq 版 diskusage

    效果图

    (左边优先置顶了目录)
    out.webp(26.34 KB)

    使用

    [depth=1] diskusage_jq [path]
    

    代码

    #!/bin/bash
    
    diskusage_jq() {
    	find "${1:-.}" -printf "%y/%s/./%P\n" |
    	sed 's|/$||' |
    	jq -nrR --arg path "${1:-.}" --arg max_depth "$((${depth:-1} < 0 ? 1 << 30 : ${depth:-1}))" '
    	def round($n):
    		[., pow(10; $n)] | .[0] *= .[1] | (.[0] / .[1] | floor | tostring) +
    		if $n > 0 then (.[0] % .[1] | tostring) | "." + "0" * ($n - length) + . else "" end;
    
    	def humanSize:
    		[., 0] | until(.[0] < 1000; [.[0] / 1024, .[1] + 1]) |
    		(.[0] | round(1)) + "BKMGTP"[.[1]: .[1] + 1];
    
    	def tree($self; $depth; $idx; $len):
    		if $depth > 0 then
    			[["    ", "│   "], ["└── ", "├── "]]
    			[if $self then 1 else 0 end]
    			[if $idx + 1 < $len then 1 else 0 end]
    		else
    			""
    		end;
    
    	def f($depth; $prefix):
    		[.[]]  # 先提取 object 的 values,转成 array
    		| length as $len
    		| sort_by(.type != "d", -.size, .name) | . as $nodes  # 按是否目录、大小逆序、名称排序
    		| range($len) | . as $i  # 逐一遍历该层所有子项
    		| $nodes[$i] |
    		{type, size, name, prefix: ($prefix + tree(true; $depth; $i; $len))},  # 生成文件信息
    		(.children | f($depth + 1; $prefix + tree(false; $depth; $i; $len)));  # DFS 递归
    
    	reduce inputs as $line (
    		{};
    		# 在每一 / 处添加 "children",方便 get/setpath
    		($line | gsub("/"; "/children/") | split("/")) as $f
    		| ($f | length) as $len
    		| reduce range(4; [$len, 4 + 2 * ($max_depth | tonumber + 1)] | min; 2) as $i (
    			# 从根目录开始,逐层遍历,直到叶节点或最大深度限制
    			.;
    			$f[4:$i+1] as $path
    			| (getpath($path) // {children: {}}) as $item  # 获取该节点原来的信息。没有则生成
    			| setpath($path; $item + {  # 更新该节点信息
    				name: $path[-1],
    				size: ($item.size + ($f[2] | tonumber)),
    				type: (if $i + 1 < $len then $item.type else $f[0] end),
    			})
    		)
    	)
    	
    	# 为每个文件/目录条目,计算大小、百分比、树形状、颜色信息,并生成
    	| .[].size as $total
    	| f(0; "")
    	| (.size | humanSize) as $size
    	| ((.size * 100 / $total | round(1)) + "%") as $pct |
    	[
    		"\u001B[", if .type == "d" then 31 else 0 end, "m",
    		" " * (6 - ($size | length)), $size, " ",
    		" " * (6 - ($pct | length)), $pct, " ",
    		"\u001B[0m",
    		.prefix,
    		"\u001B[", if .type == "d" then 32 else 0 end, "m",
    		if .name != "." then .name else $path end,
    		"\u001B[0m"
    	]
    	| join("")
    	'
    }
    

    感觉很久都不想再碰 jq

  • @Ta / 2023-01-28 / /
    上英语课的既视感!牛批
添加新回复
回复需要登录