Occasionally, while dealing with files in a Linux terminal, you may want to clear the content of a file without necessarily opening it using any Linux command line editors. How can this be achieved?
In this article, we will go through several different ways of emptying file content with the help of some useful commands.
With that said, below are means of clearing file content from the command line.
Important: For the purpose of this article, we’ve used the file access.log
in the following examples.
1. Empty File Content by Redirecting to Null
The easiest way to empty or blank a file content using shell redirect null
(non-existent object) to the file as below:
# > access.log
2. Empty File Using ‘true’ Command Redirection
Here we will use a symbol :
is a shell built-in command that is essence equivalent to the true
command and it can be used as a no-op (no operation).
Another method is to redirect the output of :
or true
built-in command to the file like so:
# : > access.log OR # true > access.log
3. Empty File Using cat/cp/dd utilities with /dev/null
In Linux, the null
device is basically utilized for discarding unwanted output streams of a process, or else as a suitable empty file for input streams. This is normally done by a redirection mechanism.
And the /dev/null
device file is therefore a special file that writes off (removes) any input sent to it or its output is the same as that of an empty file.
Additionally, you can empty the contents of a file by redirecting the output of /dev/null
to it (file) as input using the cat command:
# cat /dev/null > access.log
Next, we will use the cp command to blank a file content as shown.
# cp /dev/null access.log
In the following dd command, if
means the input file and of
refers to the output file.
# dd if=/dev/null of=access.log
4. 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
Note: You should keep in mind that an empty string is not the same as null. A string is already an object much as it may be empty while null simply means the non-existence of an object.
For this reason, when you redirect the out of the echo command above into the file, and view the file contents using the cat command, is prints an empty line (empty string).
To send a null output to the file, use the flag -n
which tells echo to not output the trailing newline that leads to the empty line produced in the previous command.
# echo -n "" > access.log
5. Empty File Using truncate Command
The truncate command helps to shrink or extend the size of a file to a defined size.
You can employ it with the -s
option that specifies the file size. To empty a file content, use a size of 0 (zero) as in the next command:
# truncate -s 0 access.log
That’s it for now, in this article we have covered multiple methods of clearing or emptying file content using simple command line utilities and shell redirection mechanisms.
These are not probably the only available practical ways of doing this, so you can also tell us about any other methods not mentioned in this guide via the feedback section below.
Just use shred command to securely delete files in Linux.
@Lewis,
Yes, you absolutely correct, using the shred command is a standard method for securely deleting files on Linux systems, as it overwrites the file’s content before deleting it, making it more challenging for data recovery tools to retrieve the original information.
I have yet another alternative for you:
Yes, it is two commands, but you cannot beat the conceptual simplicity and not need to memorize any special syntax. It is pretty good out-of-the-box thinking :-)
Doesn’t work when you want to preserve any of the file’s attributes, >file is just better.
Is there any way to clean up half of the file instead of full clean up?
Full cleanup :
Let suppose I want a file with the last 10 days of logs and can delete/cleanup older than 10 days (I don’t want to archive this file and replace a new file for fresh logs), can I do half of the file clean up in a file?
@Vinod
There should be a way but we have to do a little research about this before we can give you a solution.
Dears, I have a log file in which I have 3 months’ logs how can I just keep the last 30 days log and rest to be deleted. Ho to do this?
How to specify this condition if log file size grows 2GB then clear one-month-old data..
@shujauddin Qureshi,
You would need to do this via a cron job. are you familiar with cron? You would write a script, save it somewhere, and then trigger it via cron.
The script would need to handle the logic of parsing the file into lines, parse the date from each line, and then discard the lines that have a date more than 30 days old.
I would use logrotate for this.
https://www.tecmint.com/install-logrotate-to-manage-log-rotation-in-linux/
How to remove the content (files & directories) in the same folder at one time.
Problem is file automatically create or delete (Dynamic file are creating)
I know
>
and:>
are methods that empty the file without actually “touching” it, if the file is being used.Are the other methods working the same way? We always get tickets for large files and 80% of the time, we need to compress the contents of the file and truncate or “zero” it but with the problem that the file is being used and we can’t stop the process using it.
At the moment, I only used the redirect methods I mentioned at the start of my post.
@Lokiyo,
All know that
>
and:>
are standard options to empty the large file content without being touching it. But there are other methods works same way as show in this article, I think you should give a try and see..When I opened this link I was expecting ways to actually get rid of the file contents on the disk. All of these methods only unlink the disk space from the file, leaving the contents on the drive.
Of the choices listed, though, I think “truncate -s 0 ” is the best option.
The system dropped the word “file” at the end of the truncate command because I put it in lt and gt signs.
@James
There are other methods for completely removing file content on the disk such as shred command plus a few others. We will create a how-to guide on this topic soon.
Another simple option utilizing the cat command:
cat > /path/to/file
Then hit Ctrl+D
Voila, empty file.
@Mike
Many thanks for the useful tip, its surely simple as well.
every trick using redirection “>” are the same !
>file
: >file
true >file
cat /dev/null >file
my_prog >file (if my_prog produce no output)
because the shell truncate (or create) “file” before launching the command (cat, true, …), the most important is that the command produces no output !!!
you can understand that >file is the best one because it needs no other program !
@Bruno
It is true, the basic idea is the use of the redirection mechanism. We are simply offering various alternatives that a user can choose to use, though they are more like the same.