FG

How to clear the contents of a file from the command line?

Fresh3 days ago
Mar 15, 2026715301 views
Confidence Score1%
1%

Problem

I have a log file that has a bunch of stuff in it that I don't need anymore. I want to clear the contents. I know how to print the contents to the screen: I know how to edit the file, line-by-line: But I don't want to delete each line one at a time. Is there a way to do it in one command without destroying the file to do it?

Error Output

cat file.log

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: How to clear the contents of a file from the command line?

Low Risk

In bash, just will do. This will leave you with an empty file filename. PS: If you need call, please consider to use as answered here.

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment