6

I have a log file on server called writelog which is about 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.

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

Sushan Ghimire
  • 7,307
  • 16
  • 38
  • 65
  • 1
    you can see http://stackoverflow.com/questions/2112469/delete-specific-line-numbers-from-a-text-file-using-sed – duslabo Sep 06 '12 at 14:27

3 Answers3

13

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

cat /dev/null > writelog

See also this thread on unix.com.

roblogic
  • 1,266
  • 2
  • 13
  • 23
  • 1
    @Bala That depends on your shell settings ... A better way of doing this is: `: >| writelog` to override the `-C`/`noclobber` option ... in C shell, this is: `: >! writelog`... The `:` is the built-in "null command", which does nothing, outputs nothing, and always exits 0. – Martin Tournoij Feb 11 '15 at 14:52
7

It might be better to keep the last 1000 lines:

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
4

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

ssh username@mydomain.com sed -i '1,100000d' /path/to/logfile
perreal
  • 94,503
  • 21
  • 155
  • 181