Linux 删除大小为 0 的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5475905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux delete file with size 0
提问by Franz Kafka
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.
如果大小为 0,我如何删除 linux 中的某个文件。我想在没有任何额外脚本的情况下在 crontab 中执行它。
l filename.file | grep 5th-tab | not eq 0 | rm
Something like this?
像这样的东西?
采纳答案by Paul Tomblin
This will delete all the files in a directory (and below) that are size zero.
这将删除目录(及以下)中大小为零的所有文件。
find /tmp -size 0 -print -delete
If you just want a particular file;
如果你只想要一个特定的文件;
if [ ! -s /tmp/foo ] ; then
rm /tmp/foo
fi
回答by cdarke
On Linux, the stat(1) command is useful when you don't need find(1):
在 Linux 上,stat(1) 命令在您不需要 find(1) 时很有用:
(( $(stat -c %s "$filename") )) || rm "$filename"
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.
这里的 stat 命令只允许我们获取文件大小,即-c %s
(其他格式请参见手册页)。我正在运行 stat 程序并捕获其输出,即$( )
. 此输出以数字形式显示,即外部(( ))
. 如果大小为零,则为 FALSE,因此执行 OR 的第二部分。非零(非空文件)将为 TRUE,因此不会执行 rm。
回答by Antonio
To search and delete empty files in the current directory and subdirectories:
要搜索和删除当前目录和子目录中的空文件:
find . -type f -empty -delete
-type f
is necessary because also directories are marked to be of size zero.
-type f
是必要的,因为目录也被标记为大小为零。
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:
点.
(当前目录)是起始搜索目录。如果你有 GNU find(例如不是 Mac OS),你可以在这种情况下省略它:
find -type f -empty -delete
From GNU find
documentation:
来自GNUfind
文档:
If no files to search are specified, the current directory (.) is used.
如果未指定要搜索的文件,则使用当前目录 (.)。
回答by Harrison
For a non-recursive delete (using du and awk):
对于非递归删除(使用 du 和 awk):
rm `du * | awk ' == "0" {print }'`
回答by Di?u Thu Lê
find . -type f -empty -exec rm -f {} \;
回答by user1874594
This works for plain BSD so it should be universally compatible with all flavors. Below.e.g in pwd
( .
)
这适用于普通 BSD,因此它应该与所有风格普遍兼容。下面。例如在pwd
( .
)
find . -size 0 | xargs rm
回答by PYK
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
.
您可以使用该命令find
来执行此操作。我们可以使用 匹配文件-type f
,使用 匹配空文件-size 0
。然后我们可以删除匹配项-delete
。
find . -type f -size 0 -delete