530

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
  • 235,242
Andrew
  • 15,494

18 Answers18

835

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,733
geek
  • 10,674
266

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,760
69
: > 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.

62

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

22

Below command should also work :

cat /dev/null > file.log
17

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, where 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

Franck Dernoncourt
  • 24,246
  • 64
  • 231
  • 400
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
  • 235,242
soum
  • 139
9

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
  • 380
7
$ 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
3

It can be done using sed

sed -i d filename

apena
  • 171
3

A few alternatives:

ex +%d -scwq file.log
cp /dev/null file.log
vi +%d -escwq file.log
install -m600 /dev/null file.log
Franck Dernoncourt
  • 24,246
  • 64
  • 231
  • 400
kenorb
  • 26,615
2

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.

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

Franck Dernoncourt
  • 24,246
  • 64
  • 231
  • 400
d491049
  • 21
2

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
  • 9,237
1

With my permissions this is the only thing that worked:

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

In Windows environment:

type nul >filename
Franck Dernoncourt
  • 24,246
  • 64
  • 231
  • 400
wasif
  • 9,176
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
  • 3,257
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,483