This is a very important distinction in Linux! Both symbols are used to redirect command output to a file, but they treat the existing content of that file differently:
1. The > Operator (Overwrite)
The single arrow overwrites the destination file.
- If the file doesn't exist, it creates it.
- If the file already exists, it wipes out everything inside and replaces it with the new content.
Think of it like: Replaying a video tape. The old recording is gone, and the new one starts at the beginning.
echo "Apple" > fruit.txt
echo "Banana" > fruit.txt
# If you run 'cat fruit.txt', you will ONLY see "Banana".
2. The >> Operator (Append)
The double arrow appends to the destination file.
- If the file doesn't exist, it creates it.
- If the file already exists, it adds the new content to the very end of the file, preserving the original data.
Think of it like: Adding a new line to a grocery list. You don't erase the items you already wrote; you just add to the bottom.
echo "Apple" > fruit.txt
echo "Banana" >> fruit.txt
# If you run 'cat fruit.txt', you will see both "Apple" and "Banana".
Summary Table
| Symbol | Action | What happens to existing content? |
|---|---|---|
> |
Overwrite | Deleted and replaced. |
>> |
Append | Kept; new data added to the end. |
Labby's Warning: Be careful with >. If you accidentally use it on an important configuration file, the original data will be lost immediately! Use >> if you just want to add logs or notes to a file.