在 Linux 中,名称与正则表达式匹配的文件的磁盘使用情况? [英] Disk usage of files whose names match a regex, in Linux?

查看:14
本文介绍了在 Linux 中,名称与正则表达式匹配的文件的磁盘使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在许多情况下,我想知道我的磁盘空间有多少被什么使用,所以我知道要摆脱什么、转换为另一种格式、存储在其他地方(例如数据 DVD)、移动到另一个分区等.在这种情况下,我正在查看来自 的标准 Linux,我如何获取这些信息?p>

输出旨在被脚本解析.

解决方案

我建议类似:find .-regex '.*.bak' -print0 |du --files0-from=- -ch |尾 -1

一些注意事项:

  • find-print0 选项和 du--files0-from 选项可避免出现问题文件名中有空格
  • 正则表达式匹配整个路径,例如./dir1/subdir2/file.bak,而不仅仅是file.bak,所以如果你修改它,请考虑到这一点
  • 我使用 h 标志为 du 生成人类可读"格式,但如果您想解析输出,最好使用 k(总是使用千字节)
  • 如果您删除 tail 命令,您将另外看到特定文件和目录的大小

旁注:FileLight 是一个很好的 GUI 工具,用于找出谁占用了您的磁盘空间.它不执行正则表达式,但对于查找阻塞磁盘的大目录或文件非常方便.

So, in many situations I wanted a way to know how much of my disk space is used by what, so I know what to get rid of, convert to another format, store elsewhere (such as data DVDs), move to another partition, etc. In this case I'm looking at a Windows partition from a SliTaz Linux bootable media.

In most cases, what I want is the size of files and folders, and for that I use NCurses-based ncdu:

                

But in this case, I want a way to get the size of all files matching a regex. An example regex for .bak files:

.*.bak$

How do I get that information, considering a standard Linux with core GNU utilities or BusyBox?

Edit: The output is intended to be parseable by a script.

解决方案

I suggest something like: find . -regex '.*.bak' -print0 | du --files0-from=- -ch | tail -1

Some notes:

  • The -print0 option for find and --files0-from for du are there to avoid issues with whitespace in file names
  • The regular expression is matched against the whole path, e.g. ./dir1/subdir2/file.bak, not just file.bak, so if you modify it, take that into account
  • I used h flag for du to produce a "human-readable" format but if you want to parse the output, you may be better off with k (always use kilobytes)
  • If you remove the tail command, you will additionally see the sizes of particular files and directories

Sidenote: a nice GUI tool for finding out who ate your disk space is FileLight. It doesn't do regexes, but is very handy for finding big directories or files clogging your disk.

这篇关于在 Linux 中,名称与正则表达式匹配的文件的磁盘使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆