116

I have a file called error.log on my server that I need to frequently truncate. I have rw permissions for the file. Opening the file in vi > deleting all content > saving works (obviously). But when I try the below

cat /dev/null > error.log

I get the message

File already exists.

Obviously there is some kind of configuration done on the server to prevent accidental overriding of files. Can anybody tell how do I "truncate" the file in a single command?

Sumeet Pareek
  • 2,739
  • 2
  • 22
  • 17

9 Answers9

161

You have the noclobber option set. The error looks like it's from csh, so you would do:

cat /dev/null >! file

If I'm wrong and you are using bash, you should do:

cat /dev/null >| file

in bash, you can also shorten that to:

>| file
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
75

You can also use function truncate

$truncate -s0 yourfile

if permission denied, use sudo

$sudo truncate -s0 yourfile

Help/Manual: man truncate

tested on ubuntu Linux

risnandar
  • 5,513
  • 1
  • 26
  • 17
  • 1
    truncate also touches the file's modification time - while ">file" does not touch if if the file is unchanged. – dsteinkopf Mar 17 '17 at 21:11
  • It doesn't release the disk space that file holds. At first it shows the 0 bytes. But when you write something to that file, the file size is = before truncate + new data size. – Aniket Kulkarni Dec 02 '19 at 11:44
  • @AniketKulkarni that's because your file has not been opened with APPEND flag, so it keeps the write buffer, and when it updates that buffer it flushes the whole buffer to the file, and that's how you see it with the same + increased size. – Mladen B. Aug 13 '21 at 20:19
48

This will be enough to set the file size to 0:

> error.log
SIFE
  • 5,567
  • 7
  • 32
  • 46
  • 6
    actually, this **doesn't** work with noclobber (tested it myself), so it's NOT a valid solution in this case ("-bash: error.log: cannot overwrite existing file"); see commend above ("@Jarmund - because the user has noclobber set, >file does not work."). I'm just wondering what kind of "intelligent people" upvote obviously wrong answers like this... –  Apr 12 '14 at 16:14
13

the credit goes for my senior colleague for this:

:> filename

This will not break log files, so you can even use it on syslog, for example.

petermolnar
  • 1,626
  • 14
  • 23
  • 9
    Please explain. How does it work? What is that notation doing? – user31986 Jan 15 '16 at 22:00
  • 3
    @user31986 `:` is a no-effect command (almost a comment), then `>` is redirection as usual, thus this just redirects the lack of output from a command that does nothing into the file. If noclobber is enabled, you need `:>| filename` (bash) (and I presume `:>! filename` in csh assuming csh has `:`). – Rhubbarb Aug 31 '17 at 12:47
4

false|tee fileToTruncate

may work as well

Dodger Web
  • 114
  • 1
  • 5
  • That's just silly. Or if this is useful, obscurely getting `dd` to output nothing would be even better than the plain readable `false`? – tripleee Nov 16 '16 at 06:29
4

Since sudo will not work with redirection >, I like the tee command for this purpose

echo "" | sudo tee fileName
Community
  • 1
  • 1
sakhunzai
  • 13,900
  • 23
  • 98
  • 159
  • well, actually it does, as described in both the question you linked and here above. "sudo sh -c '> error.log'"... –  Apr 12 '14 at 16:11
  • it depends on the permissions on the target file `error.log`, if that is already writable there is not need to sudo. I think the other issue is you are passing command as string argument to `sh` , I don't know how that is handled internally but that is another layer of complexity/redirection added – sakhunzai Apr 14 '14 at 05:36
  • 2
    This won't actually truncate the file, because `echo ""` will add a newline. You can use `echo -n ""` to suppress the newline – Seb Feb 19 '16 at 10:43
1

Any one can try this command to truncate any file in linux system

This will surely work in any format :

truncate -s 0 file.txt
Jouby
  • 2,196
  • 2
  • 21
  • 33
1

I do like this: cp /dev/null file

Den129
  • 11
  • 3
-4

You can try also:

echo -n > /my/file

Oden
  • 756
  • 8
  • 21
  • 8
    See the answer with a ton of votes? Try reading it, and look up `noclobber` and then ask yourself if your answer makes any sense at all. –  May 27 '13 at 08:26