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

时间:2021-10-03 02:48:00

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

我在服务器上有一个名为writelog的日志文件,大约2GB。

I want to delete first 100,000 lines 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终端执行此操作?如果有,怎么样?

3 个解决方案

#1


3  

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

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

ssh username@mydomain.com sed -i '1,100000d' /path/to/logfile

#2


13  

If you want to clear out the whole file a quick way is

如果你想快速清除整个文件

cat /dev/null > writelog

cat / dev / null> writelog

See also this thread on unix.com.

另请参阅unix.com上的此主题。

#3


7  

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(手动)。然后系统将确保文件不会超出比例。

#1


3  

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

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

ssh username@mydomain.com sed -i '1,100000d' /path/to/logfile

#2


13  

If you want to clear out the whole file a quick way is

如果你想快速清除整个文件

cat /dev/null > writelog

cat / dev / null> writelog

See also this thread on unix.com.

另请参阅unix.com上的此主题。

#3


7  

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(手动)。然后系统将确保文件不会超出比例。