In this article, we will go over five different ways to empty/delete a large file in Linux.
You might have a file that’s gigabytes in size that you want to get rid of quickly, or you want to automate emptying a file for each iteration. Whatever the case may be, you’ll be able to easily delete or empty a file with help from this article.
We’ll be running these commands on an Ubuntu 22.04 VPS, but pretty much all of these commands should work on any modern distribution.
- A Linux VPS or a dedicated server of your choosing
- Write access to the file or root/sudo privileges
1. Using /dev/null to empty the contents of a file
The software device file (found in the /dev/ directory) called “null” is a file with two very simple purposes. It discards or deletes any data written to it or returns EOF (short for “end-of-file”) if you try to read its contents. In this case, we are going to read from /dev/null.
You can use the cat command (which is short for concatenate) to read the contents of /dev/null, followed by a redirection operator to redirect the output into the file we want to empty.
cat /dev/null > yourFile.ext
What this does is output the contents of /dev/null and then redirect that content into your file. The contents of your file are essentially starting with “End of file,” making it empty.
This redirection operator takes whatever data would’ve been printed to the console and allows you to place it into any file. Using only one > replaces the contents of the file with the data you are redirecting, and using two of them >> appends the data to the end of the file. Since we want to empty our file, we only use one > to replace the contents.
Here is a screenshot as an example.
You can see the text that says “10G” (short for ten gigabytes) changes to, say 0 (zero bytes) after our command is executed.
Unfortunately, you cannot delete a file using /dev/null as it can only do the two functions mentioned earlier.
2. Using echo to empty the contents of a file
You can use the
echo command, normally designed to send data to stdout (standard output or your console) and use a redirection operator again to place the contents of your echo command into the file you want to empty.
echo > yourFile.txt
The echo command outputs a new line when no input is provided. When combining this with a redirection of the output into your file, your file gets replaced with only the new line.
This is how it looks in practice:
As you can see, the file’s contents are emptied. You might notice, however, that unlike the previous method shown, this file now has a size of 1 byte instead of 0 bytes. This is because we store a newline instead of a truly empty file. If your use case requires that the file have a size of zero bytes, then this method won’t work for you.
Additionally, you cannot delete files using the echo command. Echo can only create data, not destroy it.
3. Using a redirection operator to empty the contents of a file
You may have noticed our use of redirection operators in both methods listed above to empty the contents of a file. Instead of using commands to redirect output, we can actually take advantage of this redirection to empty our file.
You can see in this command we are redirecting nothing! By doing this, we are taking the output of nothing and replacing the contents of our file with it.
This is what it looks like when you run the command using only redirection:
You should note that the file is emptied completely, with a file size of zero bytes. This is the easiest and fastest way to empty a file.
Once again, this method won’t work for deleting files, as it only redirects content instead of destroying it.
4. Using truncate to empty the contents of a file
This great command lets you truncate (or cut short) the content of any file easily and quickly. Interestingly, the name does not suggest it, but you can also use this command to increase the apparent size of any file too.
truncate -s 0 yourFile.txt
What this command does is execute the truncate command, the option
-s 0 sets the size to zero bytes.
You can see the example below:
This command does exactly as advertised, setting the file size to 0 bytes by truncating anything past that size.
Once again, this useful tool only sets the size of files; it does not delete them.
5. Using rm or unlink to delete a file
Now that we have seen multiple ways to empty the contents of a file, it’s about time we see ways to delete the file altogether. Luckily, there are two different ways of doing so.
Delete a file with the rm command
The rm command (short for remove) does one task extremely well: removing files and directories.
By running this, your file will be deleted instantly.
Another option is the unlike command, which simply calls the unlink function, which removes the link to the file.
If no process is using the file, then the file is deleted, and the filesystem reclaims the space.
Here are some examples detailing how they work:
The next image shows the use of unlink:
With these commands, however, the opposite issue is the case – you cannot empty the contents of the file. You can only delete the file or destroy the link to the file.
However, if you are a user of our Managed VPS hosting services, make sure to use our team of Linux experts. Not only will they give you a list of all of the large files on your system, but they can also clear their contents, delete the files, or even set up a rotation of these files to ensure their size stays small automatically.
Did this post help you empty or delete a large file on your filesystem? If so, please consider spreading the word so others can benefit from this tip. Have any other methods of emptying or deleting a file? Let us know in our comment section below!