Fundamentals 3 min read

5 Quick Linux Commands to Empty a File Instantly

Learn five simple Linux shell techniques—including redirection to /dev/null, the true command, cat/cp/dd utilities, echo variations, and truncate—to efficiently clear a file’s contents without deleting the file itself, all explained with clear examples and code snippets.

Open Source Linux
Open Source Linux
Open Source Linux
5 Quick Linux Commands to Empty a File Instantly

This guide shows how to clear the contents of a file in Linux using five straightforward command‑line methods.

1. Redirect to null to empty a file

# > access.log

Redirecting nothing (the null object) to the file overwrites it with no data.

2. Use the built‑in true command

# : > access.log
OR
# true > access.log

The colon (:) is a shell built‑in equivalent to the true command; redirecting its output clears the file.

3. Use cat, cp, or dd with /dev/null

# cat /dev/null > access.log
# cp /dev/null access.log
# dd if=/dev/null of=access.log

The special /dev/null device discards any input, and redirecting its output to a file empties the file.

4. Use echo to write an empty string

# echo "" > access.log
OR
# echo > access.log
# echo -n "" > access.log

Echoing an empty string (or using the -n flag to suppress the newline) overwrites the file with nothing.

5. Use truncate to set file size to zero

# truncate -s 0 access.log

The truncate command reduces the file size to the specified length; using -s 0 clears the file completely.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxTRUNCATEDevNullFile Clearing
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.