How-To

How to Nullify a File in Linux

Linux featured

Nullifying a file allows you to reduce its size and free up files taking up space on your hard drive. Learn how to easily nullify a file in Linux here.

Files, like log files, can easily and quickly grow taking up space on your hard drive. Before you realize it, you’re running low on hard drive space. You can easily nullify a file (or remove the content from a file) in Linux to shrink its size if you have limited hard drive space.

For example, the operating system and some programs may use log files to track activity and issues. These files can grow large over time, and you may not need to keep all the previous content.

Instead of opening the file and selecting all the content to delete it, you can use various command-line tools to nullify a file quickly and easily.

WARNING: Be aware that the Linux system consists of files. Be sure that you are not nullifying any important user or system files when removing content from files. Removing the content from a critical system or configuration file could cause fatal system errors or system failure.

If you want to know how to nullify a file in Linux, follow our guide below.

How to Check the Size of a File in Linux

Throughout the following procedures, we use a command to check the size of the file we want to nullify. You can use this command to see the size of the file before and after nullifying a file.

To check the size of a file in Linux, follow these steps:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command. Replace the path in the command, with the path to the file for which you want to check the size.
    du -sh / home/lori/Documents/nullify-this-file.txt

    Using the du command to check the size of a file in Linux

How to Nullify a File in Linux Using the truncate Command

One easy way to nullify a file in Linux is using the truncate command. This command allows you to shrink or extend the size of a file to a specific size.

To nullify a file using the truncate command in Linux, follow these steps:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command. Replace the path in the command, with the path to the file you want to nullify.
    truncate -s 0 /home/lori/Documents/nullify-this-file.txt

    Using the truncate command in Linux

    NOTE: If there is a space in the directory name or file name, put quotation marks around the entire path.
    In the above command, “-s” is used to set the size of the file, in bytes. When you use “-s 0”, it means you set the file size to 0 bytes, removing all content from the file.

How to Nullify a File in Linux by Redirecting to Null

Another easy way to nullify a file in Linux is to redirect the file to null (a non-existent object).

To nullify a file by redirecting it to null in Linux, follow these steps:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command. Replace the path in the command, with the path to the file you want to nullify.
    > /home/ lori/Documents/nullify-this-file.txt

    Redirect to null in Linux

    NOTE: If there is a space in the directory name or file name, put quotation marks around the entire path.

How to Nullify a File in Linux Using the cat Command with /dev/null

/dev/null is a virtual device available in every Linux system. This device provides data for applications. But instead of the data coming from a physical device, like a hard drive, the data comes from the operating system itself.

The /dev/null (or null) device is used for writing instead of reading. Writing /dev/null to a file clears all the content of that file.

To nullify a file using the cat command with the /dev/null device in Linux, follow these steps:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command. Replace the path in the command, with the path to the file you want to nullify.
    cat /dev/null > /home/ lori/Documents/nullify-this-file.txt

    Redirect /dev/null to file in Linux

    NOTE: If there is a space in the directory name or file name, put quotation marks around the entire path.
  3. You can also use the cat command without the null device to nullify a file.
  4. Type the following command. Replace the path in the command, with the path to the file you want to delete.
    cat /dev/null > /home/lori/Documents/nullify-this-file.txt
  5. Then, press Ctrl + D.
    This puts everything you type after the above command into the file until Ctrl + D is pressed. Because we didn’t type anything after the command and before pressing Ctrl + D, nothing is written to the file. Anything that was previously in the file is deleted and replaced with nothing.Redirect to /dev/null using the cat command in Linux

How to Nullify a File in Linux Using the echo Command

The echo command is a simple command that prints, or echoes, what you give it to display.

You can use the echo command with an empty string and redirect it to the file you want to nullify.

To nullify a file using the echo command, follow these steps:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command. Replace the path in the command, with the path to the file you want to nullify.
    echo “” > /home/ lori/Documents/nullify-this-file.txt

    Using the echo command with empty quotes in Linux

    NOTE: If there is a space in the directory name or file name, put quotation marks around the entire path.
  3. The “” indicates an empty string. You can also use the echo command without the empty string to nullify a file, as shown below:
    echo > /home/ lori/Documents/nullify-this-file.txt
  4. Notice that the file size did not change when we wrote an empty string to the file. An empty string is not the same as null. A string is an object even if it’s empty. Null is a non-existent object.
  5. To use the echo command to nullify a file and reduce the file size, use “-n” with the echo command. This tells echo to not output the trailing newline that causes the empty line.
    echo -n > /home/ lori/Documents/nullify-this-file.txt

    Using the echo command with null output in Linux

  6. Now the file size has changed to 0.

How to Nullify a File in Linux Using the touch and mv Commands

You can also use the touch and mv commands to create an empty file and replace the file you want to nullify with the empty file.

To nullify a file using the touch and mv commands in Linux, follow these steps:

  1. Press Ctrl + Alt + T to open a Terminal window.
  2. Type the following command. Replace the path and filename in the command, with the path and filename for the empty file you want to create.
    touch /home/ lori/Documents/empty-file.txt
    NOTE: If there is a space in the directory name or file name, put quotation marks around the entire path.
  3. You can use the du command to check the size of the file you created.
    du -sh /home/ lori/Documents/empty-file.txt
  4. Then, type the following command to replace the file you want to nullify with the empty file. Replace the path and filename with the path and filename for the file you want to nullify.
    mv /home/ lori/Documents/empty-file.txt /home/ lori/Documents/nullify-this-file.txt

    Using the touch and mv commands in Linux

Nullify a File in Linux Quickly and Easily Using the Command Line

The command line in Linux provides a quick and easy way to perform tasks—like nullifying a file. Of course, you could also open the file in a text editor like gedit, select all the text, delete it, and save the file. The terminal provides a quicker way to do this, however.

If you want to delete a file that contains sensitive information instead of just nullifying a file, you can securely delete the file so it’s not recoverable.

Click to comment

Leave a Reply

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

 

To Top