Truncating files on a Linux system is a rather basic and common task for users and administrators alike. Perhaps the most common use for truncating (or emptying) a file would be in the case of log files. Clearing a bunch of old data from log files to make way for newer and up to date information can make troubleshooting much easier.

In this tutorial, we'll show several ways to truncate a file on the Linux command line, including multiple files at once. Use the methods below on your own system, applying the example you feel will best fit your scenario.

NOTE
You can only truncate files if you have the proper Linux file permissions. Specifically, you must have write permissions on whichever files you're trying to truncate.
In this tutorial you will learn:
  • How to use the truncate command
  • How to empty a file with Bash shell operator >
Various examples of truncating a file on Linux
Various examples of truncating a file on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software truncate
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

How to use the truncate command


SUBSCRIBE TO NEWSLETTER
Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


The truncate command can be used to force a file to be a certain size, by either reducing or enlarging it. Let's look at a few examples to see how to use it. We'll start out with a very basic text file that contains 11 bytes of data.

$ cat file.txt 
linuxconfig
  1. To reduce the file size to 5 bytes, we would use the following truncate command to specify that we want our file to be exactly 5 bytes.
    $ truncate -s 5 file.txt
    $ cat file.txt 
    linux
    
    As you can see, our file has been reduced to only say "linux" which takes up 5 bytes of data. The extra information has been deleted by the truncate command.
  2. You can also use other units, such as K for kilobytes, M for megabytes, G for gigabytes, etc.
    $ truncate -s 5M file.txt
    $ ls -lh file.txt
    -rw-rw-r-- 1 linuxconfig linuxconfig 5.0M Apr 12 22:15 file.txt
    
  3. To empty the file completely, use -s 0 in your command.
    $ truncate -s 0 file.txt
    


  4. Add a plus or minus sign in front of the number to increase or decrease the file by the given amount.
    $ truncate -s +5M file.txt
    
  5. If you don't have proper permissions on the file you're trying to truncate, you can usually just preface the command with sudo. Be careful not to do this on any important system files.
    $ sudo truncate -s 0 file.txt
    
  6. If you have multiple files that you want to truncate, you can specify each file name in your command.
    $ truncate -s 1M file1.txt file2.txt
    
  7. You can also use wildcards in your command to truncate multiple files at once.
    $ truncate -s 1M *.txt
    

How to empty a file with Bash shell operator >

Use the following examples to truncate/empty files on Linux by using the > Bash shell operator.

  1. The > operator is the simplest and most common way to empty a file. By using this operator, we can simply redirect empty output to a file, effectively clearing the entire file and leaving it empty. Here's an example of how to use it.
    $ > data.log
    


  2. Although example 1 is probably the most simple, there are other ways to redirect empty output to a file. Here's another one that you're likely to see quite often.
    $ echo "" > data.log
    
  3. Another possible example is to redirect /dev/null, which is just empty data.
    $ cat /dev/null > data.log
    
  4. You can also use wildcards in your command to truncate multiple files at once.
    $ > *.log
    

Closing Thoughts

In this guide, we saw various examples for truncating a file on Linux. This included giving a file a specific size, as well as reducing and enlarging files by a certain amount. We also saw how to empty files completely by using the truncate command or a Bash shell operator.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.