How to empty the contents of a file in Linux?

There are several ways in which you can clear the content of a file:

  1. Utilize redirection symbols:
> 文件名

– It is raining cats and dogs outside.

– It is pouring outside.

> example.txt
  1. Utilize the cat command along with redirection symbols:
cat /dev/null > 文件名

Can you help me with this task?

Could you assist me with this assignment?

cat /dev/null > example.txt
  1. Combine the echo command with redirection symbols:
echo "" > 文件名

Original: 我每天早上都会去晨跑。
Paraphrased: I go for a morning run every day.

echo "" > example.txt
  1. Using the truncate command:

truncate -s 0 文件名

示例:

Can you please explain the concept in a simpler way?

truncate -s 0 example.txt

Any of the methods above can be used to clear the content of the file. Just choose one option.

bannerAds