Linux删除大小为0的文件
如果Linux中的某个文件大小为0,如何删除该文件。我想在crontab中执行此操作,而不需要任何额外的脚本。
l filename.file | grep 5th-tab | not eq 0 | rm
像这样的东西吗?
How do I delete a certain file in linux if its size is 0. I want to execute this in an crontab without any extra script.
l filename.file | grep 5th-tab | not eq 0 | rm
Something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这将删除目录(及以下)中大小为零的所有文件。
如果你只想要一个特定的文件;
This will delete all the files in a directory (and below) that are size zero.
If you just want a particular file;
您需要使用查找:
you would want to use find:
要搜索并删除当前目录和子目录中的空文件:
-type f
是必要的,因为目录也被标记为大小为零。点
.
(当前目录)是起始搜索目录。如果您有 GNU find(例如不是 Mac OS),则在这种情况下可以省略它:来自 GNU
查找
文档:To search and delete empty files in the current directory and subdirectories:
-type f
is necessary because also directories are marked to be of size zero.The dot
.
(current directory) is the starting search directory. If you have GNU find (e.g. not Mac OS), you can omit it in this case:From GNU
find
documentation:您可以使用命令
find
来执行此操作。我们可以使用-type f
来匹配文件,并使用-size 0
来匹配空文件。然后我们可以使用-delete
删除匹配项。You can use the command
find
to do this. We can match files with-type f
, and match empty files using-size 0
. Then we can delete the matches with-delete
.在 Linux 上,当您不需要 find(1) 时,stat(1) 命令非常有用:
这里的 stat 命令允许我们仅获取文件大小,即
-c %s
(请参阅其他格式的手册页)。我正在运行 stat 程序并捕获其输出,即$( )
。此输出以数字形式显示,即外部(( ))
。如果大小为零,则为 FALSE,因此执行 OR 的第二部分。非零(非空文件)将为 TRUE,因此 rm 不会被执行。On Linux, the stat(1) command is useful when you don't need find(1):
The stat command here allows us just to get the file size, that's the
-c %s
(see the man pages for other formats). I am running the stat program and capturing its output, that's the$( )
. This output is seen numerically, that's the outer(( ))
. If zero is given for the size, that is FALSE, so the second part of the OR is executed. Non-zero (non-empty file) will be TRUE, so the rm will not be executed.这适用于纯 BSD,因此它应该与所有风格普遍兼容。下面的例子在
pwd
(.
)This works for plain BSD so it should be universally compatible with all flavors. Below.e.g in
pwd
(.
)对于非递归删除(使用 du 和 awk):
For a non-recursive delete (using du and awk):