1. Overview

When we handle files in Linux, it’s common that at some point we need to empty some of them. Log files are a good example where we want to clear them out from time to time.

In this tutorial, we’ll look at several ways to accomplish that through some practical examples.

2. Creating Our Sample File

Before we begin, let’s create a file called original_file which will contain some lines of text:

$ cat << eof > original_file
one two
three four

Then, we’ll create another one that will be our copy on which we’ll work:

$ cp original_file example_file

We’ll have to do this to refill the file after we test each of our scenarios.

3. Using Shell-Specific Commands

First, we can use certain characteristics of the shell that we’re using.

For example, in the case we’re using Bash, we can just type the > character:

$ > example_file

Using the colon built-in:

$ : > example_file

Or the true built-in:

$ true > example_file

However, in Zsh, we can only make use of our two approaches in a quick way.

$ : > example_file
$ true > example_file

Both do nothing and return an exit status of 0.

Now, let’s try with:

$ > example_file

And nothing happens: the shell expects an input. However, if we press ctrld we send the end-of-file and finally, we can check the size of the file to confirm that the file has no content.

We can do this (either with or without the colon) because of the clobber and noclobber option. Otherwise, we receive the message:

zsh: file exists: example_file

In this scenario, we can solve in some ways like:

  • Typing setopt clobber, and try again our past strategies
  • Typing unsetopt noclobber, and try again
  • Using the ! after the > character in some of our past strategies – for example, : >! example_file

4. Sending the Empty String

Another way to solve the problem is to send an empty string to the file.

We can achieve this with the use of the echo command:

$ echo -n > example_file

Here, with the -n parameter, we prevent echo from adding the line break it adds by default.

We can also make use of the printf command:

$ printf '' > example_file

Another way that we can take is with the use of a here-document specifying some string, let’s say EOF:

$ cat << EOF > example_file

Wait, then only write EOF, and then press <Enter>.

5. Using a Specific Size

Another way to remove the content of a file is to set the file size to zero.

Let’s try that with the truncate command:

$ truncate -s 0 example_file

We can also approach this problem using the dd command:

$ dd if=/dev/urandom of=example_file bs=1 count=0

In the previous approach, we used the /dev/urandom file, but we can use any, there’s no difference as long as the count parameter is zero.

6. Using /dev/null

The /dev/null file is quite useful and comfortable since, if we write to it, it allows us to discard what was sent there, or, if we read from it, it returns end-of-file.

Let’s try using the /dev/null file with the dd command:

$ dd if=/dev/null of=./example_file

We can get the same with cat:

$ cat /dev/null > example_file

Or with cp:

$ cp /dev/null example_file

7. Using awk and sed

sed is a useful and powerful tool that we can use to solve our problem:

$ sed -i 'd' example_file

With the parameter -i, we instruct to sed that all changes are made within the file.

With the d command, we instruct sed to delete the entire row. As sed will do this record by record, the final file will be empty.

We can achieve something similar with the use of GNU Awk:

$ gawk -i inplace '0' example_file

Since a zero will be evaluated as false in awk, we force awk to ignore all input lines by returning a “0“, then we save the empty back to the input file.

We can only use the “-i inplace” option if we have version 4.1.0 of GNU Awk or later. If the inplace extension is not available in our Awk, we can still do it via a temp file:

$ awk '0' example_file > tmp_file && mv tmp_file example_file

Note also that these strategies, while maintaining the file name, change the inode number. They are also slow since they check the file.

8. Conclusion

Emptying files is a common task when working on Linux.

In this tutorial, we looked at different ways to empty them, from using shell-specific commands as well as other tools their content.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.