Bash Command to Empty a File in Terminal

Caution: This command will erase all content of the file, so be very sure of what you are doing.

If you want to erase all contents of a file then you can make use of the truncate command on the bash shell.

truncate – truncate, extend the length of files, or perform space management in files

Usage:
usage: truncate [-c] -s [+|-|%|/]size[K|k|M|m|G|g|T|t] file ...
       truncate [-c] -r rfile file ..
Example 1:
truncate -s 0 data.txt

Another quick way to empty a file is by redirecting an empty string to the file.

Example 2:
> data.txt
Examples - How to truncate or empty a file in bash shell

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!