Open In App

Different Ways to Empty or Delete a Large File Content in Linux

Last Updated : 11 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn different methods to empty or delete large file content in the Linux/Unix system by overwriting the existing target file with a newly created empty file.  Before we proceed with the given various ways, please make sure that the file we’re emptying or deleting content is not important or system file because once the content of the file is deleted by means of the given ways, recovery of the file is not possible. 

These are the following method to empty or delete a large file content in the Linux/Unix system. 

  1. Using the truncate command.
  2. Using the echo command.
  3. Using the redirection ( >) operator.
  4. Using the true redirection ( : > ).
  5. Using /dev/null.

1. Empty or delete the contents of a large file using the truncate command in the Linux/Unix system. 

The truncate command is used to shrink or extend the size of a file to a specified size in the Linux system. It is also used to empty large file contents by using the -s option followed by 0 (zero) specified size. 

In the below example, we will empty the contents of a large file using the truncate command as shown below. 

truncate -s 0 file.txt

truncate to empty a file

2. Empty or delete the contents of a large file using the echo command in the Linux/Unix system. 

Basically, the echo command is used to display lines of text. It is also used to empty the contents of a large file as shown below example. 

echo > file.txt

echo to empty a file

3. Empty or delete the contents of a large file using the > (redirection operator) in the Linux/Unix system.

To empty the contents of a large, we can just put > (redirection operator) before a file name as shown below example. 

>file.txt

redirection operator to empty  a file

4. Empty or delete the contents of a large file using the :> (true redirection) in the Linux/Unix system.

A symbol : is a shell-built command which is equivalent to a true command. To empty or delete the contents of a large, we can also use :> or true > followed by file name as shown below example. 

:>file.txt

redirection operator to empty a file

5. Empty or delete the contents of a large file using the /dev/null device file in the Linux/Unix system.

In Linux/Unix system a /dev/null is a special device file that removes the contents of a file and outputs an empty file. In the below example, we will make empty the contents of a file. 

cat /dev/null > file.txt

/dev/null to empty a file


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads