Truncating a file with Linux

Truncating a file?

To truncate a file is to make it empty (its size becomes 0 byte). This comes in handy for logs, when they grow too big, or when you need to re-use an existing file but want to skip the chmod/chown steps.

So, how to truncate a file?

Just enter and execute the following (replacing “filename.ext” with the actual name of the file you want to truncate):


Its size should now be zero, unless…

Permission denied?

No problem!

sudo sh -c ">filename.ext"

Enter your password if asked, and now its size should be zero.
If you are not in the sudoers list, then too bad, you cannot truncate this file.

Know of another / a better way to truncate file?

Please share it! 🙂

One comment

  1. The most common use case for this are large log files (eg: catalina.out) that are not rotating. If you delete the file, the application will keep writing to the “ghost” on disk until you restart the application.

    By truncating the file – the application does not lose it.

    I also use :

    cp /dev/null filename.ext

Leave a Reply

Your email address will not be published. Required fields are marked *