The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 228,004β€…    Votes:  5β€…
Tags: command-line   bash   files  
Link: πŸ” See Original Answer on Ask Ubuntu πŸ”—

Title: How to clear text in a file?
ID: /2018/03/20/How-to-clear-text-in-a-file_
Created: March 20, 2018    Edited:  June 12, 2020
Upload: November 24, 2022    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false

Not the shortest answer but…

This answer is based on another from Super User. Although not the shortest bash command, truncate is the most readable for average newbies:

$ echo Hello > Hello.txt
$ echo World! >> Hello.txt
$ cat Hello.txt
$ truncate -s 0 Hello.txt
$ ll Hello.txt
-rw-rw-r-- 1 rick rick 0 Mar 20 17:32 Hello.txt

Parameters used with truncate command here:

Clear everything except first 10,000 bytes

An advantage of truncate is you can specify how much to keep, not just zero:

$ truncate -s 10000 Hello.txt

… will truncate everything after the first 10,000 bytes. This could be useful if a program went crazy and dumped many Megabytes of data into a small log file:

⇧ How do I restrict my kids' computing time? HDMI audio disabled after resuming from sleep  β‡©