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.
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.logRedirecting nothing (the null object) to the file overwrites it with no data.
2. Use the built‑in true command
# : > access.log
OR
# true > access.logThe 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.logThe 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.logEchoing 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.logThe truncate command reduces the file size to the specified length; using -s 0 clears the file completely.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
