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

297

88

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?

Andrew

Posted 2010-01-01T01:50:57.193

Reputation: 11 982

Answers

488

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.

geek

Posted 2010-01-01T01:50:57.193

Reputation: 7 120

7On 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 – 2015-02-05T20:39:21.990

61This is probably the raddest command in the history of computing – blarg – 2013-12-02T16:34:26.803

133

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

Posted 2010-01-01T01:50:57.193

Reputation: 1 430

3Best answer around, works when you're non-root (with sudo) — sudo echo > file just gives an error in such cases. – Dmitry Verkhoturov – 2015-08-19T07:35:39.347

12@DmitryVerkhoturov: sudo bash -c '> filename' is how you use the Bash solutions with sudo. – Stuart P. Bentley – 2015-09-14T19:03:21.093

1brew install truncate on os x with homebrew installed. – xaxxon – 2016-07-04T07:47:12.070

Works faster than echo – Miao1007 – 2018-11-26T06:30:54.290

35

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

Posted 2010-01-01T01:50:57.193

Reputation: 459

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 – 2014-07-25T05:09:10.333

You might have some problems with sudo. – flapjack – 2019-01-10T02:44:51.383

4echo "" > 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 – 2012-05-04T19:47:16.670

1This will leave a space in there. – dmckee --- ex-moderator kitten – 2010-01-01T01:58:50.147

I'm getting this: -bash: : command not found – Andrew – 2010-01-01T03:52:18.130

30

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

Stuart P. Bentley

Posted 2010-01-01T01:50:57.193

Reputation: 691

I came to make this answer, but alas it is already here... Take an upvote. – Aaron Hall – 2014-12-29T03:38:04.967

5Just do :> filename. Doesn't it look like a smiley? :> – minmaxavg – 2015-09-14T02:37:20.803

11

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

Posted 2010-01-01T01:50:57.193

Reputation: 211

9

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.

soum

Posted 2010-01-01T01:50:57.193

Reputation: 99

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 – 2014-10-01T10:13:10.460

This is the only thing that worked for my maillog, even sudo > /var/log/maillog wouldn't clear it (permission denied) – bobobobo – 2014-01-16T00:00:09.370

4

Below command should also work :

cat /dev/null > file.log

paarth batra

Posted 2010-01-01T01:50:57.193

Reputation: 141

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

dmckee --- ex-moderator kitten

Posted 2010-01-01T01:50:57.193

Reputation: 7 311

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 – 2017-12-08T23:21:09.747

Removing the file will reset the creation date. – Matteo – 2013-07-16T12:40:50.453

2Thechickenmoo'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 – 2010-01-01T04:57:50.133

2You dont need to run the cat command. Just "> file.log" will do it. – camh – 2010-01-01T06:22:39.823

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

timeSmith

Posted 2010-01-01T01:50:57.193

Reputation: 100

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

d491049

Posted 2010-01-01T01:50:57.193

Reputation: 11

1

Few alternatives:

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

kenorb

Posted 2010-01-01T01:50:57.193

Reputation: 16 795

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.

DomainsFeatured

Posted 2010-01-01T01:50:57.193

Reputation: 179

0

With my permissions this is the only thing that worked:

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

Morgan

Posted 2010-01-01T01:50:57.193

Reputation: 1

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

Posted 2010-01-01T01:50:57.193

Reputation: 22 001

5If 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 – 2010-01-01T04:19:44.860

@Babu gg moves to the top so the entire sequence could be ggdG. – Bob – 2014-03-04T00:22:20.110