# UNIX command to delete a line from a file?

**URL:** <https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637>\
**Category:** Factual Questions\
**Created:** [May 1, 2002, 5:11am UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637 "2002-05-01T05:11:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrWhy](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@MrWhy](https://boards.straightdope.com/u/MrWhy)\
**Post date:** [May 1, 2002, 5:11am UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637/1 "2002-05-01T05:11:32Z")

</div>

I’ve been trying to find the answer to this through Google, but I can’t come up with the magic words.

How do I delete the first (or indeed any) line from a text file in UNIX?

I can’t load the file into vi (which is the only editor on our system) because it is too large - over 300MB.

Any help would be great…

---

<div class="post-metadata">

**Author:** ![K364](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/k364/32/5_2.png) [@K364](https://boards.straightdope.com/u/K364)\
**Post date:** [May 1, 2002, 5:42am UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637/2 "2002-05-01T05:42:22Z")

</div>

The tail command should do it:

[Here’s the man page](http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?tail)

---

<div class="post-metadata">

**Author:** ![ScottHaneda](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@ScottHaneda](https://boards.straightdope.com/u/ScottHaneda)\
**Post date:** [May 1, 2002, 5:45am UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637/3 "2002-05-01T05:45:23Z")

</div>

I think you need to think of it more as sucking out all but the one line, and pushing that into a new file…you will use grep to locate the culprit line…

rather than give my way of doing it, this article I just drummed up should give you some ideas…  
[http://www.computing.net/unix/wwwboard/forum/3053.html](http://www.computing.net/unix/wwwboard/forum/3053.html)

---

<div class="post-metadata">

**Author:** ![MrWhy](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@MrWhy](https://boards.straightdope.com/u/MrWhy)\
**Post date:** [May 1, 2002, 5:53am UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637/4 "2002-05-01T05:53:06Z")

</div>

OK, I like the idea of “sucking out all but the one line”, and making a new file.

So, using that idea and the man page, the command would be:

tail +1 oldfile.txt \> newfile.txt

to delete the first line. Is that right?

---

<div class="post-metadata">

**Author:** ![ScottHaneda](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@ScottHaneda](https://boards.straightdope.com/u/ScottHaneda)\
**Post date:** [May 1, 2002, 6:06am UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637/5 "2002-05-01T06:06:53Z")

</div>

look good to me, plus, since you are piping the output to a new file, there is not much risk.

---

<div class="post-metadata">

**Author:** ![MrWhy](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@MrWhy](https://boards.straightdope.com/u/MrWhy)\
**Post date:** [May 1, 2002, 6:08am UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637/6 "2002-05-01T06:08:56Z")

</div>

Thanks man. You guys are great. 🙂

(And I’ll bookmark the sites too.)

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [May 1, 2002, 4:54pm UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637/7 "2002-05-01T16:54:20Z")

</div>

You could also do this with sed or awk, if available on your system, but I don’t remember the exact commands. If you’re going to be doing a lot with UNIX, those will be very useful tools to know.

---

<div class="post-metadata">

**Author:** ![yabob](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/yabob/32/2821_2.png) [@yabob](https://boards.straightdope.com/u/yabob)\
**Post date:** [May 1, 2002, 5:16pm UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637/8 "2002-05-01T17:16:24Z")

</div>

> [@](#):
>
> \*Originally posted by Chronos \*  
> \*\*You could also do this with sed or awk, if available on your system, but I don’t remember the exact commands. If you’re going to be doing a lot with UNIX, those will be very useful tools to know. \*\*

sed -e 1d oldfile \> newfile

would be one way. sed is often my tool of choice for ad-hoc  
file manipulation. I thought I’d let this pass since the OP solved  
his problem, but since you brought it up …

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [May 1, 2002, 6:46pm UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637/9 "2002-05-01T18:46:03Z")

</div>

To delete an arbitrary line from a file, you could grep it out:

```auto

grep -v foo file.txt > newfile.txt

```

That would send all lines _not_ matching the pattern “foo” (-v option) to newfile.txt.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [May 1, 2002, 7:07pm UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637/10 "2002-05-01T19:07:39Z")

</div>

Yes, but that (grep) would only work if that line alone matches some specific pattern. In a file that size, it seems quite possible that you’d have duplicate entries, so at the least you’d want to do a straight grep to stdout first, to check if the line is unique.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [May 1, 2002, 7:30pm UTC](https://boards.straightdope.com/t/unix-command-to-delete-a-line-from-a-file/106637/11 "2002-05-01T19:30:57Z")

</div>

That’s true, **Chronos** , though I am assuming that this is a data file of some sort with a fairly predictable format, or that a specific enough regex could be composed to isolate a single line.
