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
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap