Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

I know how to print the contents to the screen:

cat file.log

I know how to edit the file, line-by-line:

nano file.log

But I don't want to delete each line one at a time. Is there a way to do it in one command without destroying the file to do it?

share|improve this question

10 Answers 10

up vote 123 down vote accepted

In bash, just

> filename

will do.

This will leave you with an empty file filename.

share|improve this answer
This is probably the raddest command in the history of computing –  blarg Dec 2 '13 at 16:34
I feel bad for destroying the 42 count, I really do. –  k0pernikus May 14 '14 at 1:43
stunning command! –  Ansyori Jan 23 at 4:54
Windows 8.1 fail: C:\Users\User\Desktop>> okay123.txt The syntax of the command is incorrect. –  Arekkusandaa.Irujasukin Feb 3 at 23:16
On the contrary, @Arekkusandaa.Irujasukin; it works just fine in any version of Windows, so long as, as mentioned, you are using bash as your shell and not e. g. cmd.exe or PowerShell, neither of which are POSIXy enough to be expected to handle such things. –  DopeGhoti Feb 5 at 20:39

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).

share|improve this answer
This will leave a space in there. –  dmckee Jan 1 '10 at 1:58
+1. "" > file.log if you don't want the space. –  RJFalconer Jan 1 '10 at 2:18
I'm getting this: -bash: : command not found –  Andrew Jan 1 '10 at 3:52
should be echo " " > file.log, I believe. –  Babu Jan 1 '10 at 4:13
echo "" > will still give you a file with one character (a newline.) If you want to use echo, use "echo -n > file.log" to echo null. –  Dave Forgac May 4 '12 at 19:47

You can use the user command : truncate

truncate -s 0 test.txt

("-s 0" to specify the size)


share|improve this answer
That's another good one. –  bobobobo Jan 16 '14 at 0:01

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

vim file.txt 

Press Esc.


Press Enter.

You will find all lines deleted.

share|improve this answer
This is the only thing that worked for my maillog, even sudo > /var/log/maillog wouldn't clear it (permission denied) –  bobobobo Jan 16 '14 at 0:00
@bobobobo It doesn't work as sudo > /var/log/maillog means - run sudo, and put its output into the file. Obviously it puts output with current user permissions. Easier thing do sudo su to start root shell and then do > /var/log/maillog. –  kan Oct 1 '14 at 10:13


>! filename

ZSH will protect users from clobbering files using the io redirect operator >. If you use >! you can force the truncation of an existing file.

If you want ZSH to use Bash's redirection behavior, were there is no protection from file clobbering, then you need to set the clobber option for your shell.

More Info: http://zsh.sourceforge.net/Doc/Release/Redirection.html

share|improve this answer
$ rm file.log; touch file.log


$ cat > file.log

followed by control-d.


Ah. Here is a single command version:

$ dd if=/dev/null of=file.log
share|improve this answer
Thechickenmoo's suggestion of echo "" > file.log is better than the cat option you show, in this situation, tho there are others where cat is more appropriate. both use the same shell redirection to do the heavy lifting. –  quack quixote Jan 1 '10 at 4:57
You dont need to run the cat command. Just "> file.log" will do it. –  camh Jan 1 '10 at 6:22
@camh: +1, that's a trick i didn't know before. nice one. –  quack quixote Jan 1 '10 at 12:42
Removing the file will reset the creation date. –  Matteo Jul 16 '13 at 12:40
+1 for: the dd version allows you to clear a file that you only have access to with sudo. –  timeSmith Jan 30 at 19:07
: > file.log

Same as > filename in Bash, but works in more shells (credit). Redirects the output from the true builtin (which has no output) to filename.

share|improve this answer
I came to make this answer, but alas it is already here... Take an upvote. –  Aaron Hall Dec 29 '14 at 3:38

One line at a time?

Try vi(m), the lovely text editor that can do anything. In this case, navigate to a line, press d (for delete), and d again (for line).

share|improve this answer
If you want to get rid of the whole file in vim, with the cursor at the top of the document, typing d G (that's d, then shift-G) will delete the entire file (d for delete, G for end of the file). I prefer your method, though (it gives me more time to think about whether or not I REALLY want to trash the file). –  Babu Jan 1 '10 at 4:19
@Babu gg moves to the top so the entire sequence could be ggdG. –  Bob Mar 4 '14 at 0:22

With my permissions this is the only thing that worked:

touch temp.txt
sudo mv temp.txt original-file.txt
share|improve this answer
truncate -s0 file


sudo truncate -s0 file

I found this at http://www.commandlinefu.com/commands/view/12/empty-a-file

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.