How-To

How to Empty a File in Linux

Delete featured

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 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:

  1. 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.
  2. In the terminal window, type: cat /dev/null > /home/document/path and press Enter. Replace /home/document/path with the correct file path to the file you want to empty. If the file path contains spaces, wrap the whole path in quote marks.
    Empty a file in Linux using the cat command
  3. If the file is protected, use sudo (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

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:

  1. Open the terminal on your Linux PC. You can press Ctrl + T on your keyboard to open it on most Linux distributions.
  2. In the terminal, type: echo “” > /home/document/path and press Enter. Replace /home/document/path with the correct file path and wrap the directory in quote marks if it contains spaces.
    Empty a Linux file using echo
  3. If the file is protected, use sudo to overwrite the file (e.g., sudo echo…) but take 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 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:

  1. Open the terminal on your Linux PC. If your distribution supports it, press Ctrl + T to do this.
  2. In the terminal window, type: cat > /home/document/newpath and press Enter. Replace /home/document/newpath with a suitable location for the temporary file.
  3. Press Ctrl + C to stop the running cat command.
  4. 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.
    Emptying a Linux file using the cp command
  5. If the file you’re replacing is protected, use sudo to overwrite it (e.g., sudo cp…) but make sure to take extreme care doing this.

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:

  1. Open the terminal on your Linux PC. You may be able to press Ctrl + T to open this.
  2. In the terminal, type: truncate -s 0 /home/document/path and press Enter. Make sure to replace /home/document/path with the correct path to the file you want to empty.
    Emptying a Linux file using the truncate command
  3. If the file is protected, use sudo 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.

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.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

 

To Top