Mastering Command Line Interface Basics for Beginners
Command Line Interfaces (CLIs) let you control your computer by typing commands instead of clicking buttons. This can feel intimidating at first, but it quickly becomes one of the most powerful skills you can learn: it’s faster for repetitive tasks, works well over remote connections, and gives you precise control over files, programs, and automation.
This tutorial is a practical, beginner-friendly guide with deep explanations and real commands you can run. It focuses primarily on Linux/macOS shells (Bash/Zsh) and includes Windows equivalents where helpful.
1) What a CLI is (and why it matters)
A CLI is a text-based interface where you type commands into a shell. The shell reads your command, runs programs, and prints output.
- Terminal: the window/app that shows the CLI (e.g., Terminal on macOS, GNOME Terminal on Linux, Windows Terminal on Windows).
- Shell: the program interpreting what you type (e.g.,
bash,zsh,fish, PowerShell). - Command: what you type, usually a program name plus options and arguments.
Why it matters:
- Speed: rename/move thousands of files in seconds.
- Automation: scripts make repeatable processes.
- Remote work: servers are often managed via SSH in a terminal.
- Transparency: you can see exactly what happened and reproduce it.
2) Opening a terminal and identifying your shell
Linux
Open your terminal from the application menu. Common shells: bash, zsh.
macOS
Open Terminal (Applications → Utilities → Terminal). Default shell is often zsh.
Windows
You have options:
- PowerShell (modern default)
- Command Prompt (older)
- WSL (Windows Subsystem for Linux) gives you a real Linux shell
To see what shell you’re using:
Linux/macOS:
echo $SHELL
To see your current user:
whoami
To see your current directory (where you are “standing” in the filesystem):
pwd
3) Understanding command structure: program, options, arguments
Most commands follow this pattern:
command [options] [arguments]
Example:
ls -l /etc
lsis the program (lists directory contents)-lis an option (long listing format)/etcis an argument (the directory to list)
Options can be:
- Short:
-l,-a,-h - Long:
--all,--human-readable
Many commands let you combine short options:
ls -la
4) Your filesystem: paths, directories, and navigation
Absolute vs relative paths
-
Absolute path starts from the filesystem root:
- Linux/macOS:
/home/alex/Documents - Windows (PowerShell):
C:\Users\Alex\Documents
- Linux/macOS:
-
Relative path starts from your current directory:
Documents./Documents(explicitly “from here”)
Special path symbols (Linux/macOS shells):
.means “current directory”..means “parent directory”~means “your home directory”/is the root directory
Moving around: cd
Go to your home directory:
cd
Go to a directory by path:
cd /usr/local
Go up one level:
cd ..
Go to a directory relative to where you are:
cd ./Downloads
Go back to the previous directory:
cd -
Listing files: ls
Basic listing:
ls
Long listing (permissions, owner, size, time):
ls -l
Include hidden files (those starting with .):
ls -a
Combine options:
ls -lah
-hmakes sizes human-readable (KB/MB/GB)
5) Creating, copying, moving, and deleting files
Create directories: mkdir
Create one directory:
mkdir projects
Create nested directories in one go:
mkdir -p projects/cli-tutorial/notes
Create empty files: touch
touch hello.txt
If the file exists, touch updates its timestamp.
Copy files/directories: cp
Copy a file:
cp hello.txt hello-copy.txt
Copy a directory recursively:
cp -r projects projects-backup
Move/rename: mv
Rename a file:
mv hello.txt greeting.txt
Move a file into a directory:
mv greeting.txt projects/
Delete: rm (be careful)
Remove a file:
rm greeting.txt
Remove a directory and its contents:
rm -r projects
Force removal without prompts (dangerous):
rm -rf projects
Safety tips:
- Use
lsfirst to confirm what you’re targeting. - Consider interactive mode:
rm -i important.txt - If you’re new, avoid
rm -rfuntil you fully understand it.
6) Viewing and editing text in the terminal
Print file contents: cat, less
View a file quickly:
cat /etc/hosts
For large files, use less (scrollable):
less /var/log/system.log
In less:
- Use arrow keys or Page Up/Down
- Press
/to search, type text, press Enter - Press
nfor next match - Press
qto quit
Show first/last lines: head, tail
First 10 lines:
head -n 10 file.txt
Last 10 lines:
tail -n 10 file.txt
Follow a file as it grows (great for logs):
tail -f /var/log/system.log
Editing files: nano (beginner-friendly)
Open a file in nano:
nano notes.txt
Inside nano:
- Edit text normally
Ctrl + Oto save (write out)Ctrl + Xto exit
(If you later want a more advanced editor, vim is powerful but has a learning curve.)
7) Understanding permissions (read, write, execute)
On Linux/macOS, files have permissions controlling who can read/write/execute them.
Run:
ls -l
You might see something like:
-rwxr-xr-- 1 alex staff 1234 Jan 10 12:00 script.sh
Breakdown:
- First character: file type (
-file,ddirectory,lsymlink) - Next 9 characters: permissions in groups of three:
- Owner:
rwx - Group:
r-x - Others:
r--
- Owner:
Meanings:
r= readw= writex= execute (run as a program)
Changing permissions: chmod
Make a script executable:
chmod +x script.sh
Remove execute permission:
chmod -x script.sh
Set exact permissions with numeric mode:
7=rwx6=rw-5=r-x4=r--
Example: owner can read/write, group and others can read only:
chmod 644 file.txt
Example: executable script for owner, readable/executable for others:
chmod 755 script.sh
Changing ownership: chown (often requires admin)
sudo chown alice:staff file.txt
8) Running commands as administrator: sudo
Some actions require elevated privileges (installing software, changing system files).
Example:
sudo mkdir /opt/myapp
sudoasks for your password (on most systems).- Use it only when needed.
- Prefer editing files in your home directory when possible.
9) Getting help: man, --help, and examples
Manual pages: man
man ls
Navigate:
- Arrow keys to scroll
/patternto searchqto quit
Built-in help
Many commands support:
ls --help
Or for some shells/programs:
command -h
Practical approach to learning a command
- Skim
--helpto see common options. - Try a simple example.
- Add one option at a time.
- Use
manfor deeper details.
10) Searching and filtering text (a core CLI superpower)
grep: search for lines matching a pattern
Search for “error” in a file:
grep "error" app.log
Case-insensitive search:
grep -i "error" app.log
Search recursively in a directory:
grep -R "TODO" projects/
Show line numbers:
grep -n "TODO" projects/notes.txt
find: locate files by name, type, time, size
Find files named notes.txt under current directory:
find . -name "notes.txt"
Find all .txt files:
find . -name "*.txt"
Find directories only:
find . -type d -name "src"
Find files modified in last 1 day:
find . -type f -mtime -1
Combine find with actions (advanced but extremely useful):
Print found files safely (null-separated) and pass to another command:
find . -type f -name "*.log" -print0 | xargs -0 rm -f
If you’re new, prefer -print first to verify matches:
find . -type f -name "*.log" -print
11) Pipes and redirection: connecting commands together
This is one of the most important CLI concepts.
Pipes: |
A pipe sends the output of one command into another command as input.
Example: list files, then filter for .txt:
ls -la | grep "\.txt"
Redirect output: > and >>
Write output to a file (overwrite):
echo "Hello CLI" > message.txt
Append output to a file:
echo "Another line" >> message.txt
Redirect errors separately:
- Standard output (stdout):
1> - Standard error (stderr):
2>
Example: write errors to a file:
ls /does-not-exist 2> errors.txt
Write both stdout and stderr to the same file:
command > all-output.txt 2>&1
Read input from a file: <
sort < unsorted.txt
12) Wildcards (globbing): selecting many files at once
The shell expands patterns before running the command.
*matches any characters?matches a single character[abc]matches one character from the set
Examples:
ls *.txt
Remove all .tmp files in current directory:
rm *.tmp
List files like img1.png, img2.png, etc.:
ls img?.png
Important: Always test patterns with ls before using them with rm:
ls *.tmp
rm *.tmp
13) Quoting and escaping: handling spaces and special characters
If a filename has spaces:
touch "my file.txt"
ls "my file.txt"
Without quotes, the shell treats spaces as separators:
# This is interpreted as two arguments: my and file.txt
ls my file.txt
Escape a single space with backslash:
ls my\ file.txt
Single quotes vs double quotes:
- Single quotes
'...'prevent almost all expansions. - Double quotes
"..."still allow some expansions like$HOME.
Example:
echo "$HOME"
echo '$HOME'
14) Environment variables and your PATH
Environment variables
Variables like HOME, USER, PATH affect how your shell and programs behave.
View a variable:
echo $HOME
List all environment variables:
printenv
Set a variable for the current shell session:
export PROJECTS_DIR="$HOME/projects"
echo $PROJECTS_DIR
PATH
PATH is a list of directories where the shell searches for commands.
See it:
echo $PATH
If you type python, the shell looks through each directory in PATH until it finds an executable named python.
Find where a command comes from:
which ls
which python
15) Processes: running programs and managing them
See running processes
ps
More detailed:
ps aux
Interactive process viewer (if installed):
top
On many systems, htop is nicer (may require installation):
htop
Stop a running command
- Press
Ctrl + Cto interrupt the foreground process.
Background jobs
Run a command in the background by adding &:
sleep 60 &
List jobs:
jobs
Bring a job to the foreground:
fg %1
16) Networking essentials (safe, beginner-level)
Check connectivity: ping
ping -c 4 example.com
-c 4sends 4 packets then stops (Linux/macOS).- On Windows PowerShell,
ping example.comdefaults to 4.
Fetch a URL: curl
Download a webpage:
curl https://example.com
Download to a file:
curl -o example.html https://example.com
Follow redirects:
curl -L -o file.zip https://example.com/file.zip
DNS lookup (varies by system)
On many systems:
nslookup example.com
Or:
dig example.com
17) Archives and compression: tar, zip, gzip
Create a tar archive
tar -cvf backup.tar projects/
-ccreate-vverbose (show files)-ffilename
Extract a tar archive
tar -xvf backup.tar
Create a gzipped tarball (.tar.gz)
tar -czvf backup.tar.gz projects/
Extract:
tar -xzvf backup.tar.gz
Zip (common cross-platform)
Create:
zip -r backup.zip projects/
Extract:
unzip backup.zip
18) Installing software (brief overview)
This varies widely by operating system.
Ubuntu/Debian (APT)
sudo apt update
sudo apt install curl
Fedora (DNF)
sudo dnf install curl
macOS (Homebrew)
Install Homebrew (follow official instructions), then:
brew install wget
Windows
- Use winget:
winget install Git.Git - Or use Chocolatey (third-party) if you prefer.
19) Practical mini-projects (hands-on practice)
These exercises build real skills. Run them in a safe folder like your home directory.
Project A: Create a workspace and organize files
- Create a workspace:
mkdir -p ~/cli-practice/{inbox,archive,notes}
cd ~/cli-practice
- Create sample files:
touch inbox/report-2026-01.txt inbox/report-2026-02.txt inbox/todo.txt
echo "Buy milk" > inbox/todo.txt
- List everything:
find . -maxdepth 2 -type f -print
- Move completed reports to archive:
mv inbox/report-2026-01.txt archive/
mv inbox/report-2026-02.txt archive/
- Confirm:
ls -la inbox archive
Project B: Search notes for keywords
- Create a notes file:
cat > notes/ideas.txt << 'EOF'
CLI practice ideas:
- Learn grep
- Learn find
- Automate backups
TODO: write a small script
EOF
- Search for TODO:
grep -n "TODO" notes/ideas.txt
- Search recursively for “Learn”:
grep -R "Learn" notes/
Project C: Create a compressed backup
From ~/cli-practice:
tar -czvf cli-practice-backup.tar.gz .
List archive contents:
tar -tzf cli-practice-backup.tar.gz | head
20) Common mistakes and how to avoid them
Mistake: Running destructive commands in the wrong directory
Before rm, mv, or cp -r, confirm where you are:
pwd
ls
Mistake: Not understanding wildcard expansion
Always preview:
ls *.log
Then act:
rm *.log
Mistake: Confusing shell features with command features
Wildcards (*) and pipes (|) are handled by the shell, not by the program you run. This matters when debugging: the shell may be changing your input before the command sees it.
Mistake: Copying commands blindly
If you see:
curl ... | sudo bash
be cautious. This downloads a script and runs it as administrator. Prefer reading scripts first:
curl -o install.sh https://example.com/install.sh
less install.sh
sudo bash install.sh
21) A quick Windows PowerShell mapping (optional reference)
If you are using PowerShell, many concepts are similar but commands differ.
-
List directory:
Get-ChildItemAlias:
ls,dir -
Change directory:
Set-Location C:\Users\AlexAlias:
cd -
Print current directory:
Get-Location -
Copy/move/remove:
Copy-Item file.txt copy.txt Move-Item file.txt archive\ Remove-Item file.txt -
View file:
Get-Content file.txt
PowerShell pipelines pass objects rather than plain text, which is powerful but different from Bash-style text pipelines.
22) Where to go next (a learning path)
Once you’re comfortable with the basics above, a solid next step is:
- Text processing:
grep,sed,awk,sort,uniq,cut,tr - Shell scripting: variables,
if, loops, functions - Version control:
git(essential for development and many workflows) - Remote work:
ssh,scp,rsync - System insight:
df,du,free,journalctl(Linux),dmesg
A good habit: each time you learn a command, learn:
- the simplest usage
- one or two common options
- one real workflow where it saves time
Appendix: Command cheat sheet (Linux/macOS)
# Where am I?
pwd
# List files
ls
ls -lah
# Change directory
cd /path/to/dir
cd ..
cd ~
# Create
mkdir -p a/b/c
touch file.txt
# Copy / move
cp src.txt dst.txt
cp -r dir1 dir2
mv oldname newname
# Delete (careful)
rm file.txt
rm -r dir
# View files
cat file.txt
less file.txt
head -n 20 file.txt
tail -n 20 file.txt
tail -f file.txt
# Search
grep -n "pattern" file.txt
find . -name "*.txt"
# Permissions
chmod +x script.sh
chmod 644 file.txt
# Help
man ls
ls --help
# Pipes and redirect
ls | grep "\.txt"
echo "hi" > out.txt
echo "more" >> out.txt
If you want, tell me your operating system (Linux/macOS/Windows + whether you use PowerShell or WSL) and what you want to do with the CLI (programming, data work, system admin, automation). I can tailor a set of practice exercises and a “first 30 commands” list for your exact setup.