How to delete log file contents from linux terminal?

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

I want to remove the first lines of 100,000 from a file. I could open the file and delete these lines, but because of the size of the file, which is required for me forever to download it.

So, can this be done from a Linux terminal? If so, how?

+6
source share
3 answers

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

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

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

cat /dev/null > writelog

See also this thread on unix.com .

+13
source

It might be better to keep the last 1000 lines:

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

And you must enable logrotate ( manual ) for the file. Then the system will ensure that the file does not grow out of proportion.

+7
source

Source: https://habr.com/ru/post/924746/


All Articles