The Command Line for Beginners: Terminal, Shell and Essential Commands
What is the command line?
The command line is a text interface where you type commands for the operating system to run, instead of clicking icons. A terminal is the window that shows the text, and a shell such as bash, zsh or PowerShell is the program that reads your commands and runs them. Developers use it to manage files, run code and automate tasks.
Terminal, shell and prompt: the key terms
- Terminal (terminal emulator): the application window, such as Windows Terminal, macOS Terminal or GNOME Terminal.
- Shell: the command interpreter running inside it. bash is common on Linux, zsh is the default on macOS, and PowerShell is the modern shell on Windows.
- Prompt: the text before your cursor, often showing the user and current folder, ending in
$(bash),%(zsh) or>(PowerShell). - Command, options and arguments: in
ls -l /etc,lsis the command,-lan option and/etcan argument.
On Windows you can also run bash through WSL (Windows Subsystem for Linux) or Git Bash, which is useful because most server documentation assumes a Linux shell.
Paths and navigation
Every file has a path. An absolute path starts from the root (/home/asha/notes.txt on Linux, C:\Users\Asha\notes.txt on Windows). A relative path starts from your current folder. . means the current folder, .. the parent and ~ your home folder.
pwd # print working directory
ls # list files
ls -la # long listing, including hidden files
cd projects # move into a folder (relative path)
cd .. # up one level
cd ~ # home folder
cd /var/log # absolute pathGetting help for any command
You rarely need to memorise options. On Linux and macOS, man ls opens the manual page and most tools also accept --help. In PowerShell, Get-Help Get-ChildItem shows the documentation for a cmdlet. Linux and macOS paths are case-sensitive and use forward slashes; Windows paths are case-insensitive and traditionally use backslashes, although PowerShell accepts forward slashes as well.
Working with files and folders
mkdir -p site/css # create folders, including parents
touch site/index.html # create an empty file
cp site/index.html backup.html
mv backup.html old.html # move or rename
cat site/index.html # print a file
less /var/log/syslog # page through a long file (q quits)
head -n 5 data.csv # first five lines
rm old.html # delete a file: no recycle bin
rm -r site # delete a folder and its contentsrm deletes permanently. Run ls on the same path first to confirm what you are about to remove. Our Linux commands guide lists many more commands for system administration.
Pipes and redirection
Every command has standard input, standard output and standard error. A pipe (|) sends one command's output into the next command's input. Redirection sends output to a file instead of the screen.
| Symbol | Meaning | Example |
|---|---|---|
| | | Pipe output to another command | ls | wc -l |
| > | Write output to a file (overwrites) | ls > files.txt |
| >> | Append output to a file | date >> log.txt |
| < | Read input from a file | sort < names.txt |
| 2> | Redirect error messages | python app.py 2> errors.txt |
| 2>&1 | Send errors to the same place as output | make > build.log 2>&1 |
Redirection works in PowerShell too, with the same |, > and >> symbols. The difference is that PowerShell pipes objects rather than plain text, so commands such as Get-ChildItem | Sort-Object Length sort by a real property instead of parsing columns.
# count how many lines in access.log contain "404"
grep "404" access.log | wc -l
# five most common IP addresses in the first column
cut -d " " -f 1 access.log | sort | uniq -c | sort -rn | head -n 5Chaining small tools like this is the core idea of the Unix command line. grep searches with patterns; our regular expressions guide explains the syntax.
Environment variables and the PATH
An environment variable is a named value that the shell passes to every program it starts. PATH is the most important one: it lists the folders the shell searches when you type a command name. If a newly installed tool gives "command not found", its folder is usually missing from PATH.
# bash / zsh
echo $HOME
echo $PATH
export APP_ENV=development # set for this session
APP_ENV=test python app.py # set for one command only
# PowerShell
$env:Path
$env:APP_ENV = "development"Variables set this way last only for the current terminal session. To make them permanent, add the export line to ~/.bashrc or ~/.zshrc, or use the Windows environment variables settings. Keep secrets such as API keys out of scripts you commit to Git.
PowerShell and bash equivalents
PowerShell defines aliases such as ls, cd and cat, so many basic commands look familiar, but the underlying cmdlets work on objects rather than text and accept different options.
| Task | bash | PowerShell |
|---|---|---|
| Current folder | pwd | Get-Location |
| List files | ls -la | Get-ChildItem -Force |
| Change folder | cd path | Set-Location path |
| Show a file | cat file.txt | Get-Content file.txt |
| Copy | cp a b | Copy-Item a b |
| Move or rename | mv a b | Move-Item a b |
| Delete folder | rm -r dir | Remove-Item dir -Recurse |
| New folder | mkdir -p a/b | New-Item -ItemType Directory a/b |
| Search text | grep "error" app.log | Select-String "error" app.log |
| Read a variable | echo $HOME | $env:USERPROFILE |
| Find a command | which python3 | Get-Command python |
Practical exercise: organise a project folder from the terminal
- Open a terminal and use
cd ~thenmkdir -p practice/logs practice/srcto create a folder structure. - Move into
practiceand create three files insrcwithtouch(orNew-Itemin PowerShell). - Run
ls -R > inventory.txtand view the result withcat. - Append the date with
date >> inventory.txt. - Use a pipe to count the entries in
src:ls src | wc -l. - Set an environment variable called
PROJECTtopracticeand print it.
- Done looks like this:
inventory.txtlists both folders and the three files with a date at the end, and the count prints 3. - Extension: repeat every step in PowerShell using the table above.
The command line is central to working with servers and networks; the Networking Foundation course uses it throughout, and debugging techniques often start with reading terminal output.
Common mistakes
- Running
rm -ron a path without checking it first, since the shell has no undo. - Using
>when you meant>>, which overwrites a file instead of appending to it. - Forgetting to quote paths that contain spaces, so the shell treats them as two separate arguments.
- Pasting commands from the internet with
sudowithout understanding what they do. - Expecting an
exportor$env:setting to survive after the terminal is closed.
Frequently asked questions
Is Command Prompt the same as PowerShell?
No. Command Prompt (cmd.exe) is the older Windows shell with its own commands such as dir and copy. PowerShell is newer, object-based and far more capable for scripting.
What is the difference between bash and zsh?
Both are Unix shells with very similar syntax for everyday commands. zsh adds richer completion and customisation and is the default on macOS; bash is the usual default on Linux servers.
What does sudo do?
On Linux and macOS, sudo runs a single command with administrator rights after asking for your password. Use it only when a command genuinely needs those rights.
Do I need the command line to learn programming?
You can start in an editor, but you will soon need it to run scripts, install packages, use Git and deploy code, so learning the basics early pays off.