如何从linux终端删除日志文件的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12302290/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:16:32  来源:igfitidea点击:

How to delete content of a log file from linux terminal?

linuxterminal

提问by Sushan Ghimire

I have a log file on server called writelogwhich is about 2GB.

我呼吁服务器日志文件writelog大约是2GB

I want to delete first 100,000lines from the file. I could open the file and delete those lines but because of the file size it takes me forever to download it.

我想100,000从文件中删除第一行。我可以打开文件并删除这些行,但由于文件大小,我需要永远下载它。

So, is it possible to do this from Linux terminal? If yes how?

那么,是否可以从 Linux 终端执行此操作?如果是如何?

采纳答案by perreal

If you are running a Linux server, you can use ssh:

如果您正在运行 Linux 服务器,则可以使用ssh

ssh [email protected] sed -i '1,100000d' /path/to/logfile

回答by Aaron Digulla

It might be better to keep the last 1000 lines:

保留最后 1000 行可能会更好:

mv writelog writelog.bak
tail -1000 writelog.bak > writelog

And you should enable logrotate(manual) for the file. The system will then make sure the file doesn't grow out of proportions.

您应该为文件启用logrotate手动)。然后系统将确保文件不会超出比例。

回答by roblogic

If you want to clear out the whole filea quick way is

如果你想清除整个文件,一个快速的方法是

cat /dev/null > writelog

cat /dev/null > writelog

See also this thread on unix.com.

另请参阅unix.com 上的此线程