Bash Basics

Lesson 2 of 8

Gathering Resources

Concept:

touch β€” creates empty files. The name comes from the Unix idea of "touching" a file's timestamp, but if the file doesn't exist, it creates one. Like tapping something into existence.
echo β€” prints text to the screen. Named because it echoes your words back at you.
> β€” the redirect operator. Instead of printing to screen, it pours output into a file. echo "hello" > file.txt creates (or overwrites) file.txt with "hello".
cat β€” short for "concatenate" (joining files together), but most people use it to simply display a file's contents on screen.
cp β€” copies files: cp source dest. The original stays untouched.
mv β€” moves OR renames files: mv old new. Same command, two uses.
rm β€” removes files permanently. No trash can, no undo. Gone means gone.

Tip: use ; to chain commands on one line β€” echo hi > a.txt; cp a.txt b.txt does both in sequence.

Terminal: Good β€” you've set up camp. Now you need supplies. In this world, files are your resources. 'touch' creates an empty one β€” you're literally touching a file into existence, like conjuring a blank notebook from thin air.
You: So 'touch water.txt' creates a file called water.txt? What good is an empty notebook?
Terminal: You fill it with 'echo'. 'echo "fresh spring"' prints text to the screen. But the magic is the '>' operator β€” it redirects that text INTO a file. 'echo "fresh spring" > water.txt' pours your words into water.txt. Think of '>' as an arrow pointing where the text should go.
You: And how do I read it back?
Terminal: 'cat water.txt' β€” short for 'concatenate', but think of it as the file coughing up its contents onto your screen. Quick and dirty, no editor needed. Now the critical part: 'cp' duplicates resources. 'cp water.txt reserve.txt' β€” instant backup. 'mv' moves or renames. And 'rm'...
You: Let me guess β€” remove?
Terminal: Permanently. No recycling bin out here in the wilderness. Once something is gone, it's gone. Like fire consuming paper. Always make backups before deleting.
You: What if I need to do two things in a row?
Terminal: Use a semicolon β€” ';'. It chains commands on one line: 'echo hello > note.txt; cat note.txt' creates the file and reads it back, one after the other. Think of ';' as saying 'do this, then do that.' Now β€” create a signal file with 'SOS' inside and back it up. We might need to call for help.
Example Code:
touch notes.txt
echo "Hello World" > notes.txt
cat notes.txt
# Hello World

cp notes.txt backup.txt
mv backup.txt archive.txt
rm notes.txt

Your Assignment

Create a file called 'signal.txt' with the text 'SOS' inside it, then make a copy called 'backup.txt'.

Bash Console
bash>