1. Overview

Sometimes, we want to quickly clear only a file’s content from the Linux command line and keep the empty file.

In this tutorial, let’s see how to achieve that.

2. Introduction to the Problem

As usual, let’s understand the problem through an example. Let’s say we have a non-empty text file:

$ cat input.txt
Hi there,
   I'm not an empty file.
Bye.

Now, we’d like to clear the content of the input.txt file but keep the file. So, in the end, we expect to see the file become empty.

To verify it, we can use the wc command to check the number of bytes the file has. If a file is empty, we should have 0 as a result:

$ wc -c input.txt
0 input.txt

Next, let’s see how to empty a file.

3. Using Commands

We can do the job by executing some commands on the target file. Next, let’s see them in action.

Usually, we use the cp command to copy files in Linux. Also, we know /dev/null is a special file present in every Linux system. Typically, we use /dev/null for disposing of unwanted output streams of a process, or as a convenient empty file for input streams.

Therefore, we can copy /dev/null to input.txt to empty the target file:

$ cp /dev/null input.txt 
$ wc -c input.txt
0 input.txt

As the output above shows, the file is emptied.

sed is a powerful text-processing utility. It supports the in-place change feature. For example, we can use the -i option with GNU Sed to do in-place changes.

Therefore, we can use sed to in-place delete everything from the given file. Let’s restore the input.txt file and see how to do that:

$ sed -i 'd' input.txt 
$ wc -c input.txt
0 input.txt

As the sed command above shows, we only use sed‘s “d” command to delete everything from input.txt.

In Linux, the command truncate contracts or expands a file to a given size. Therefore, we can truncate the file to the size “0” to clear its content:

$ truncate -s 0 input.txt 
$ wc -c input.txt
0 input.txt

4. Using Redirection

So far, we’ve used different commands to clear the input.txt file. Alternatively, in Linux, we can change files’ content using IO redirections.

For example, we know /dev/null provides an empty input, so we can redirect the output of the command “cat /dev/null” to the target file to clear its content:

$ cat /dev/null > input.txt
$ wc -c input.txt
0 input.txt

Similarly, we can redirect echo‘s output to the target file:

$ echo -n '' > input.txt
$ wc -c input.txt 
0 input.txt

It’s worth mentioning that we need the -n option. Otherwise, the file is not empty. Instead, it will contain a trailing newline:

$ echo '' > input.txt
$ wc -c input.txt
1 input.txt

As we can see, the command to empty a file using redirection is pretty compact. Next, let’s explore a few even more quick ways to do that.

: > FILE” can truncate FILE to zero length:

$ : > input.txt
$ wc -c input.txt
0 input.txt

The ‘:‘ in the command above works as a dummy command, producing no output.

If we’re working with Bash, we can even save the colon character:

$ > input.txt
$ wc -c input.txt 
0 input.txt

However, we should note that this won’t work for all shells. For example, it won’t work in Zsh.

In Zsh, we can use “: > input.txt“.

5. When sudo Is Required

We’ve learned how to empty a file in different ways. So far, in all examples, we assume our current login user has the “write” permission to the input.txt file.

Next, let’s see how to empty a file that the current user doesn’t have permission to write to.

We know sudo allows us to execute a command with the super user’s permission. So, assuming our user can use sudo, we can use sudo to clear the target file. Let’s see an example:

$ ls -l input_super.txt
-rw-r--r-- 1 root root 41 Apr 18 15:56 input_super.txt

As the ls command shows, the file input_super.txt is 41 bytes in length. Moreover, only the user root has the “write” permission on it.

If we decide to use our command-based approach to empty the file, it’s easy – just add sudo in front of the command:

$ sudo cp /dev/null input_super.txt
[sudo] password for kent: 

$ wc -c input_super.txt 
0 input_super.txt

As shown in the example above, we’ve executed the cp command with sudo. After giving the current user (kent)’s password, the file is successfully cleared.

However, if we would like to empty the file in a redirection-based way, simply adding sudo in front of the command won’t work:

$ sudo echo -n '' > input_super.txt
bash: input_super.txt: Permission denied

We may wonder why it complains about permission even if we’ve used sudo. This is because although the echo command is executed with the super user’s permission, the redirection is performed by the shell, which doesn’t have the permission to write to the file input_super.txt.

To solve that, we can run a shell with sudo and pass the command to it by using the -c option:

$ sudo sh -c "echo -n '' > input_super.txt" 
[sudo] password for kent: 

$ wc -c input_super.txt
0 input_super.txt

6. Conclusion

In this article, we’ve discussed two approaches to clearing the content of a file in Linux:

  • Using some command such as sed or truncate
  • Using redirection

Further, we’ve addressed how to use the approaches above together with sudo.

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