Linux Administration
Basic Linux Commands
In this lesson
Linux commands are instructions typed into a shell that tell the operating system to perform specific tasks — navigating directories, creating files, reading content, or managing processes. Unlike graphical interfaces, the command line gives administrators precise, scriptable, and repeatable control over every aspect of a Linux system. Mastery of basic commands is the single most important practical skill in Linux administration.
The Terminal and the Shell
Two terms that beginners often confuse are terminal and shell. The terminal is the window you type into — it is purely a display program. The shell is the program running inside that window that interprets your commands and communicates with the kernel.
Fig 1 — Your typed command travels from terminal to shell to kernel and back
The default shell on most Linux distributions is Bash. The prompt shows your username, hostname, and current directory:
alice@webserver:~$
The $ means regular user; # means root.
Analogy: The terminal is a telephone handset. The shell is the person on the other end who understands your language. The kernel is the organisation that actually carries out the request.
Command Structure
Every Linux command follows the same pattern: command → options → arguments.
Fig 2 — Anatomy of a Linux command
| Command | ls, cd, cp |
The program or built-in to execute. Always comes first. |
| Options / Flags | -l, -a, --all |
Modify behaviour. Short flags use -, long flags use --. Can be combined: -la |
| Arguments | /home/alice, file.txt |
The target the command acts on. Optional for some commands. |
Navigation Commands
Navigation is the foundation of all terminal work. These three commands must become muscle memory.
pwd — Print Working Directory
Outputs the full absolute path of your current directory. Run this first on any unfamiliar server.
pwd/home/alice
What just happened? The shell asked the kernel for the current working directory and printed its absolute path. You are in /home/alice.
cd — Change Directory
Moves you into a different directory. Accepts both absolute and relative paths.
# Move to an absolute path
cd /etc/ssh
# Move up one level
cd ..
# Move to your home directory
cd ~
# Move to the previous directory
cd -ls — List Directory Contents
Lists files and directories. Its flags are among the most commonly used in Linux.
# Basic listing
ls
# Long format — shows permissions, owner, size, date
ls -l
# Show hidden files (names starting with .)
ls -a
# Long format + hidden files combined
ls -la
# Human-readable file sizes (KB, MB, GB)
ls -lh
# List a specific directory without entering it
ls -la /etctotal 72 drwxr-xr-x 2 alice alice 4096 Mar 10 09:14 . drwxr-xr-x 18 root root 4096 Mar 8 11:22 .. -rw------- 1 alice alice 220 Mar 8 11:22 .bash_logout -rw-r--r-- 1 alice alice 3526 Mar 8 11:22 .bashrc -rw-r--r-- 1 alice alice 807 Mar 8 11:22 .profile drwxr-xr-x 2 alice alice 4096 Mar 10 09:14 projects
What just happened? The -la flags combined long format and hidden files. Each line shows permissions, link count, owner, group, size in bytes, modification date, and filename. Files beginning with . are hidden configuration files.
File and Directory Operations
Creating, copying, moving, and removing files and directories are daily tasks. Several of these commands are irreversible.
| Command | Purpose | Example |
|---|---|---|
touch |
Create empty file or update timestamp | touch notes.txt |
mkdir |
Create a new directory | mkdir -p /opt/app/logs |
cp |
Copy file or directory | cp -r /etc/nginx /backup/ |
mv |
Move or rename file/directory | mv old.txt new.txt |
rm |
Remove file or directory | rm -rf /tmp/oldlogs |
rmdir |
Remove an empty directory | rmdir empty_folder |
# Create nested directories in one command
mkdir -p /opt/myapp/config/ssl
# Copy a directory recursively (-r required for directories)
cp -r /etc/nginx /backup/nginx-backup
# Rename a file using mv
mv server.log server.log.bak
# Move a file to a different directory
mv server.log.bak /var/log/archive/Viewing File Content
Reading configuration files, logs, and scripts without opening an editor is a core daily skill.
cat
Prints the entire file at once. Best for short files. Avoid on large files — the output floods the terminal.
less
Opens a file in a scrollable viewer. Use arrow keys to navigate. Press q to quit. The go-to tool for reading large log files.
head
Prints the first 10 lines by default. Use -n to specify a count.
tail
Prints the last 10 lines by default. The -f flag follows the file in real time — essential for watching live log output.
grep
Searches file content and prints matching lines. One of the most powerful and frequently used commands in Linux.
# Print entire file
cat /etc/hostname
# Scroll through a large file
less /var/log/syslog
# First 20 lines of a file
head -n 20 /etc/passwd
# Last 50 lines of a log file
tail -n 50 /var/log/auth.log
# Follow a log file in real time (Ctrl+C to stop)
tail -f /var/log/syslog
# Search for "error" in a log file (case-insensitive)
grep -i "error" /var/log/syslog
# Show line numbers with matches
grep -n "sshd" /var/log/auth.log# tail -f /var/log/syslog output: Mar 10 09:22:01 webserver CRON[1234]: (root) CMD (run-parts /etc/cron.hourly) Mar 10 09:22:14 webserver sshd[1291]: Accepted publickey for alice Mar 10 09:22:14 webserver sshd[1291]: pam_unix(sshd:session): session opened Mar 10 09:23:07 webserver systemd[1]: Started Daily apt upgrade and clean.
What just happened? tail -f opened the syslog and continued streaming new lines as they were written. Press Ctrl+C to stop following.
Essential Utility Commands
A small set of utility commands appears constantly in day-to-day administration work.
Prints text or variable values to the terminal. Fundamental in scripts and for quick output testing.
echo "Hello, $USER"
Prints the current logged-in username. Critical for verifying whether you are root before running privileged commands.
whoami
Opens the manual page for any command. The most complete reference available. Press q to exit.
man ls
Clears the terminal screen. Keyboard shortcut: Ctrl+L.
clear
Lists previously run commands. Use !N to re-run command N, or the up arrow to cycle.
history | tail -20
Shows the full path of a command binary. Confirms which version is executed when multiple are installed.
which python3
rm -rf Has No Undo
Linux has no Recycle Bin. Running rm -rf permanently deletes with no recovery option. Always double-check your path with pwd before running any rm command on a production server.
Lesson Checklist
pwd, cd, and ls with appropriate flags
cat, less, head, tail -f, and grep
Teacher's Note
The fastest way to build command fluency is a live terminal session. Spin up an Ubuntu or Rocky Linux VM and spend 20 minutes navigating directories, creating files, and reading logs. Typing the commands yourself is irreplaceable — reading alone is not sufficient.
Practice Questions
1. You connect to a production server via SSH and need to immediately confirm your current directory, the username you are logged in as, and list all files including hidden ones. Write the three commands in order.
pwd → confirms current directory | whoami → shows logged-in username | ls -la → lists all files including hidden ones with full details.
2. A web application is producing errors. Describe the commands you would use to (a) watch the application log file in real time and (b) search that same log file for lines containing the word "CRITICAL".
tail -f /path/to/app.log — the -f flag streams new lines in real time. Press Ctrl+C to stop. (b) grep "CRITICAL" /path/to/app.log — or add -i for case-insensitive matching.
3. Explain the difference between cp -r and mv when applied to a directory. Give a scenario where you would use each.
cp -r duplicates the directory — the original remains in place. Use it to create a backup before editing. mv relocates the directory — the original is removed from the source. Use it to reorganise the filesystem or rename a directory.
Lesson Quiz
1. A terminal prompt ending with # instead of $ indicates which of the following?
2. Which command is best suited for monitoring a log file as new entries are written in real time?
3. The command mkdir -p /opt/app/config differs from mkdir /opt/app/config in which way?
Up Next
Lesson 5 — Working with Files and Directories
Wildcards, find, redirection, pipes, and advanced file manipulation techniques