5 Ways to Empty or Delete a Large File Content in Linux – Tecmint

Occasionally, when dealing with files in the Linux terminal, you may want to erase the contents of a file without necessarily opening it using any Linux command line editor. How can this be achieved? In this article, we will look at several different ways to empty file contents with the help of some useful commands.

Caution: Before proceeding to look at the various ways, keep in mind that because in Linux everything is a file, you should always make sure that the files you are emptying are not important user or system files. Deleting the contents of a critical system or configuration file could result in an application/system error or fatal error.

That said, below are the means to erase the contents of the file from the command line.

Important: For this article, we have used file access.log in the following examples.

1. Empty file contents

by redirecting

to null

An easier way to empty or blank the contents of a file using shell redirect null (non-existent object) to the file as follows:

# > access.log <img src="https://www.tecmint.com/wp-content/uploads/2016/12/Empty-Large-File-in-Linux.png" alt="Empty large file using null redirect in

linux” />
Empty large file using null redirect in linux

2. Empty

file using ‘true’ command redirection

Here we will use a symbol: it is a built-in shell command that is essentially equivalent to the true command and can be used as a no-op (no operation).

Another method is to redirect the output of : or true command embedded in the file like this

: # : > access.log or # true > access.log

Large empty file using Linux

commands 3. Empty file Using cat/cp/dd utilities with /dev/


On Linux, the null device is basically used to discard unwanted output streams from a process, or as an empty file suitable for input streams. This is usually done by a redirection mechanism.

And the device file /

dev/null is therefore a special file that cancels (deletes) any input sent to it or its output is the same as that of an empty file.

In addition, you can empty the contents of a file by redirecting the output of

/dev/null to it (file) as input using the command cat: # cat /dev/null > access.log

<img src="https://www.tecmint.com/wp-content/uploads/2016/12/Empty-Large-File-Using-Linux-Commands.png" alt="Empty file using the cat command" />
Empty file Using the cat A command

Next, we’ll use the cp command to leave the contents of a file blank as shown.

# cp /dev/null access.log

<img src

=”https://www.tecmint.com/wp-content/uploads/2016/12/Empty-File-Using-cat-Command.png” alt=”Empty file content using cp command” />Empty file content using cp command

In the next command, if stands for the input file and de refers to the output file.

# dd if=/dev/null of=access.log

Empty file content using the dd command
Empty file content using the dd 4 command


Empty file using echo command

Here, you can use an echo command with an empty string and redirect it to the file as follows:

# echo “” > access.log OR # echo > access.log

<img src="https://www.tecmint.com/wp-content/uploads/2016/12/Empty-File-Content-Using-dd-Command.png" alt="Empty file using echo Command" />
Empty File Using echo

Command Note – You should note that an empty string is not the same as null. A string is already an object, as it can be empty, while null simply means the non-existence of an object.

For this reason, when you redirect the previous out of the echo command to the file and view the contents of the file using the cat command, an empty line (empty string) is printed.

To send null output to the file, use the -n flag that tells echo not to generate the new final line that leads to the empty line produced in the previous command.

# echo -n “” > access.log <img src="https://www.tecmint.com/wp-content/uploads/2016/12/Empty-File-Using-echo-Command.png" alt="Empty file using null redirection

” />
Empty file using null redirection

5. Empty file by using

the truncate command

The truncate command helps you reduce or expand

the size of a file to a defined size.

You can use it with the -s option that specifies the file size. To empty the contents of a file, use a size of 0 (zero) as in the following command:

# truncate -s 0 access.log <img src="https://www.tecmint.com/wp-content/uploads/2016/12/Empty-File-Using-Null-Redirect.png" alt="Truncate file content on Linux

” />
file content on Linux

That’s all for now, in this article we have covered multiple methods to delete or empty file contents using simple command-line utilities and shell redirection mechanism.

These are probably not the only practical ways available to do this, so you can also tell us about any other methods not mentioned in this guide via the comments section below.

Contact US