If you want to empty a file on Linux but don’t want to remove it entirely, you can try a couple of methods. Using the Linux terminal, you can make changes to the contents of a file, stripping it of content. The file will remain, allowing you to reuse it for other things. If you want to know how to empty a file in Linux, follow these steps.
How to Empty a File on Linux Using the cat Command
An easy and quick way to empty a file on Linux is to use the cat command. cat prints the output of a command or contents of a file to the terminal or writes the output to a file. You can use cat to redirect the contents of /dev/null to overwrite a file. /dev/null is a special Linux device used as a placeholder, allowing apps (and users) to write content to it to erase it. Treat /dev/null like a black hole, where content goes in but doesn’t come back out. Using cat, you can use the contents (or lack of) of /dev/null to overwrite any existing file. To use cat to empty a file on Linux:
How to Use the echo Command to Empty Linux Files
The echo command will output whatever you write back to you in the terminal or to a file. If you write nothing to echo, you can use it to empty a file. To use echo to empty a file on Linux:
How to Use the cp Command to Empty Linux Files
You can overwrite a file with an empty version using the cp command. cp stands for copy, allowing you to copy and replace larger files with empty ones. Using cat, you can create a new (empty) file. You can then use cp to move this file and overwrite the larger one. To use cp to empty a file on Linux:
Truncating Files on Linux
The truncate command on Linux allows you to reduce files to a specific size. If you use truncate with an output size of 0, you can completely empty a file. To empty a file on Linux using the truncate command:
Clearing Out Your Linux Files
Using the steps above, you can quickly empty a file on Linux. You can also follow more complex techniques to securely erase important files on Linux, ensuring that there’s no way to recover a file that includes sensitive personal data. Do you want to empty your files because you’re running out of space? You might want to check your disk space on Linux to see what’s taking up the space and delete those files next. If you can’t find an important document, you can always search for files on Linux using the find command. Comment Name * Email *
Δ Save my name and email and send me emails as new comments are made to this post.