Detail five ways to empty or delete large file contents in Linux

  • 2020-12-13 19:16:15
  • OfStack

Sometimes, when working with files in the Linux terminal, you may want to clear the contents of the file without having to open it with any Linux command-line editor. How can I achieve this goal? In this article, we will use 1 useful command to clear the file contents in several different ways.

Warning: Before we move on to the various methods, note that because all 1 slices in Linux are files, you must always make sure that the files you are clearing are not important user or system files. Cleaning up the contents of critical systems or configuration files can lead to fatal application/system errors or failures.

As I said, here's how to clear the contents of a file from the command line.

Important: For the purposes of this article, access.log uses files in the following example.

1. Empty the contents of the file by redirecting to empty

The simplest way to use shell to redirect null (a nonexistent object) to empty or empty the contents of a file is as follows:


 # > access.log

2. Use the 'true' command to redirect and clear the file

Here we will use one symbol: it is an shell built-in command, which is essentially the same as the true command, which can be used as no operation (no operation).

Another option is to redirect the output: or the true built-in command to a file, as shown below:


 # : > access.log 
OR 
 # true > access.log

3. Use the cat/ cp/ dd utility with /dev/null to empty the files

In Linux, this null device is basically used to discard unwanted output streams of a process, or as a suitable empty file for an input stream. This is usually done through a redirection mechanism.
Thus, a device file is a special file that can log out (delete) any input sent to it, or its output is the same as the output of an empty file.
In addition, you can use the cat command via /dev/null to clear the contents of the file by redirecting the output to it (the file) as input.


#cat /dev/null> access.log

Next, we will use the cp command to empty the file contents, as shown in the figure.


#cp / dev/null access.log

In the following commands, if represents the input file and of refers to the output file.


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

4. Use the echo command to empty the file

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


#echo  ""  > access.log 
 or 
 # echo > access.log

Note: You should remember that empty strings are different from null. The string is already an object because it might be empty, whereas null simply means there is no object.

Therefore, when you redirect the echo command above to a file and use the cat command to view the contents of the file, a blank line (an empty string) is printed.

To send empty output to a file, use the -n flag to tell echo that the flag does not print the end line break that resulted in the empty line generated in the previous command.


#echo -n  "" > access.log

5. Use the truncate command to empty the file

The truncation command helps shrink or expand the file size to a specified size.
You can specify the file size option using -s. To clear the contents of the file, use size 0 (zero) as follows:


 # truncate -s 0 access.log

That's it. In summary, in this article, we've covered various ways to clear or empty file contents using a simple command-line utility and the shell redirection mechanism.

These may not be the only practical methods available, so you can also tell us about any other methods not covered in this guide through the feedback section below.


Related articles: