bash 用于截断大文件的Unix shell脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13575477/
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
Unix shell script to truncate a large file
提问by peedee
I am trying to write a Unix script which will truncate/empty a file which is continuously being written/open by an application when it reaches say 3GB of space. I know that the below command would do it :
我正在尝试编写一个 Unix 脚本,当它达到 3GB 空间时,它将截断/清空应用程序不断写入/打开的文件。我知道下面的命令可以做到:
cp /dev/null [filename]
But I am going to run this in an production environment automatically as a cron job - just posting here to see if you guys faced any issues while doing something similar to this.
但是我将在生产环境中自动运行它作为 cron 作业 - 只是在这里发布看看你们在做类似的事情时是否遇到任何问题。
回答by chepner
Just to add another answer,
只是为了添加另一个答案,
: > filename
:
is a no-op in bash (POSIX-compliant), so this essentially just opens the file for writing (which of course truncates the file) and then immediately closes it.
:
是 bash 中的无操作(符合 POSIX 标准),因此这实际上只是打开文件进行写入(当然会截断文件),然后立即关闭它。
EDIT: as shellter commented, you don't actually need a command to go along with the redirection:
编辑:正如 shellter 所评论的,你实际上不需要一个命令来进行重定向:
$ echo foo > foo.txt
$ cat foo.txt
foo
$ > foo.txt
$ cat foo.txt
$
A simple redirection all by itself will clear the file.
一个简单的重定向本身将清除文件。
回答by Ricardo Marimon
I've used the following command on debian
我在 debian 上使用了以下命令
truncate -s 0 filename
回答by Don Branson
That seems reasonable to me. Unix, of course, would let you do this about 50 different ways. For example,
这对我来说似乎是合理的。当然,Unix 可以让您以大约 50 种不同的方式执行此操作。例如,
echo -n "" >filename
cat /dev/null >filename
回答by San
trunc filename
trunc filename
works on AIX flavor of UNIX
适用于 AIX 风格的 UNIX