Skip to the content

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

CommandMeaning
pwdPrint the current folder
lsList files
ls -laList all files, including hidden ones (names starting with .), with details
cd projectsGo into a folder
cd ..Go up one level
cd ~ or just cdGo 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

CommandMeaning
mkdir appMake a folder
mkdir -p app/src/utilsMake nested folders in one go
touch index.jsMake an empty file
cp a.txt b.txtCopy a file
cp -r src backupCopy a folder and everything in it
mv old.txt new.txtRename (move) a file
mv file.txt docs/Move a file into a folder
rm file.txtDelete a file
rm -r folderDelete 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

CommandMeaning
cat file.txtPrint the whole file
less file.txtRead a long file page by page (press q to quit, /word to search)
head -n 20 file.txtFirst 20 lines
tail -n 20 file.txtLast 20 lines
tail -f app.logKeep showing new lines as they are added. Essential for watching server logs.

Searching

CommandMeaning
grep "error" app.logLines containing "error"
grep -i "error" app.logIgnore 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

SymbolMeaningExample
|Send the output of one command into the nextcat app.log | grep "500" | wc -l (count lines with 500)
>Write output to a file, replacing itecho "PORT=3000" > .env
>>Add output to the end of a fileecho "DEBUG=true" >> .env
&&Run the next command only if the first succeedednpm install && npm test

Getting help

  • man ls shows the manual for a command (press q to quit).
  • ls --help shows short help for most commands.
  • which node shows 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_URL

export 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

KeyAction
TabAuto-complete file and folder names. Press it constantly.
Up / DownPrevious / next command
Ctrl + RSearch your command history
Ctrl + CStop the running command
Ctrl + L or clearClear the screen
Ctrl + A / Ctrl + EJump to the start / end of the line

Running programs

node app.js
npm run dev

A 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 -rf deletes everything in a folder without asking. Never run it with a path you have not checked.
  • sudo runs a command as the administrator. Only use it when you understand why it is needed. You should not need sudo for npm install in 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 folder fails. 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

WordMeaning
TerminalThe window where you type commands
ShellThe program that runs commands, such as bash or zsh
WSLA way to run Linux inside Windows
Pipe|, sending one command's output into another
Redirection> and >>, sending output to a file
Environment variableA named setting available to programs
PATHThe 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.