Bash 脚本 - 查找具有非零字节内容的目录
我将如何使用 Bash 脚本查找工作目录中至少包含非零字节文件的所有目录?这:
查找 . -最大深度 1 -类型 d -尺寸 +1c |排序
似乎不起作用
how would I go about finding all dirs in my working dir that contain at least a non zero byte file using a Bash script ? This :
find . -maxdepth 1 -type d -size +1c | sort
does not seem to work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
这更深入一层,查找非空文件,然后获取父目录
找到任何内容,然后删除重复项。
How about this:
This goes one level deeper, looking for nonempty files, then gets the parent directory
of anything it finds, then removes duplicates.
不确定是否理解完全。如果我有 ./lvl1/lvl2/file (文件非空)并且 lvl1 仅包含空文件和目录 lvl2,那么 lvl1 是否应该出现在输出中?
我以为你想要它。认为这是可行的:
查找工作目录的所有子目录,看看其中是否有非空文件。所以我们只能看到感兴趣的关卡名称。 uniq 因为目录不太可能只包含 1 个非空文件。
编辑:减慢速度的最大的事情可能是(没有做任何测试哈哈)find 在找到大小> 0 的文件后继续在目录中查找(我们应该能够停止查看这一点)。可以在每个子目录上调用 find ,然后在看到第一个匹配项时退出 find 。
可以在此处放弃对 uniq 的调用(因为每个顶级目录只有 1 个结果)。我认为采取任何措施来消除切口不会有太大帮助。
另一件事是,您可能想要更改它查看常规文件而不是查看目录或其他内容(它将跳过一堆内容)。呃,而不是“-type f”考虑使用“!-type d”
即将入睡,所以完全有可能我错过了一些事情/做了一些愚蠢的事情xD
Not sure if understanding fully. If I have ./lvl1/lvl2/file (file is non-empty) and lvl1 contains only empty files and the directory lvl2, should lvl1 appear in the output?
I assumed you want it to. Think this works:
find looks in all sub-directories of working directory to see if there is a non-empty file anywhere inside of it. cut so we only see the level name of interest. uniq since it is unlikely a directory only contains 1 non-empty file.
EDIT: biggest thing slowing it down is probably (didn't do any tests lol) that find continues to look in a directory after finding a file of size >0 (we should be able to stop looking at this point). can call find on each subdir and then have find exit when it sees the first match.
can drop call to uniq here (since there will only be 1 result for each top level dir). I don't think doing anything to get rid of the cut will help much.
another thing is that you might want to change that this looks at regular files with it not looking at directories or something (it will skip over a bunch of stuff). er, instead of "-type f" think about using "! -type d"
about to fall asleep so it is entirely possible I missed something/did something stupid xD
我使用这个,converse grep 因为空目录显示为 4.0K
编辑:
不使用
max-depth
并使用summary
请注意,如果您想要包含点目录,请确保已设置
I use this, converse grep because empty directories appear as 4.0K
Edit:
without
max-depth
and usingsummary
Note if you have dot directories you want to include make sure you have set