Occasionally, while working on a file in the Linux terminal, you may want to empty the contents of the file without opening it using any Linux command line editor. How can this be achieved? In this article, we will empty the file contents in a few different ways with the help of some useful commands.
Warning : Before we go further into the various methods, please note that since everything is a file in Linux, you must make sure that the user files or system files you empty are not important. Empty contents are critical system files or configuration files that can cause fatal application/system errors or failures.
Having said all that, here's how to get into how to clear the contents of a file from the command line.
Important : We use the access.log file in the examples that follow in this article.
1. Empty the file by redirecting to Null
One of the easiest ways to empty the contents of a file is to use the shell to redirect null (non-existent objects) to the file as follows
# > access.log
2. Empty files using "true" named redirects
Here we will use the notation : is also a shell built-in command, essentially equivalent to the true command, which can be used as a no-op.
Another way is to redirect the output of the builtin command : or true to a file like this:
# : > access.logOR # true > access.log
3. Empty File Using cat/cp/dd utilities with /dev/null
In Linux, a null device is basically used for discarding an unneeded output stream in a process, or a suitable empty file as an input stream. This is usually achieved through a redirection mechanism.
Therefore, the /dev/null device file is a special kind of file that will delete any input sent to it or output an empty file.
As an extension, you can clear the contents of a file by redirecting the output of /dev/null to a file using the cat name.
# cat /dev/null > access.log
Next use the cp command to whitewash the file as follows:
# cp /dev/null access.log
In the command below, if is the input file and of is the output file.
# dd if=/dev/null of=access.log
4. Empty the file using the echo command
Here, we redirect to a file using the echo command and an empty string, as follows:
# echo "" > access.log
or
# echo > access.log
Note : You should know that an empty string is not equal to null. String is already an object, it may be empty and null means non-existent object.
For this reason, when you use the echo command to redirect the above file, and use the cat command to view the contents of the file, a blank line (empty string) is output.
To send a null output to the file, use the flag -n which tells echo to not wrap the output after the output, whereas the preceding command produced an empty line.
# echo -n "" > access.log
5. Empty the file named with truncate
The truncate command helps to shrink or expand the file size to a defined size.
You can use the -s option to specify the file size. The next command clears the file contents by specifying a file size of 0:
# truncate -s 0 access.log
Now, in this article, we've covered multiple methods for clearing or emptying file contents using simple command line utilities and shell redirection mechanisms. But these may not be the only practical ways to do so.