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).
pwd
ls
ls -la
cd ~
mkdir -p lab/logs
cd lab
pwdpwd 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.
/home/asha/labShortcuts 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
| Command | What it does | Example |
|---|---|---|
| touch | Create an empty file (or update its timestamp) | touch notes.txt |
| cat | Print a whole file | cat notes.txt |
| less | Page through a long file (q to quit) | less access.log |
| head and tail | First or last lines. tail -f follows a file as it grows | tail -n 20 app.log |
| cp | Copy | cp a.txt b.txt or cp -r src backup |
| mv | Move or rename | mv old.txt new.txt |
| rm | Delete (no recycle bin) | rm a.txt or rm -r folder |
| echo | Print text; with > writes it to a file | echo 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
- 1Create the practice folder and the log file with the commands below.
- 2Count the lines with
wc -lto see how big the log is. - 3Filter with
grepto find errors or one client's requests. - 4Combine commands with pipes to count and rank results.
- 5Save the result to a file with a redirect.
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.logThe <<'EOF' ... EOF block (a here-document) writes everything between the markers into access.log. wc -l counts lines.
6 access.logSearch 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.
grep POST access.log
grep -n ' 500' access.log
grep -v ' 200' access.log
grep -c ' 5[0-9][0-9]$' access.logThe last pattern is a regular expression: a space, 5, any two digits, then end of line. It counts server errors (500 to 599).
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
1Pipes 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?
cut -d' ' -f1 access.log | sort | uniq -c | sort -rncut -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.
3 10.0.0.5
2 10.0.0.7
1 10.0.0.9grep ' 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.
#!/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
fiThe 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.
chmod +x check.sh
./check.sh
echo "exit code: $?"
ls -l check.shServer errors in access.log: 1
exit code: 1
-rwxr-xr-x 1 asha asha 183 Oct 31 10:00 check.shProcesses, the network and installing software
Commands you will meet on servers
| Command | What it does |
|---|---|
ps aux | List running processes (add | grep python to filter them) |
top or htop | Live view of CPU and memory use (q to quit; htop may need installing) |
kill 1234 | Ask process 1234 to stop; kill -9 1234 forces it as a last resort |
df -h | Free disk space in human-readable units |
free -h | Memory use |
curl -i https://example.com | Make an HTTP request and show response headers |
export NAME=value | Set an environment variable for this shell; echo $NAME reads it |
sudo apt update then sudo apt install -y tree | Install 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.
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
- I can move around with pwd, ls and cd and explain absolute vs relative paths
- I can search with grep and count or invert matches
- I can chain commands with | and save output with >
- I know why a script needs chmod +x and what exit code 0 means
- I never run rm -rf or sudo without reading the command first
Key takeaways
- Linux is one tree starting at
/;~is your home folder; Tab and Up-arrow save time. grep,cut,sort,uniqand the pipe turn logs into answers in seconds.>overwrites,>>appends, andrmhas no undo.- Scripts need a shebang line, the execute permission and sensible exit codes;
set -euo pipefailmakes them fail loudly. - Environment variables carry configuration;
manand--helpexplain 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.