Large files on Linux can take up unnecessary disk space. If you want to empty a file on Linux, follow the steps in this guide.
The more we use a PC, the more files we collect. Some of them are important, while others aren’t, and it’s important to know how (and when) to remove them.
If you want to clear the contents of 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 thecatcommand. This command 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 toredirect the contents of /dev/null to overwrite a file.
In Linux and other Unix variants,/dev/null is a special device used as a placeholder. It allows apps (and users) to write content to it to erase that content. 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 usecatto empty a file on Linux:
- Open the terminal on your Linux PC. If you’re running a Linux distribution with a GUI, you can usually do this by pressing Ctrl+T on your keyboard.
- In the terminal window, type: cat /dev/null > /home/document/path and press Enter. Replace/home/document/pathwith the correct file path to the file you want to empty. If the file path contains spaces, wrap the whole path in quote marks.
- If the file is protected, usesudo (e.g., sudo cat /dev/null > /home/document/path) to overwrite the file.Take extreme care doing this, however, as Linux won’t stop to check if you overwrite important files—it’ll just do it.
How to Use the echo Command to Empty Linux Files
Theecho 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.
- Open the terminal on your Linux PC. You can press Ctrl+T on your keyboard to open it on most Linux distributions.
- In the terminal, type: echo "" > /home/document/path and pressEnter. Replace/home/document/pathwith the correct file path and wrap the directory in quote marks if it contains spaces.
- If the file is protected, usesudo to overwrite the file (e.g., sudo echo…) buttake extreme care doing this.
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 forcopy, allowing you to copy and replace larger files with empty ones.
Usingcat, you can create a new (empty) file. You can then usecp to move this file and overwrite the larger one.
To use cp to empty a file on Linux:
- Open the terminal on your Linux PC. If your distribution supports it, press Ctrl+T to do this.
- In the terminal window, type: cat > /home/document/newpath and press Enter. Replace/home/document/newpath with a suitable location for the temporary file.
- PressCtrl+C to stop the running catcommand.
- Next, type: cp /home/document/newpath /home/document/oldpath and press Enter. Replace the first path (newpath) with the new and empty file’s location and the second path (oldpath) with the location of the file you want to erase.
- If the file you’re replacing is protected, use sudo to overwrite it (e.g., sudo cp…) but make sure totake extreme care doing this.
Truncating Files on Linux
Thetruncate command on Linux allows you to reduce files to a specific size. If you use truncatewith an output size of0, you can completely empty a file.
To empty a file on Linux using the truncate command:
- Open the terminal on your Linux PC. You may be able to pressCtrl+T to open this.
- In the terminal, type: truncate -s 0 /home/document/path and pressEnter. Make sure to replace/home/document/path with the correct path to the file you want to empty.
- If the file is protected, usesudo instead to overwrite it (e.g., sudo truncate -s…), but take extreme care doing this.
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.
Click to comment
Leave a Reply
Related Items:linux
Recommended for you
Boost Your Productivity: Learn to Copy and Paste in Ubuntu Terminal Today
How to Add or Delete a User in Ubuntu
How to Install Java on Ubuntu