← Back to Tutorials

Mastering Command Line Interface Basics for Beginners

command line basicscli for beginnersterminal commandsshell basicslearn command line

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.

Why it matters:


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:

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

Options can be:

Many commands let you combine short options:

ls -la

4) Your filesystem: paths, directories, and navigation

Absolute vs relative paths

Special path symbols (Linux/macOS shells):

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

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:


6) Viewing and editing text in the terminal

View a file quickly:

cat /etc/hosts

For large files, use less (scrollable):

less /var/log/system.log

In less:

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:

(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:

Meanings:

Changing permissions: chmod

Make a script executable:

chmod +x script.sh

Remove execute permission:

chmod -x script.sh

Set exact permissions with numeric mode:

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

9) Getting help: man, --help, and examples

Manual pages: man

man ls

Navigate:

Built-in help

Many commands support:

ls --help

Or for some shells/programs:

command -h

Practical approach to learning a command

  1. Skim --help to see common options.
  2. Try a simple example.
  3. Add one option at a time.
  4. Use man for 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:

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.

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:

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

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

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/

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


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

  1. Create a workspace:
mkdir -p ~/cli-practice/{inbox,archive,notes}
cd ~/cli-practice
  1. Create sample files:
touch inbox/report-2026-01.txt inbox/report-2026-02.txt inbox/todo.txt
echo "Buy milk" > inbox/todo.txt
  1. List everything:
find . -maxdepth 2 -type f -print
  1. Move completed reports to archive:
mv inbox/report-2026-01.txt archive/
mv inbox/report-2026-02.txt archive/
  1. Confirm:
ls -la inbox archive

Project B: Search notes for keywords

  1. Create a notes file:
cat > notes/ideas.txt << 'EOF'
CLI practice ideas:
- Learn grep
- Learn find
- Automate backups
TODO: write a small script
EOF
  1. Search for TODO:
grep -n "TODO" notes/ideas.txt
  1. 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.

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:

  1. Text processing: grep, sed, awk, sort, uniq, cut, tr
  2. Shell scripting: variables, if, loops, functions
  3. Version control: git (essential for development and many workflows)
  4. Remote work: ssh, scp, rsync
  5. System insight: df, du, free, journalctl (Linux), dmesg

A good habit: each time you learn a command, learn:


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.