Unix Shell脚本截断大文件 [英] Unix shell script to truncate a large file

查看:76
本文介绍了Unix Shell脚本截断大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Unix脚本,它将截断/清空一个文件,当文件达到3GB的空间时,该文件将被应用程序连续写入/打开.我知道下面的命令可以做到:

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 :

cp /dev/null [filename]

但是,我将在生产环境中自动将其作为cron作业-只是在此处发布以查看你们在执行类似操作时是否遇到任何问题.

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.

推荐答案

只需添加另一个答案

: > filename

:是bash中的无操作对象(符合POSIX),因此,这实际上只是打开文件进行写入(当然会截断文件),然后立即将其关闭.

: 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.

正如Shellter所说,您实际上不需要命令来进行重定向:

as shellter commented, you don't actually need a command to go along with the redirection:

$ echo foo > foo.txt
$ cat foo.txt
foo
$ > foo.txt
$ cat foo.txt
$

仅通过简单的重定向即可清除文件.

A simple redirection all by itself will clear the file.

这篇关于Unix Shell脚本截断大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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