468

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?

slhck
  • 223,558
  • 70
  • 607
  • 592
Andrew
  • 14,554
  • 30
  • 70
  • 82

18 Answers18

743

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.

Pablo A
  • 1,470
  • 13
  • 21
geek
  • 9,746
  • 3
  • 21
  • 16
  • 110
    This is probably the raddest command in the history of computing – blarg Dec 02 '13 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 Feb 05 '15 at 20:39
  • 2
    LInters complain, maybe more elegant is to `true > filename` – Pablo A Mar 23 '20 at 19:15
  • 1
    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](https://superuser.com/a/634217/33767) is actually more robust. – Caleb Apr 19 '21 at 10:49
232

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

nono
  • 2,420
  • 1
  • 12
  • 4
55
: > 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.

55

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

Thechickenmoo
  • 659
  • 4
  • 4
16

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

Brian Wigginton
  • 261
  • 2
  • 6
13

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.

slhck
  • 223,558
  • 70
  • 607
  • 592
soum
  • 139
  • 1
  • 2
  • 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 00: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 01 '14 at 10:13
10

Below command should also work :

cat /dev/null > file.log
paarth batra
  • 201
  • 2
  • 3
6
$ 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 01 '10 at 04:57
  • 2
    You dont need to run the cat command. Just "> file.log" will do it. – camh Jan 01 '10 at 06:22
  • 1
    Removing the file will reset the creation date. – Matteo Jul 16 '13 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 Dec 08 '17 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 Jan 11 '21 at 10:48
4

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

timeSmith
  • 310
  • 1
  • 2
  • 7
3

It can be done using sed

sed -i d filename

apena
  • 171
  • 5
2

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

d491049
  • 21
  • 1
2

Few alternatives:

ex +%d -scwq file.log
cp /dev/null file.log
vi +%d -escwq file.log
install -m600 /dev/null file.log
kenorb
  • 24,736
  • 27
  • 129
  • 199
1

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.

DomainsFeatured
  • 209
  • 1
  • 3
  • 9
1

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
Io-oI
  • 7,588
  • 3
  • 12
  • 41
1

With my permissions this is the only thing that worked:

touch temp.txt
sudo mv temp.txt original-file.txt
Morgan
  • 11
  • 1
0

In windows environment:

type nul >filename
Wasif
  • 7,984
  • 2
  • 19
  • 32
0
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

MMM
  • 2,652
  • 5
  • 25
  • 43
  • 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 [answer]. There is also [tour] for the site tour, and [help] for the help center. – help-info.de Mar 17 '21 at 16:59
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).

Phoshi
  • 23,233
  • 2
  • 61
  • 81
  • 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 01 '10 at 04:19
  • @Babu `gg` moves to the top so the entire sequence could be `ggdG`. – Bob Mar 04 '14 at 00:22