249

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?

14 Answers 14

418

In bash, just

> filename

will do.

This will leave you with an empty file filename.

p.s.

If you need sudo call, please consider to use truncate as answered here.

  • 46
    This is probably the raddest command in the history of computing – blarg Dec 2 '13 at 16:34
  • 7
    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 '15 at 20:39
98

You can use the user command : truncate

truncate -s 0 test.txt

("-s 0" to specify the size)

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

  • 2
    Best answer around, works when you're non-root (with sudo) — sudo echo > file just gives an error in such cases. – Dmitry Verkhoturov Aug 19 '15 at 7:35
  • 9
    @DmitryVerkhoturov: sudo bash -c '> filename' is how you use the Bash solutions with sudo. – Stuart P. Bentley Sep 14 '15 at 19:03
  • brew install truncate on os x with homebrew installed. – xaxxon Jul 4 '16 at 7:47
  • Works faster than echo – Miao1007 Nov 26 '18 at 6:30
33

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

  • This will leave a space in there. – dmckee Jan 1 '10 at 1:58
  • I'm getting this: -bash: : command not found – Andrew Jan 1 '10 at 3:52
  • 3
    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
  • 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). – Stuart P. Bentley Jul 25 '14 at 5:09
  • working fine with Mac, thanks – AMIC MING Dec 1 '15 at 1:40
22
: > 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.

  • I came to make this answer, but alas it is already here... Take an upvote. – Aaron Hall Dec 29 '14 at 3:38
  • 3
    Just do :> filename. Doesn't it look like a smiley? :> – minmaxavg Sep 14 '15 at 2:37
9

ZSH

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

7

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.

  • 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
  • 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 Oct 1 '14 at 10:13
4
$ 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
  • 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. – quack quixote Jan 1 '10 at 4:57
  • 2
    You dont need to run the cat command. Just "> file.log" will do it. – camh Jan 1 '10 at 6:22
  • Removing the file will reset the creation date. – Matteo Jul 16 '13 at 12:40
  • 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 Dec 8 '17 at 23:21
4

Below command should also work :

cat /dev/null > file.log
2

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

1

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

0

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

With my permissions this is the only thing that worked:

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

Few alternatives:

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

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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