Code / Command Line & Git / Unit 2: Making & changing files

Write and read files फ़ाइल लिखें और पढ़ें

Lesson 4 of 12 · about 12 minutes · echo, > to write, >> to add a line, cat to read.

⚡ 0 XP

Step 1 of 2

echo, > and >>

movemoveturnpick

echo "hello" prints text. Add an arrow and the text goes into a file instead:

  • echo "Masala chai ₹10" > menu.txt — one arrow writes: it creates the file, or wipes it and starts fresh.
  • echo "Ginger chai ₹15" >> menu.txt — two arrows add: a new line at the end, keeping what was there.
  • cat menu.txt shows what is inside a file.

Careful: a single > on an existing file throws away its old text. When in doubt, use >>.

Example
echo "Poha ₹20" > tiffin.txt
echo "Upma ₹25" >> tiffin.txt
cat tiffin.txt

🎯 Challenge: Write the chai menu +15 XP

In your home folder, create menu.txt with exactly two lines:

  1. Masala chai ₹10
  2. Ginger chai ₹15

Write the first line with >, add the second with >>, then show the file with cat menu.txt.

Try twice to unlock the solution (0/2)

terminalCtrl ⏎

A safe practice terminal. Type a command and press Enter. Type help to list commands. Tab completes names, ↑ recalls the last command.

Files · ~ = /home/asha

Your home folder is empty.

Git timeline

No git repository yet. Start with git init.

Checklist

Press Run to check your code.

  • menu.txt has exactly the two lines
  • You added a line with >>
  • You read it with cat
Lesson notes (text version)

1. echo, > and >>

echo "hello" prints text. Add an arrow and the text goes into a file instead:

  • echo "Masala chai ₹10" > menu.txt — one arrow writes: it creates the file, or wipes it and starts fresh.
  • echo "Ginger chai ₹15" >> menu.txt — two arrows add: a new line at the end, keeping what was there.
  • cat menu.txt shows what is inside a file.

Careful: a single > on an existing file throws away its old text. When in doubt, use >>.

                echo "Poha ₹20" > tiffin.txt
echo "Upma ₹25" >> tiffin.txt
cat tiffin.txt
              

Challenge: Write the chai menu

In your home folder, create menu.txt with exactly two lines:

  1. Masala chai ₹10
  2. Ginger chai ₹15

Write the first line with >, add the second with >>, then show the file with cat menu.txt.

2. Overwrite on purpose

Sometimes wiping a file is what you want — like changing yesterday's notice board. One > replaces everything; then >> adds more lines below.

Real developers use this all the time to create small config and note files straight from the terminal.

Challenge: Update the notice board

notice.txt still says "Closed today". Make it contain exactly these two lines:

  1. Open 7am to 9pm
  2. Diwali special: kesar chai

The old text must be gone. Check with cat notice.txt.

Search by task, format or tool name.