Delete all contents of the file using sed Delete all contents of the file using sed unix unix

Delete all contents of the file using sed


It looks like a strange request. Anyway, this is a way:

sed -i '/^/d' file
  • sed -i does an in-place replacement.
  • /^/ matches lines, in this case all of them because ^ means "beginning of line".
  • /d deletes them.

Or shorter (thanks glenn jackman as always):

sed -i d file


You don't need sed for this. To empty a file:

> filename

with no command, that redirection will truncate the file.


Try this sed. It will remove all.

sed -ni '' file

n do not print if not told to do so.
i in place.
Since no code is given, file will be replaced by nothing.