How to Clear the Contents of a File from the Command Line in Ubuntu 22.04?


Ubuntu 22.04 offers a powerful command line interface (CLI) capable of performing all operations, such as file management and administrative tasks effectively. While dealing with large files via the command line, a user might want to clear the contents of a file without opening or deleting the file. This article will demonstrate the following methods of clearing the contents of the file via the Command Line in Ubuntu 22.04 LTS.

  • Redirection (>) Operator.
  • True Redirection Operator (:>).
  • Truncate Command.
  • echo Command.
  • /dev/null Device File.
  • Cat Command.
  • Cp Command.

How to Clear/Erase the Contents of a File From the Command Line via Redirection (>) Operator?

A redirection (>) operator is used mainly to redirect the output of previous commands to a file, overwrite a file if the file exists already, and if the file does not exist, the redirection operator creates a new file. To clear the contents of a file “File1.txt” of 8632 bytes via the redirection (>) operator, run the following command:


It can be verified from the output, that the size of File1.txt is 0 bytes.

How to Clear/Erase the Contents of a File From the Command Line via True Redirection Operator?

True and the symbol “:” are equivalent commands in Linux/Ubuntu Terminal. To clear the contents of a file “File1.txt” of 8632 bytes via the “:” symbol, run the following command:


The above command directs the output of “:”, i.e., 0 to “File1.txt”. It can be verified from the output, that the size of File1.txt is 0 bytes. Alternatively, To clear the contents of a file “File1.txt”, that is 8632 bytes via true command, run the following command:


The above command directs the output of “true”, i.e., 0 to “File1.txt”. It can be verified from the output, that the size of File1.txt is 0 bytes.

How to Clear/Erase the Contents of a File from the Command Line via the Truncate Command?

The “truncate” command enables the reduction of file size. To clear the contents of a file “File1.txt” of 8632 bytes via the “truncate” command, run the following command:


Where “-s”: size of file flag.

The above command sets the size flag to 0, i.e., empty file. It can be verified from the output, that the size of File1.txt is 0 bytes.

How to Clear/Erase the Contents of a File from the Command Line via the Echo Command?

The echo command is used to create, overwrite, and append text files. To clear the contents of a file “File1.txt” of 8632 bytes via the “echo” command, run the following command:

$ echo -n “” > File1.txt

Where “n” restricts the trailing newline:

The above command overwrites the File1.txt with an empty file. It can be verified from the output, that the size of File1.txt is 0 bytes. Alternatively, to clear the contents of a file “File1.txt” of 8632 bytes via the “echo” command, run the following command:


The above command overwrites File1.txt with an empty file. It can be verified from the output, that the size of File1.txt is 0 bytes.

How to Clear/Erase the Contents of a File from the Command Line via /dev/null?

The “/dev/null” is a device file. /dev/null clears any input sent to it, i.e., the output of the “/dev/null” file is an empty file. To clear the contents of a file “File1.txt” of 8632 bytes via the “/dev/null” command, run the following command:

$ cat /dev/null > File1.txt

The /dev/null returns an empty file, which is then directed to File1.txt, i.e., overwriting File1.txt with an empty file. It can be verified from the output, that the size of File1.txt is 0 bytes. Alternatively, the cp (copy) command can be used in combination with the/dev/null file to copy the output of the /dev/null file to File1.txt. To clear the contents of a file “File1.txt” of 8632 bytes via the “/dev/null” command, run the following command:


The above output verifies that the size of File1.txt is 0 bytes.

How to Clear/Erase the Contents of a File from the Command Line via the Cat Command?

The cat (concatenate) is used to create and view the file. To clear the contents of a file “File1.txt” of 8632 bytes via the “cat” command, run the following command:


From the above image, it can be seen that the cat command is used to create a new file. Ctrl+C is then pressed to create an empty file, which is then redirected to File1.txt. Thus, File1.txt is overwritten by an empty file. It can be verified from the output, that the size of File1.txt is 0 bytes.

How to Clear/Erase the Contents of a File from the Command Line via the Cp Command?

The cp (copy) command can be used to overwrite the file, i.e., File1.txt with an empty file by first, creating an empty file via touch command and then overwriting the file with the empty file. To clear the contents of a file “File1.txt” of 8632 bytes via the “cp” command, run the following command:

$ touch Emptyfile.txt
$ cp EmptyFile.txt File1.txt


It can be verified from the output, that the size of File1.txt is 0 bytes.

Conclusion

The contents of large files can be cleared by a single command via redirection (>) and true redirection(:>) operators. Additionally, truncate, echo, cat, and cp commands can also be used along with /dev/null device files to clear the contents of large files without opening or deleting them. This article demonstrated seven ways of clearing the contents of a file via the Command Line in Ubuntu 22.04 LTS.

Print Friendly, PDF & Email