- Most efficient method to empty the contents of a file
- 3 Answers 3
- 5 Ways to Empty or Delete a Large File Content in Linux
- 1. Empty File Content by Redirecting to Null
- 2. Empty File Using ‘true’ Command Redirection
- 3. Empty File Using cat/cp/dd utilities with /dev/null
- 4. Empty File Using echo Command
- 5. Empty File Using truncate Command
- How to Empty a Log File in Linux
- 4 ways to empty a file in Linux
- Method 1: Truncate a file using truncate command
- Method 2: Empty file using :> or >
- Method 3: Using echo command to empty file in Linux
- Method 4: Use /dev/null to clear a file
- In the end…
- HOW TO CLEAR A FILE IN LINUX
- 1. Redirect operator.
- 2. The cat command.
- 3. The echo command.
- 4. cp command.
- 5. truncate command.
- 6. Results.
Most efficient method to empty the contents of a file
Of these three I abuse >filename the most as that requires the least number of keystrokes.
However, I would like to know which is the most efficient of the three (if there are any more efficient methods) with respect to large log files and small files.
Also, how does the three codes operate and delete the contents?
1 Edit: as discussed in this answer, this actually does not clear the file!
Very similar to Difference between cat and ‘>’ to zero out a file where you’ll find more information.
touch does not delete contents, but does change access time on the file. It does create an empty file if none existed.
3 Answers 3
Actually, the second form touch filename doesn’t delete anything from the file — it only creates an empty file if one did not exist, or updates the last-modified date of an existing file.
cp /dev/null filename works.
As for efficient, the most efficient would be truncate -s 0 filename (see here).
Otherwise, cp /dev/null filename or > filename are both fine. They both open and then close the file, using the truncate-on-open setting. cp also opens /dev/null , so that makes it marginally slower.
On the other hand, truncate would likely be slower than > filename when run from a script since running the truncate command requires the system to open the executable, load it, and then run it.
5 Ways to Empty or Delete a Large File Content in Linux
Occasionally, while dealing with files in 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.
Caution: Before we proceed to looking at the various ways, note that because in Linux everything is a file, you must always make sure that the file(s) you are emptying are not important user or system files. Clearing the content of a critical system or configuration file could lead to a fatal application/system error or failure.
With that said, below are means of clearing file content from the command line.
Important: For the purpose of this article, we’ve used file access.log in the following examples.
1. Empty File Content by Redirecting to Null
A easiest way to empty or blank a file content using shell redirect null (non-existent object) to the file as below:
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 of unwanted output streams of a process, or else as a suitable empty file for input streams. This is normally done by 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 same as that of an empty file.
Additionally, you can empty contents of a file by redirecting output of /dev/null to it (file) as input using cat command:
Next, we will use cp command to blank a file content as shown.
In the following command, if means the input file and of refers to the output file.
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 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.
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:
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 mechanism.
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.
How to Empty a Log File in Linux
In this tutorial, you’ll learn various ways to truncate a log file and delete its content without deleting the file itself.
You’ll find yourself in situations where you need to empty a file. This often happens when you have huge log files and How would you do that?
One not so clean way would be to remove the file and then create a new file. But this is not a good idea. It won’t be the same file; the timestamp (atime, mtime etc) will be different along with other file permissions.
Instead of creating a new empty file, you can delete its content. So, how do you empty a file in Linux? How to clear a file from all of its content without deleting the actual file?
4 ways to empty a file in Linux
There are several ways you can empty a file without actually deleting the file. Let me show you some of these methods.
Method 1: Truncate a file using truncate command
The safest way to truncate a log file is using the truncate command.
In the above command, -s is used to set/adjust the size (in bytes) of the file. When you use -s 0, it means you adjusted the file size to 0 bytes.
Method 2: Empty file using :> or >
The simplest way to empty a file is to use the command below. If the file is not in use, it will work in Bash:
While the above works only in Bash Shell, you can use a similar command for other shells:
You can also use this command to clear a file:
Method 3: Using echo command to empty file in Linux
Another way to empty a file is via echo command in Linux:
You can also use the echo command in this way:
Method 4: Use /dev/null to clear a file
You can also use the famous /dev/null and combine it with the cat command to clear a log file:
In the end…
And if you don’t have enough permissions for any of the above commands, this is the sure shot but a little dirty way to achieve it:
touch newfile mv newfile filename
If your aim was to clear log files and make some free space, you should learn about cleaning journald logs.
I hope this quick tip helped you to clear a file in Linux. Do bookmark us for more Linux quick tips.
HOW TO CLEAR A FILE IN LINUX
In today’s article, I will tell you about different ways to clean up a file. Sometimes we all have to work with files in the Linux terminal and suddenly we need to clear the contents of a file without opening the file itself using any command line editors lines.
- Redirect operator.
- cat.
- The echo command
- cp command
- Truncate command.
- Summary
1. Redirect operator.
To clean the contents of a file, we can simply use the redirect operator (> ) before the file name.
2. The cat command.
Alternatively, you can clear the contents of a file by redirecting the output of /dev/null to a file as input with the cat command.
3. The echo command.
The echo command is used to output the contents to the console, thereby redirecting an empty string to a file.
4. cp command.
The cp command is actually used to copy something, but it can also clear the file.
5. truncate command.
You can use the truncate command to reduce or increase the size of a file.
6. Results.
In today’s article, we looked at how to clean up files in Linux. In fact, there are still a lot of ways, but I showed you the most frequent that everyone uses.
Thank you all, I hope that my article helped you in some way.