Empty a file

> file.txt
For when you want to flush all content from a file without removing it (hat-tip to Marc Kilgus).

By: root
2009-01-26 10:22:31

12 Alternatives + Submit Alt

What Others Think

Nice one! Some thing in *nix are so simple that you'd never imagine they exist :)
evad · 625 weeks and 3 days ago
cat > file.txt followed by shift+ins allows you to insert your buffer content into file. ^d to finish the process
boombastic · 625 weeks ago
A similar useful trick which works with things like sudo and pfexec is "cp /dev/null file.txt"; that way you don't have to switch to another account to empty files which you do not own.
systemj · 624 weeks and 1 day ago
To avert surprises, prepend ":" to perform specified redirections reliably: : > foo FWIW, the (built-in) null command ":" also expands arguments.
laburu · 624 weeks ago
laburu can you expand on the reliability issue you're referring to?
pixelbeat · 624 weeks ago
This is especially useful with large files that need to be cleared. I've run across a situation where the inodes ran out on the hd because people kept cat'ing /dev/null and redirecting it to file. That method kept the inodes locked.
leprasmurf · 623 weeks and 6 days ago
It should be noted that this is a Bash-ism. This will hang in ZSH and throw an error to STDERR with BSD-CSH.
atoponce · 620 weeks and 4 days ago
Excellent! Just what I was looking for
xenon87 · 619 weeks and 3 days ago
If you've done set -o noclobber the command will need to be: >| file.txt
rtmhal · 618 weeks and 2 days ago
This is not just a is a Bash-ism it works fine on the korn shell too.. tested on ksh/HP-UX.
zlemini · 604 weeks and 6 days ago
Perfect for clearing log files :)
okuehn · 589 weeks ago
Useful trick, thanks One more question: if i need to not change the time of the emptied file? How can i do? thanks in advance
bugmenot · 536 weeks and 1 day ago
That is very nice also becouse '>' command preserves the permissions
carlesso · 523 weeks and 2 days ago
Thanks! I used to make it longer with: echo "" > file.txt
ktonga · 457 weeks ago
The portability to zsh is because in zsh, the command invoked by "> file.txt" is the null command. When zsh is invoked in sh compatibility mode, the option sh_nullcmd will be automatically set, and the null cmd will be ":". If csh_nullcmd (csh compat mode triggers that), then the null cmd will cause an error. Without a compatibility option, the _default_ null command in zsh is "cat", so "> file.txt" is equivalent to "cat > file.txt" and you'll need to type ctrl-D to get the same effect. Set the shell variable "NULLCMD" to another command to change it. So NULLCMD=: is equivalent to sh_nullcmd and will make the above work. The suggestion from laburu to use ": > file.txt" is good and is the most portable solution, since its behaviour is guaranteed by POSIX rather than being a shell extension.
syscomet · 452 weeks and 5 days ago
this does *not* work with zsh. It's much better to do ":>file.txt"
j_melis · 438 weeks and 6 days ago
For some reason, all I get using ZSH is "zsh: file exists: log/test.log", using both "> log/test.log" and ":> log/test.log". I haven't figured it out yet.
_who · 195 weeks and 6 days ago
Danke, dafur dass Ihr jeden Tag mit uns die Hl. Messe gefeiert habt, fur all die Reparaturen im ganzen Haus jeuxdefriv10.net jogosfriv2018.net jogosfriv2019.com immer zur Verfugung zu stehen fur Euere Bereitschaft juegofriv10.com juegofriv20.com juegosdefriv2.net juegosgeometrydash.com juegosdetwizl.com Danke, dass Sie Ihr Muhen um den Aufbau des Leibes Christi mit uns teilten. juegostwizy.com twizlgratis.com
ngocmai · 183 weeks and 1 day ago
wir danken euch fur das bisherige Teilnehmen lassen, mochten aber den Rundbrief nicht mehr friv-2018.com friv-2019.com friv-2020.com friv4school2019.com Ihnen allen vielen Dank dafur, dass Sie diesen gl?cklichen Tag mit uns teilen. jogosfofy.com friv4school2020.com friv2018.us Ich mochte Kommissar Fischler fur seine Freimutigkeit und Offenheit danken, jeuxdetwizl.com jeuxdefriv10.net jogosfriv2018.net n der er uns jeden unternommenen Schritt erl?utert und die verschiedenen vom Wissenschaftlichen Lenkungsausschu.
ngocmai · 180 weeks and 3 days ago
Herr Cashman, ich danke Ihnen f?r diese Information. juegosyepi2017.com juegosdetwizl.com juegoskizi2017.net Wir danken aber auch Ihnen friv.uno juegosfriv2com.com juegosdefriv2019.com liebe Leserinnen und Leser, juegosdefriv20.com fur Ihr Interesse und Ihre Mitwirkung juegofriv20.com juegofriv4.com juegosfriv3com.com die Informationsgesellschaft in der Schweiz voranzubringen.
ngocmai · 180 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: