<p>I was trying to remove all the lines of a file except the last line but the following command did not work, although file.txt is not empty.</p>

<pre><code>$cat file.txt |tail -1 &gt; file.txt

$cat file.txt
</code></pre>

<p>Why is it so?</p>

It seems to not like the fact you're writing it back to the same filename.  If you do the following it works:
```
```
$cat file.txt | tail -1 > anotherfile.txt
```
```