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: 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 (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

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