Home » Questions » Computers [ Ask a new question ]

How to clear the contents of a file from the command line?

How to clear the contents of a file from the command line?

I have a log file that has a bunch of stuff in it that I don't need anymore. I want to clear the contents.

Asked by: Guest | Views: 106
Total answers/comments: 5
Guest [Entry]

"In bash, just

> filename

will do. This will leave you with an empty file filename.

PS: If you need sudo call, please consider to use truncate as answered here."
Guest [Entry]

"You could do this:

echo -n """" > file.log

Using > to write the (null) input from echo -n to the file.

Using >> would append the null input to the file (effectively doing nothing but touching it)."
Guest [Entry]

"IF you want to do from inside a vim editor in command line, you can try this:

vim file.txt

Press Esc.

:1,$d

Press Enter.

You will find all lines deleted."
"IF you want to do from inside a vim editor in command line, you can try this:

vim file.txt

Press Esc.

:1,$d

Press Enter.

You will find all lines deleted."
Guest [Entry]

"$ rm file.log; touch file.log

or

$ cat > file.log

followed by control-d.

or...or...or...

Ah. Here is a single command version:

$ dd if=/dev/null of=file.log"
Guest [Entry]

"Below command should also work :

cat /dev/null > file.log"