Cloud & DevOps
Hands-onBeginner 35 min· Developer foundations · 1 of 16

Linux command line essentials

Learn the everyday Linux commands: navigating, searching logs with grep and pipes, permissions, processes and a first shell script. Every server, container and CI job you meet later runs on this.

What you'll do: learn the 20 or so Linux commands that developers and DevOps engineers use every day, then use them to investigate a small web-server log and write a tiny script. Almost every cloud server, Docker container and CI machine runs Linux, so this is the base for the rest of the track.

Prerequisites: none. You need a Linux shell. On Windows, install WSL (open PowerShell as administrator and run wsl --install, then restart). On macOS, open Terminal (the commands are nearly identical). Or use any free online Linux terminal.

Where you are and what is here

Linux has no drive letters. Everything lives in one tree that starts at / (called root). Your personal folder is your home directory, written ~. A path is the address of a file: /home/asha/notes.txt is an absolute path; notes.txt or ../notes.txt are relative to where you are now (. means here, .. means the parent folder).

Terminal
pwd
ls
ls -la
cd ~
mkdir -p lab/logs
cd lab
pwd

pwd prints the current folder. ls -la lists everything including hidden files (names starting with a dot) with sizes, owners and permissions. mkdir -p creates nested folders and does not complain if they exist.

What the last pwd prints (your username will differ)
/home/asha/lab

Shortcuts worth learning today

Press Tab to auto-complete file names and the Up arrow to bring back earlier commands. Ctrl+C stops a running command. Ctrl+R searches your history. These habits save more time than any single command.

Working with files

Everyday file commands

CommandWhat it doesExample
touchCreate an empty file (or update its timestamp)touch notes.txt
catPrint a whole filecat notes.txt
lessPage through a long file (q to quit)less access.log
head and tailFirst or last lines. tail -f follows a file as it growstail -n 20 app.log
cpCopycp a.txt b.txt or cp -r src backup
mvMove or renamemv old.txt new.txt
rmDelete (no recycle bin)rm a.txt or rm -r folder
echoPrint text; with > writes it to a fileecho hello > a.txt

rm is permanent

rm deletes for good. There is no undo and no trash. Never run rm -rf with a path you have not read twice, and never paste a sudo rm -rf command from the internet without understanding it. Run ls with the same path first to see what you are about to delete.

Practice: investigate a log file

Servers write a line to a log for every request. Reading logs fast is a core DevOps skill. We will create a small log for the Brew & Bean ordering API and answer questions about it using search, pipes and redirects.

The investigation, in order

  1. 1Create the practice folder and the log file with the commands below.
  2. 2Count the lines with wc -l to see how big the log is.
  3. 3Filter with grep to find errors or one client's requests.
  4. 4Combine commands with pipes to count and rank results.
  5. 5Save the result to a file with a redirect.
Terminal
cat > access.log <<'EOF'
10.0.0.5 GET /menu 200
10.0.0.7 POST /orders 201
10.0.0.5 GET /orders/12 200
10.0.0.9 GET /orders/99 404
10.0.0.7 GET /menu 200
10.0.0.5 POST /orders 500
EOF
wc -l access.log

The <<'EOF' ... EOF block (a here-document) writes everything between the markers into access.log. wc -l counts lines.

Output
6 access.log

Search with grep. grep prints lines that match a pattern. Useful flags: -i ignore case, -n show line numbers, -v invert (lines that do NOT match), -c count matching lines, -r search inside all files in a folder.

Terminal
grep POST access.log
grep -n ' 500' access.log
grep -v ' 200' access.log
grep -c ' 5[0-9][0-9]$' access.log

The last pattern is a regular expression: a space, 5, any two digits, then end of line. It counts server errors (500 to 599).

Output of the four commands, in order
10.0.0.7 POST /orders 201
10.0.0.5 POST /orders 500
6:10.0.0.5 POST /orders 500
10.0.0.7 POST /orders 201
10.0.0.9 GET /orders/99 404
10.0.0.5 POST /orders 500
1

Pipes and redirects. The pipe | sends the output of one command into the next, so small tools combine into powerful ones. > writes output to a file (replacing it), >> appends. Here is a classic: which client IP sent the most requests?

Terminal
cut -d' ' -f1 access.log | sort | uniq -c | sort -rn

cut -d' ' -f1 takes the first space-separated column (the IP). sort groups equal lines together, uniq -c counts them, and sort -rn puts the biggest number first.

Output
      3 10.0.0.5
      2 10.0.0.7
      1 10.0.0.9
Terminal
grep ' 5[0-9][0-9]$' access.log > errors.txt
cat errors.txt
find . -name '*.txt'

Saves only the error lines to errors.txt, then uses find to locate files by name (. means start from here).

Permissions and running scripts

Every file has an owner and three sets of permissions (owner, group, others), each made of read, write and execute. ls -l shows them, for example -rwxr-xr--. A script must have the execute bit before you can run it directly. chmod +x file adds it. sudo runs one command as the administrator (root); use it only when you must, for example to install software.

check.sh
#!/usr/bin/env bash
set -euo pipefail

file="${1:-access.log}"
errors=$(grep -c ' 5[0-9][0-9]$' "$file" || true)
echo "Server errors in $file: $errors"
if [ "$errors" -gt 0 ]; then
  exit 1
fi

The first line says which program runs the script. set -euo pipefail makes the script stop at the first error or unset variable instead of carrying on. ${1:-access.log} means the first argument, or access.log if none was given. exit 1 signals failure; exit code 0 means success. CI tools use exit codes to decide whether a step passed.

Terminal
chmod +x check.sh
./check.sh
echo "exit code: $?"
ls -l check.sh
Sizes and dates will differ
Server errors in access.log: 1
exit code: 1
-rwxr-xr-x 1 asha asha 183 Oct 31 10:00 check.sh

Processes, the network and installing software

Commands you will meet on servers

CommandWhat it does
ps auxList running processes (add | grep python to filter them)
top or htopLive view of CPU and memory use (q to quit; htop may need installing)
kill 1234Ask process 1234 to stop; kill -9 1234 forces it as a last resort
df -hFree disk space in human-readable units
free -hMemory use
curl -i https://example.comMake an HTTP request and show response headers
export NAME=valueSet an environment variable for this shell; echo $NAME reads it
sudo apt update then sudo apt install -y treeInstall software on Ubuntu or Debian (WSL Ubuntu uses this too)

Environment variables are how apps get settings like database URLs and secrets without putting them in code. You will use them constantly with Docker and CI. For help on any command, run man grep (press q to leave) or grep --help.

Prompt · Explain a command before I run it
Explain this Linux command piece by piece, in plain English, and tell me if anything in it could delete files, expose secrets or change system settings:

[paste command]

Then suggest a safer way to test it first.

What to expect: Check that every flag is explained and that the answer warns about destructive parts such as rm -rf, chmod -R or > file overwriting. Verify with man before running.

Try it: Find the slowest endpoints

Add a fifth column to each line of a new file timed.log with a response time in milliseconds (for example 10.0.0.5 GET /menu 200 35). Write a one-line pipeline that prints the three lines with the largest times. Then put it in a script slow.sh that takes the file name as an argument.

Self-check

  1. I can move around with pwd, ls and cd and explain absolute vs relative paths
  2. I can search with grep and count or invert matches
  3. I can chain commands with | and save output with >
  4. I know why a script needs chmod +x and what exit code 0 means
  5. I never run rm -rf or sudo without reading the command first

Key takeaways

  1. Linux is one tree starting at /; ~ is your home folder; Tab and Up-arrow save time.
  2. grep, cut, sort, uniq and the pipe turn logs into answers in seconds.
  3. > overwrites, >> appends, and rm has no undo.
  4. Scripts need a shebang line, the execute permission and sensible exit codes; set -euo pipefail makes them fail loudly.
  5. Environment variables carry configuration; man and --help explain any command.

Check yourself

Finishing the quiz marks the lesson as done.

1. What is the difference between > and >> in a shell?

2. You want to count how many requests came from each IP in a log. Which pipeline does it?

3. A script you wrote fails with 'Permission denied' when you run ./deploy.sh. What is the most likely fix?

4. Why do many scripts start with set -euo pipefail?

5. Which is the safest habit before running rm -r backup*?

Saved on this device. Create a free account to keep it on every device.

Practise this with Sam

A real mock interview, out loud, with an honest scorecard. Free to start.

Start free