Linux Commands - sed (delete)

by
Jeremy Canfield |
Updated: November 26 2020
| Linux Commands articles
Let's say that example.txt contains the following two lines.
~]# cat example.txt
foo
bar
The sed command with the d (delete) option can be used to delete lines from example.txt. In this example, lines matching "foo" will be removed.
sed '/foo/d' example.txt
Which should return the following.
bar
However, example.txt will still contain both foo and bar.
~]# cat example.txt
foo
bar
The -i or --in-place flag is needed to actually perform the removal.
sed -i '/foo/d' example.txt
Now example.txt will only contain bar.
~]# cat example.txt
bar
nobody owner
Let's say that example.txt is owned by John Doe.
~]# ls -l example.txt
-rw-r--r--. 1 john.doe john.doe 64 Nov 26 01:33 example.txt
When using the -i or --in-place flag to actually perform the removal, the file will be updated to be owned by the "nobody" user.
~]# ls -l example.txt
-rw-r--r--. 1 nobody john.doe 64 Nov 26 01:33 example.txt
The -c or --copy flag can be used to so that the file remains owned by John Doe.
sed --copy -i '/foo/d' example.txt
Did you find this article helpful?
If so, consider buying me a coffee over at