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?

18 Answers 18


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.

  • 120
    This is probably the raddest command in the history of computing
    – blarg
    Commented Dec 2, 2013 at 16:34
  • 8
    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
    Commented Feb 5, 2015 at 20:39
  • 4
    LInters complain, maybe more elegant is to true > filename
    – Pablo A
    Commented Mar 23, 2020 at 19:15
  • 5
    This is fancy and short, but it does have a couple of downsides. First it's not immediately obvious how to handle file permissions such as the need for sudo. Of course you can do it with sudo bash -c '> filename', but spawning a shell is (relatively) expensive. Second it can easily trip up interactive shells that will actually hang waiting for input. This is especially relevant if you want to copy bits of a script into a terminal and get the same results. For these reasons I suggest the truncate based answer is actually more robust.
    – Caleb
    Commented Apr 19, 2021 at 10:49
  • it does not work in zsh :-(
    – alper
    Commented Sep 19, 2023 at 18:08

You can use the user command : truncate

truncate -s 0 test.txt

("-s 0" to specify the size)


  • 6
    Best answer around, works when you're non-root (with sudo) — sudo echo > file just gives an error in such cases. Commented Aug 19, 2015 at 7:35
  • 17
    @DmitryVerkhoturov: sudo bash -c '> filename' is how you use the Bash solutions with sudo. Commented Sep 14, 2015 at 19:03
  • 1
    brew install truncate on os x with homebrew installed.
    – xaxxon
    Commented Jul 4, 2016 at 7:47
  • Works faster than echo
    – Miao1007
    Commented Nov 26, 2018 at 6:30
  • This also works in a bash script, while > filename does not
    – Black
    Commented Feb 23, 2022 at 10:59
: > 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.

  • 21
    Just do :> filename. Doesn't it look like a smiley? :>
    – minmaxavg
    Commented Sep 14, 2015 at 2:37
  • On a macOS, > filename freezes. But : > filename works.
    – shinokada
    Commented Jul 28, 2021 at 1:26

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

  • 1
    This will leave a space in there. Commented Jan 1, 2010 at 1:58
  • I'm getting this: -bash: : command not found
    – Andrew
    Commented Jan 1, 2010 at 3:52
  • 9
    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. Commented May 4, 2012 at 19:47
  • It would be better to use a dedicated null-output command like true (or its builtin alias :), although ultimately you don't need a command at all (see the accepted answer). Commented Jul 25, 2014 at 5:09
  • You might have some problems with sudo.
    – flapjack
    Commented Jan 10, 2019 at 2:44

Below command should also work :

cat /dev/null > file.log
  • this is what ive been using for many years with no error
    – MaXi32
    Commented Oct 13, 2020 at 5:58
  • 3
    This one should be the best compatible answer because truncate isn't built-in in BSD like MacOS and bash isn't defult shell for some OS. Commented Mar 10, 2022 at 4:40


>! 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


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.

  • This is the only thing that worked for my maillog, even sudo > /var/log/maillog wouldn't clear it (permission denied)
    – bobobobo
    Commented Jan 16, 2014 at 0:00
  • 2
    @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
    Commented Oct 1, 2014 at 10:13
$ 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
  • 2
    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. Commented Jan 1, 2010 at 4:57
  • 2
    You dont need to run the cat command. Just "> file.log" will do it.
    – camh
    Commented Jan 1, 2010 at 6:22
  • 1
    Removing the file will reset the creation date.
    – Matteo
    Commented Jul 16, 2013 at 12:40
  • 1
    Removing the file will cause programs watching that file to lose their pointer to it (i.e., it will have a new address in the filesystem). Generally speaking, yes rm will accomplish the task of producing an empty file with the same name. But it's not going to be right for every use case.
    – robert
    Commented Dec 8, 2017 at 23:21
  • +1 for Robert. The rm way is useless in case you have a process writing into the file, while you want this truncation look unnoticed for this process.
    – jmary
    Commented Jan 11, 2021 at 10:48

If you need to sudo to superuser privilege to access the file then the accepted answer will not work. Using this does work:

truncate -s0 file

or explicitly with sudo:

sudo truncate -s0 file

More info here http://www.commandlinefu.com/commands/view/12/empty-a-file


It can be done using sed

sed -i d filename

  • +1. and you can do also with awk but this will be too slow for large files.
    – Cuauhtli
    Commented Jun 14, 2020 at 20:41
  • @Cuauhtli, why would it be slow?
    – CervEd
    Commented Jun 17, 2021 at 18:06

Few alternatives:

ex +%d -scwq file.log
cp /dev/null file.log
vi +%d -escwq file.log
install -m600 /dev/null file.log

Also, if you have multiple files you can use the following:

for file in /path/to/file/*; do > $file; done

This is helpful for log files in the same directory.


If you have spaces in filenames, use:

for file in /path/to/file/*; do > "$file"; done

(I could not to include it in comments to previous answer because I don't have 50 reputation. Sometimes limitations are contra productive.)


With my permissions this is the only thing that worked:

touch temp.txt
sudo mv temp.txt original-file.txt

There are multiple ways to clear the file as listed below:

echo "" > filename
cat /dev/null > filename

Below examples are mostly used in shell scripting

just# > filename
: > filename

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

  • 5
    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
    Commented Jan 1, 2010 at 4:19
  • @Babu gg moves to the top so the entire sequence could be ggdG.
    – Bob
    Commented Mar 4, 2014 at 0:22

In windows environment:

type nul >filename
cat filename
cp filename filename.bak
cat /dev/null > test 

This will empty the original file and but you can still access the backup if you need the old contents of the file

  • Welcome to Super User! Before answering an old question having an accepted answer (look for green ✓) as well as other answers ensure your answer adds something new or is otherwise helpful in relation to them. Here is a guide on How to Answer. There is also tour for the site tour, and help center for the help center. Commented Mar 17, 2021 at 16:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .