When to use `>` versus `>>`?

QuestionsQuestions8 SkillsProDec, 18 2025
0100

That's a great question! Choosing between > and >> depends entirely on whether you want to keep or discard the existing data in a file.

Here is a simple breakdown to help you decide:

Use > (Overwrite) when:

  • Creating a new file: You want to start a file from scratch.
  • Replacing content: You have updated information and the old data is no longer needed (e.g., updating a configuration setting).
  • Emptying a file: You can actually use > filename by itself to quickly wipe a file's contents without deleting the file.
  • Example: echo "Last updated: Oct 24" > version.txt (You only care about the latest version).

Use >> (Append) when:

  • Logging: You are tracking events over time and want to keep a history (e.g., recording every time a script runs).
  • Building a list: You are gathering data from multiple commands and want them all saved in one place.
  • Protecting data: You want to add information to a file but are afraid of accidentally deleting what is already there.
  • Example: date >> login_history.log (You want to see every login time, not just the last one).

Summary Table

Operator Action Risk
> Overwrites existing content Higher (Old data is lost forever)
>> Appends to the end Lower (Old data is preserved)

Pro Tip: If you are unsure, it's safer to use >>. You can always delete lines later, but you can't recover data deleted by >

0 Comments

no data
Be the first to share your comment!