Lesson 92 of 296
Terminal basics
What you will learn
- What a terminal and a shell are
- Paths and moving around
- Working with files and folders
- Reading files and searching
- Pipes and redirection
- Environment variables and PATH
- Keyboard shortcuts that save time
- Commands to be careful with
On this page
Terminal and shell
- The terminal is the window where you type.
- The shell is the program inside it that reads your commands and runs them. Common shells: bash (most Linux servers), zsh (the default on Mac), PowerShell (Windows).
This lesson uses bash/zsh commands. They work on Mac, Linux and in WSL (Windows Subsystem for Linux) on Windows. Most servers run Linux, so these are the commands you need for work.
The prompt
sam@laptop:~/projects$The prompt shows the user, the computer and the current folder. ~ means your home folder (for example /Users/sam on Mac or /home/sam on Linux).
Moving around
| Command | Meaning |
|---|---|
pwd | Print the current folder |
ls | List files |
ls -la | List all files, including hidden ones (names starting with .), with details |
cd projects | Go into a folder |
cd .. | Go up one level |
cd ~ or just cd | Go to your home folder |
cd - | Go back to the previous folder |
cd / | Go to the root of the whole file system |
Paths work as in Lesson 5: absolute paths start with / (or ~), relative paths start from the current folder.
Files and folders
| Command | Meaning |
|---|---|
mkdir app | Make a folder |
mkdir -p app/src/utils | Make nested folders in one go |
touch index.js | Make an empty file |
cp a.txt b.txt | Copy a file |
cp -r src backup | Copy a folder and everything in it |
mv old.txt new.txt | Rename (move) a file |
mv file.txt docs/ | Move a file into a folder |
rm file.txt | Delete a file |
rm -r folder | Delete a folder and everything in it |
rm does not use the Recycle Bin. Deleted is gone. Double-check before pressing Enter, especially with -r.
Reading files
| Command | Meaning |
|---|---|
cat file.txt | Print the whole file |
less file.txt | Read a long file page by page (press q to quit, /word to search) |
head -n 20 file.txt | First 20 lines |
tail -n 20 file.txt | Last 20 lines |
tail -f app.log | Keep showing new lines as they are added. Essential for watching server logs. |
Searching
| Command | Meaning |
|---|---|
grep "error" app.log | Lines containing "error" |
grep -i "error" app.log | Ignore case |
grep -rn "useState" src/ | Search every file in a folder; show line numbers |
find . -name "*.test.js" | Find files by name |
Pipes and redirection
| Symbol | Meaning | Example |
|---|---|---|
| | Send the output of one command into the next | cat app.log | grep "500" | wc -l (count lines with 500) |
> | Write output to a file, replacing it | echo "PORT=3000" > .env |
>> | Add output to the end of a file | echo "DEBUG=true" >> .env |
&& | Run the next command only if the first succeeded | npm install && npm test |
Getting help
man lsshows the manual for a command (pressqto quit).ls --helpshows short help for most commands.which nodeshows where a program is installed.
Environment variables and PATH
An environment variable is a named value that programs can read. Apps use them for settings and secrets (Part 15).
echo $HOME
export API_URL="https://api.example.com"
echo $API_URLexport sets it only for the current terminal session.
PATH is a special variable: a list of folders where the shell looks for programs. When you type node, the shell searches each folder in PATH. "Command not found" usually means the program is not installed, or its folder is not in PATH.
Time-saving keys
| Key | Action |
|---|---|
Tab | Auto-complete file and folder names. Press it constantly. |
Up / Down | Previous / next command |
Ctrl + R | Search your command history |
Ctrl + C | Stop the running command |
Ctrl + L or clear | Clear the screen |
Ctrl + A / Ctrl + E | Jump to the start / end of the line |
Running programs
node app.js
npm run devA server keeps running until you press Ctrl + C. To run two things at once, open a second terminal tab.
Commands to be careful with
rm -rfdeletes everything in a folder without asking. Never run it with a path you have not checked.sudoruns a command as the administrator. Only use it when you understand why it is needed. You should not needsudofornpm installin a project.- Never paste commands from the internet that you do not understand, especially ones that download and run scripts.
Common mistakes
- Running commands in the wrong folder. Check with
pwd. - Spaces in names:
cd my folderfails. Use quotes:cd "my folder", or Tab completion. >instead of>>, wiping a file.- Forgetting a server is still running in another tab, then getting "port already in use".
How to say it in an interview
"I work in the terminal daily: navigating with cd and ls, managing files, reading logs with tail -f, searching code with grep, and chaining commands with pipes and &&. I understand environment variables and PATH, which explains most 'command not found' problems, and I am careful with destructive commands like rm -rf and sudo."
Key words
| Word | Meaning |
|---|---|
| Terminal | The window where you type commands |
| Shell | The program that runs commands, such as bash or zsh |
| WSL | A way to run Linux inside Windows |
| Pipe | |, sending one command's output into another |
| Redirection | > and >>, sending output to a file |
| Environment variable | A named setting available to programs |
| PATH | The list of folders the shell searches for programs |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.