Skip to content
AkOps Labs
Playgrounds

Linux

Your first terminal. Move around, manage files, find anything.

8 levels · 46 lessons · about 1 hours

Linux commands cheat sheet

Every command in the Linux Playground, grouped by level. Practise each one above in the interactive terminal.

Show all 46 commands

Level 1: First Steps (Beginner)

Find out where you are and move around the file system.

Where am I?
pwd

The terminal always works inside one folder, called the current directory. pwd (print working directory) tells you which one.

List files
ls

ls lists what is inside the current folder.

See hidden files & details
ls -la

Files starting with a dot (like .bashrc) are hidden. -l shows details (permissions, owner, size) and -a shows hidden files.

Change folder
cd projects

cd (change directory) moves you into another folder.

Go back up
cd ..

Two dots (..) mean the parent folder - one level up.

Level 2: Files & Folders (Beginner)

Create, read, copy, move and delete files.

Make a folder
mkdir logs

mkdir (make directory) creates a new folder. Add -p to create nested folders in one go.

Create an empty file
touch app.log

touch creates an empty file, or updates the timestamp if it already exists.

Read a file
cat notes.txt

cat prints a whole file to the screen. Great for short files.

Copy a file
cp notes.txt notes.bak

cp SOURCE DESTINATION copies a file. Use cp -r to copy a whole folder.

Move or rename
mv notes.bak backup.txt

mv moves a file. Moving to a new name in the same folder is how you rename.

Delete a file
rm backup.txt

rm removes a file for good - there is no recycle bin. rm -r removes a folder and everything in it, so double-check first!

Level 3: Search & Read Logs (Intermediate)

Find text in files and watch logs like an on-call engineer.

Search inside a file
grep ERROR app.log

grep prints only the lines that match a word. It's the #1 tool for reading logs.

First lines of a file
head -n 5 app.log

head shows the start of a file (10 lines by default). -n sets how many.

Watch a log live
tail -f app.log

tail shows the end of a file. tail -f keeps it open and prints new lines as they arrive.

Find a file by name
find . -name "*.yaml"

find searches folders for files. find PATH -name PATTERN looks by name.

Chain commands with a pipe
grep ERROR app.log | wc -l

The pipe | sends the output of one command into the next. wc -l counts lines.

Level 4: Permissions & Processes (Intermediate)

Run scripts, check what's running and keep a server healthy.

Make a script executable
chmod +x deploy.sh

Linux files have read (r), write (w) and execute (x) permissions. chmod +x lets you run a script.

Run a script
./deploy.sh

./ means 'in this folder'. You need it to run a script that isn't on your PATH.

See running processes
ps aux

ps aux lists every running process with its PID (process ID), CPU and memory use.

Stop a process
kill 812

kill sends a signal to a process by its PID. Plain kill asks it to stop nicely; kill -9 forces it.

Check disk space
df -h

df -h shows free space on each disk in human-readable units (G, M).

Run as admin
sudo apt install curl

sudo runs one command as the administrator (root). apt is the package manager on Ubuntu/Debian.

Level 5: Run a Real Server (Intermediate)

Connect to servers, check ports and manage services like a sysadmin.

Connect with SSH
ssh ubuntu@10.0.1.25

ssh opens a secure terminal on a remote server. The format is user@address.

Is the website up?
curl -I https://akopslabs.com

curl talks to URLs from the terminal. -I fetches only the response headers, so you quickly see the status code.

What's listening?
ss -tulpn

ss shows network sockets. -tulpn = TCP + UDP, listening only, with process names and port numbers.

Check a service
systemctl status nginx

systemd runs background services. systemctl status shows if a service is running and its latest logs.

Restart a service
sudo systemctl restart nginx

systemctl restart stops and starts a service. Changing services needs sudo.

Read service logs
journalctl -u nginx -f

journalctl reads systemd logs. -u picks one service (unit) and -f follows new lines live.

Level 6: Text Power Tools (Advanced)

Slice, count and edit text files from the terminal, like log files and configs.

Cut out one column
cut -d: -f1 /etc/passwd

cut picks columns from each line. -d sets the separator and -f the field number. /etc/passwd uses : between fields, and field 1 is the username.

Count repeated lines
sort ips.txt | uniq -c

sort puts the same lines next to each other, and uniq -c counts them. Together they answer questions like 'which IP called us most?'.

Print one field with awk
awk '{print $1}' access.log

awk splits each line into fields by spaces. $1 is the first field. In a web access log, the first field is the client IP.

Find and replace in a file
sed -i 's/8080/9090/' app.conf

sed edits text. s/old/new/ replaces text, and -i saves the change into the file itself. Very common in scripts to change a port or a version.

Pack a folder
tar -czf backup.tar.gz app/

tar bundles files into one archive. -c creates, -z compresses with gzip, -f names the file. Backups and deployments use this all the time.

Unpack an archive
tar -xzf backup.tar.gz

-x extracts. Use the same z and f flags. Add -C folder to extract somewhere else.

Level 7: Users, Network & Scheduling (Advanced)

Create service users, check the network and run jobs on a schedule.

Create a user
sudo useradd -m deploy

Apps and CI jobs should run as their own user, not root. useradd -m also creates a home folder.

Add a user to a group
sudo usermod -aG docker deploy

Groups give permissions. Members of the docker group can run Docker without sudo. -aG appends a group without removing the others.

Look up a domain
dig akopslabs.com

dig asks DNS which IP a name points to. When a site 'does not work', DNS is one of the first things to check.

Check a server is reachable
ping -c 3 10.0.1.25

ping sends small test packets. -c 3 stops after three, so it does not run forever.

Copy a file to a server
scp backup.tar.gz ubuntu@10.0.1.25:/tmp/

scp copies files over SSH. The format is scp file user@host:/path.

See scheduled jobs
crontab -l

cron runs commands on a schedule, like a nightly backup. crontab -l lists your jobs, crontab -e edits them. 0 2 * * * means every day at 02:00.

Level 8: Troubleshoot a Live Server (Advanced)

Find out why a server is slow or broken, the way an on-call engineer does.

How busy is the server?
uptime

uptime shows how long the server has run and the load average for the last 1, 5 and 15 minutes. Load higher than the number of CPUs means work is queuing up.

Check memory
free -h

free -h shows used, free and available memory in a readable way. If available is close to 0 and swap is used, the server is out of memory.

Find what fills the disk
sudo du -sh /var/log/*

du -sh shows the size of each item. Logs in /var/log are the most common reason a disk fills up.

Who is using this port?
sudo lsof -i :8080

lsof -i :PORT shows which process has the port open. Use it when an app fails with 'address already in use'.

Read only the errors
journalctl -p err -b

journalctl -p err shows only error messages. -b limits it to the current boot. A fast way to see what broke.

Live view of processes
top

top shows processes live, sorted by CPU. Press M to sort by memory and q to quit. htop is a friendlier version.