Empty Contents of a Specified File Without Deleting via the Command Line

May 7, 2012 - 3 Comments

Remove Contents from a specified file without deleting the file

If you’re working at the command line and need to quickly empty the contents of a file, throw a greater than symbol and a space in front of the filename like so:

> filename

All content within the target file will be immediately removed without warning, leaving it blank while retaining the files existence, filename, and permissions. This is often preferable and quicker than manually deleting and recreating a file.

A good practical example is for clearing out the contents of log files, for example:

> ~/Library/Logs/updates.log

You can also use this command to create a new 0 byte file at the location, similar to using the touch command.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: William Pearson in Command Line, Tips & Tricks

3 Comments

» Comments RSS Feed

  1. sault says:

    Nice tip.

  2. Bryan says:

    this is a great idea if a file is being held open for writing, rather than stop the process and delete the file and restart the process.

  3. Portcontrol7 says:

    Interestingly, I messed around with this a bit this morning and found that it was unable to delete .textClipping file content. I made a file in nano, and gave it the .textClipping extension and it did remove the contents of the file…

    Strange…

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates