Beginner · Linux for DevOps · topic 5 of 53 · 30 min
The Linux filesystem and everyday commands
Explanation
Why Linux matters in DevOps
Linux is a free, open-source operating system. Most cloud servers run it, nearly every container image is built on it, and the machines that run CI pipelines (called runners or agents) are usually Linux too. Kubernetes nodes run Linux. If you work in DevOps, you will spend a lot of time typing commands on Linux machines you never see physically.
Distributions
A distribution (distro) is Linux packaged with tools, a package manager and a release schedule. You will meet these most often:
| Distribution | Where you see it | Package manager |
|---|---|---|
| Ubuntu (LTS releases) | Cloud servers, CI runners, laptops | apt |
| Debian | Servers, base of many container images | apt |
| RHEL, Rocky Linux, AlmaLinux | Banks, enterprises, telecom | dnf |
| Alpine | Very small container images (a few megabytes) | apk |
Rocky Linux and AlmaLinux are free rebuilds of Red Hat Enterprise Linux (RHEL). Ubuntu LTS (long-term support) versions come out every two years and get years of security updates, which is why servers use them.
The shell and the terminal
The terminal is the window where you type. The shell is the program inside it that reads your command, runs it and prints the result. The most common shell is bash; zsh is popular on laptops. The prompt, such as deploy@web1:~$, tells you the user (deploy), the machine (web1) and the current folder (~ means your home folder).
One tree, starting at /
Windows has drives like C: and D:. Linux has a single tree that starts at / (called root). Disks and other devices are attached, or mounted, somewhere inside that tree.
| Path | What lives there |
|---|---|
| / | The top of the tree |
| /etc | System and app configuration files, such as /etc/ssh/sshd_config and /etc/nginx/ |
| /var/log | Log files |
| /var/lib | App data, for example /var/lib/docker and /var/lib/postgresql |
| /home | Home folders of normal users, such as /home/asha (root’s home is /root) |
| /usr/bin | Programs (commands) installed by the system |
| /opt | Optional, third-party software, often your own apps |
| /tmp | Temporary files, often cleared on reboot |
| /proc | Not real files: live information from the kernel, such as /proc/cpuinfo and /proc/meminfo |
Absolute and relative paths
An absolute path starts with / and works from anywhere: /var/log/nginx/access.log. A relative path starts from the current folder: if you are in /var/log, then nginx/access.log points to the same file. Two special names help: . means "this folder" and .. means "the parent folder". So from /var/log/nginx, the path ../syslog means /var/log/syslog.
Everyday commands
pwd # print the current folder
ls -la # list all files, including hidden ones, with details
cd /var/log # go to a folder (cd alone goes home, cd - goes back)
mkdir -p /opt/shop/releases # make a folder and any missing parents
cp app.conf app.conf.bak # copy a file
cp -r config/ backup/ # copy a folder and everything inside it
mv old.log archive.log # move or rename
rm notes.txt # delete a file
rm -r old-build/ # delete a folder and its contents
touch deploy.log # create an empty file (or update its time)Reading files
cat /etc/os-release # print a short file (which distro is this?)
less /var/log/syslog # scroll a long file: q quits, /word searches
head -n 20 app.log # first 20 lines
tail -n 50 app.log # last 50 lines
tail -f app.log # follow: keep printing new lines as they arrive
find /var/log -name "*.log" -mtime +7 # .log files older than 7 days
find / -type f -size +500M 2>/dev/null # files bigger than 500 MBEditing files on a server
Servers have no graphical editor, so learn one terminal editor. nano is simple: type, then Ctrl+O to save and Ctrl+X to exit. vim is on almost every machine: press i to insert text, Esc to stop inserting, then type :wq and Enter to save and quit, or :q! to quit without saving.
Installing software
sudo apt update # Ubuntu/Debian: refresh the package list
sudo apt install -y nginx # install nginx without asking yes/no
sudo dnf install -y nginx # RHEL/Rocky/Alma
apk add --no-cache curl # Alpine, often inside a DockerfileNeed help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
The Linux filesystem and everyday commands · 1/8
Linux runs most servers, containers and pipeline runners, so this is where DevOps work happens.
Prefer a video by a teacher? Find free videos on “linux command line basics filesystem hierarchy for devops beginners” on YouTube ↗
In real life: Checking a release on a shop server
A DevOps engineer at an online grocery company in Hyderabad logs in after a release. cat /etc/os-release shows Ubuntu. ls -la /opt/shop/releases shows the new release folder. tail -f /var/log/nginx/error.log shows no new errors while she refreshes the site. find /var/log -name "*.gz" -mtime +30 lists old compressed logs filling the disk, which she reports for a cleanup job instead of deleting by hand.
cat /etc/os-release | head -n 2
ls -la /opt/shop/releases
tail -f /var/log/nginx/error.logPractice (0/3 done)
You are in /var/log/nginx. Which absolute path does ../syslog point to?
Write normalise(cwd, path) that works out the absolute path a shell would use, without touching the disk. cwd is the current folder (always absolute). If path starts with "/", start from the root; otherwise start from cwd. Ignore empty parts and ".", and let ".." remove the last folder (but never go above "/"). Return the result as a string starting with "/", with no trailing slash (except the root itself, "/").
Where would you normally look for the configuration of the SSH server?
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Users, groups and permissionsLinux for DevOps · same module
- Processes, signals and systemd servicesLinux for DevOps · same module
- Bash scripting, text tools and cronLinux for DevOps · same module
- The filesystem hierarchy and pathsLinux and Red Hat (RHCSA to RHCE) · Linux and the shell
- Working with files: ls, cp, mv, rm, links and findLinux and Red Hat (RHCSA to RHCE) · Linux and the shell
- What Linux and Red Hat Enterprise Linux areLinux and Red Hat (RHCSA to RHCE) · Linux and the shell
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which folder holds log files on a typical Linux server?
Answer: /var/log
/var/log holds system and many application logs.
What does mkdir -p /opt/app/releases/v2 do if /opt/app does not exist yet?
Answer: Creates all missing folders in the path
The -p flag creates every missing parent folder and does not complain if they exist.
Which command keeps showing new lines of a log file as they are written?
Answer: tail -f app.log
tail -f follows the file and prints new lines as they arrive.
Which package manager do you use on Rocky Linux?
Answer: dnf
Rocky Linux and other RHEL-family systems use dnf. apt is for Debian and Ubuntu, apk for Alpine.
In vim, how do you save the file and quit?
Answer: Esc, then :wq and Enter
:wq writes and quits. :q! quits without saving. Ctrl+X exits nano, not vim.
You are in /etc. Which absolute path does the relative path ../home/asha mean?Related
Answer: /home/asha
.. takes you from /etc up to /, then into home/asha.
Which command copies the folder /etc/ssh with all owners and permissions kept?Related
Answer: cp -a /etc/ssh /backup
-a (archive) copies recursively and keeps owner, permissions and times.
Which command shows the RHEL release?Related
Answer: cat /etc/redhat-release
/etc/redhat-release holds the release line. uname -r shows the kernel version instead.
vim says "E45: readonly option is set" on /etc/hosts. Most likely cause?Related
Answer: You opened a root-owned file as a normal user without sudo
Open it again with sudo vim /etc/hosts.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 4 more topics in “Linux for DevOps” before its exam: Users, groups and permissions, Processes, signals and systemd services, Bash scripting, text tools and cron, Linux networking for DevOps.
- Module 2 exam: Linux for DevOps · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Git in depth. 53 topics left in the course before the final exam.