Linux commands remain the most direct way to understand, operate and troubleshoot Linux systems. They are best learned as practical task families rather than as isolated syntax to memorize.
Table of Contents
Introduction: Why Linux commands still matter
Linux commands are not just shortcuts for people who do not want to use a graphical interface. They are the natural language of the Linux operating system. When you open a terminal, you are not opening an old or limited tool. You are opening the most direct control layer of the system.
A graphical desktop shows icons, windows and menus. The command line shows the real structure underneath those visual elements. Files become paths. Programs become processes. Background applications become services. Storage becomes mounted filesystems. Internet connections become interfaces, routes, ports and packets. Permissions become visible rules that decide who can read, write or execute something.
This is why the Linux command line still matters everywhere: on personal computers, servers, Raspberry Pi devices, cloud infrastructure, containers, cybersecurity labs, development machines, hosting environments, routers, embedded devices, automation platforms and production systems. A person who understands Linux commands does not only know how to type instructions. They understand how the operating system thinks.
The goal of this guide is to explain Linux commands deeply, practically and clearly. It is not a dry list of commands. A list can help you remember syntax, but it does not teach real competence. Real competence comes from understanding what each command does, what problem it solves, what can go wrong, how it combines with other commands and how it fits into the larger structure of Linux administration.
A beginner often tries to memorize commands one by one. That is understandable, but it is not the best approach. Linux becomes much easier when you understand that commands belong to families of tasks.
Task family
What you are trying to do
Commands you will use often
Orientation
Understand where you are in the filesystem
pwd, cd, ls, tree
File creation
Create folders and files
mkdir, touch, nano
File operations
Copy, move, rename and delete data
cp, mv, rm
File reading
Display and inspect text files
cat, more, less, head, tail
Searching
Find text, files and patterns
grep, find
Compression
Create and extract archives
tar, zip, unzip
Permissions
Control access to files and folders
chmod, chown
Package management
Install, update and remove software
apt, dpkg
System management
Restart, shut down and manage services
reboot, shutdown, service, systemctl
Process control
Inspect and stop running programs
ps, htop, kill, killall
Disk analysis
Check free space and folder size
df, du, mount, umount
Networking
Inspect connections and communicate remotely
ip, ping, wget, ssh, scp, rsync
Automation
Repeat commands and schedule work
history, crontab, screen
Text processing
Transform structured text and logs
awk, sed, cut, wc
Diagnostics
Investigate system activity
lsof, watch, netstat, ss, dmesg
The command line becomes powerful because Linux commands are designed to be combined. One command produces output. Another command filters it. A third command sorts it. A fourth command saves it. A fifth command can run the whole workflow every night.
This is one of the deepest ideas in Linux: a command is useful by itself, but a command pipeline can become a custom tool.
Why Linux commands are still essential
Linux commands remain essential because many important systems do not have a graphical desktop at all. Most production servers are managed through SSH. Cloud instances often start as minimal systems. Containers usually run without a graphical interface. Network devices, Raspberry Pi projects and embedded systems may expose only a shell. Even when dashboards exist, they often show only part of the truth.
The command line is also important because it is precise. When you click a button in a graphical interface, you may not know exactly what happened. When you run a command, the instruction is explicit. You can copy it, document it, repeat it, automate it and audit it.
This makes Linux commands especially valuable in professional environments.
Professional situation
Why commands matter
Server administration
Servers are often managed remotely through terminal sessions
Web hosting
Websites, logs, permissions, services and packages are controlled through shell tools
DevOps
Deployment, automation, containers and monitoring rely heavily on command-line workflows
Cybersecurity
Logs, network connections, permissions and processes must be inspected precisely
Software development
Git, package managers, build tools and deployment scripts are command-line oriented
Data processing
Large text files, logs and exports can be filtered faster from the terminal
Troubleshooting
Real errors are often visible in logs before they appear in dashboards
Automation
Repetitive work can be converted into scripts and scheduled jobs
Recovery
Broken systems may boot into a minimal shell where commands are the only option
A graphical interface is comfortable when everything works. The terminal is essential when something does not.
For example, a web application may show a generic error page. The real reason may be inside a log file. You might need to inspect the last lines of a log, search for a specific error, check whether the web server is running, verify disk space, restart a service and confirm that the port is listening.
That is not one command. It is a workflow.
tail -n 50 /var/log/syslog
grep -i "error" /var/log/syslog
df -h
systemctl status apache2
sudo systemctl restart apache2
sudo ss -tulpn
A person who only knows individual commands may see this as a collection of strange lines. A person who understands Linux sees a logical diagnostic sequence: read recent logs, search errors, check disk space, inspect service state, restart service, confirm network listening ports.
Why command combinations matter more than memorization
A single command answers a simple question. A combination answers a real operational question.
Simple question:
ls
What files are here?
Better question:
ls -lah
What files are here, including hidden files, with permissions and readable sizes?
Operational question:
find . -type f -name "*.log" -exec ls -lh {} \;
Which log files exist under this directory, and how large are they?
More operational:
find . -type f -name "*.log" -size +10M -exec ls -lh {} \;
This progression shows how Linux thinking works. You begin by inspecting. Then you filter. Then you act.
Never skip directly to the action stage when using find, rm, chmod, chown or administrative commands.
Key takeaways
At this point, the foundation is clear. Linux commands operate inside a structured filesystem. Paths can be absolute or relative. Commands have options and arguments. Output can be displayed, filtered, redirected and combined. The safest workflow is to inspect before changing anything.
The core beginner commands are already enough to perform real work:
Command
Essential purpose
pwd
Know your location
cd
Move through directories
ls
List files and metadata
tree
Understand structure visually
mkdir
Create directories
touch
Create empty files
nano
Edit text files
cat
Display short files
more
Read long files page by page
less
Read and search long files comfortably
head
Inspect the beginning of a file
tail
Inspect the end of a file and follow logs
grep
Search text patterns
cp
Copy files and directories
mv
Move and rename files
rm
Remove files and directories
rmdir
Remove empty directories
man
Read command manuals
These commands are not “basic” in the sense of being unimportant. They are basic in the sense that everything else depends on them.
If you understand these commands deeply, then package management, permissions, services, networking, logs, processes, automation and scripting become much easier. Linux is not a collection of disconnected tools. It is a coherent environment built around files, processes, users, permissions and text streams.
How to think at the Linux command line
The command line becomes safer and more useful when it is approached with the right mental model: inspect first, understand the context, make controlled changes and verify the result.
The Linux command line mindset
Before learning syntax, it is important to understand the mindset. Linux does exactly what you tell it to do. It does not always ask whether you are sure. It does not always protect you from your own mistakes. It assumes that the user knows what they are doing, especially when administrative privileges are used.
This is both powerful and dangerous.
A careful Linux user follows a basic principle: inspect first, understand second, change third, delete last.
Stage
Purpose
Typical commands
Inspect
See where you are and what exists
pwd, ls, tree, df, du, ps
Understand
Read files, logs and service status
cat, less, head, tail, grep, systemctl status
Change
Edit, move, install, restart or reconfigure
nano, mv, cp, apt install, systemctl restart
Delete
Remove only after verification
rm, apt remove, find -delete
A beginner often wants to jump directly to action. An expert first gathers context.
For example, before deleting a directory, do not immediately run this:
rm -rf old-project
A safer workflow is:
pwd
ls -lah
du -sh old-project
tree -L 2 old-project
rm -ri old-project
This workflow confirms your location, lists the current directory, checks the size of the target, previews its structure and then removes it interactively.
Linux rewards this kind of discipline.
A professional mental model for Linux commands
Linux command-line work is not about typing fast. It is about thinking accurately.
A professional mental model follows these questions:
Question
Command examples
Where am I?
pwd
What is here?
ls -lah, tree -L 2
How big is it?
du -sh, df -h
What does it contain?
cat, less, head, tail
Does it contain a pattern?
grep
Where is the file?
find
Who owns it?
ls -l, stat
Can I access it?
ls -l, chmod, chown
Is a process running?
ps, htop
Is a service running?
systemctl status
Is a port listening?
ss -tulpn
What changed recently?
ls -lt, find -mtime
What is failing?
tail, grep, journalctl
A beginner memorizes commands. An expert asks better questions.
Practical workflow: build a command-line investigation habit
When something is wrong, do not jump to solutions. Collect facts.
For a service problem:
hostname
uptime
df -h
systemctl status service-name
journalctl -u service-name -n 100
sudo ss -tulpn
For a disk problem:
df -h
sudo du -h --max-depth=1 / | sort -h
sudo find /var/log -type f -size +100M -exec ls -lh {} \;
sudo lsof | grep deleted
For a network problem:
ip a
ip route
ping -c 4 8.8.8.8
getent hosts example.com
sudo ss -tulpn
For a permission problem:
whoami
pwd
ls -ld .
ls -l target
namei -l /full/path/to/target
The command namei -l shows permissions along each component of a path. It is very useful for permission troubleshooting.
Example:
namei -l /var/www/site/uploads/image.jpg
This can reveal that the file itself is readable, but one parent directory blocks access.
Practical workflow: write commands as documentation
A good Linux command is also documentation. It shows what was done.
Compare this vague note:
I cleaned the logs.
with this clear command record:
sudo find /var/log/myapp -type f -name "*.gz" -mtime +30 -delete
The command tells you:
Detail
Information
Location
/var/log/myapp
Type
Regular files only
Pattern
Compressed .gz files
Age
Older than 30 days
Action
Delete
This is why command-line work is reproducible. The exact action can be reviewed and repeated.
For important operations, save the command and output.
sudo find /var/log/myapp -type f -name "*.gz" -mtime +30 -exec ls -lh {} \; | tee cleanup-preview.txt
Then after deletion:
sudo find /var/log/myapp -type f -name "*.gz" -mtime +30 -delete
Save a maintenance log:
{
date
hostname
uptime
df -h
systemctl --failed
} | tee maintenance-check.txt
This creates a simple record of system state.
How to think about advanced shell work
Advanced shell usage is not about writing unreadable one-liners. It is about building reliable, inspectable workflows.
A good shell workflow has these qualities:
Quality
Meaning
Clear
You can understand it later
Safe
It previews or limits destructive actions
Quoted
Variables and paths are protected
Logged
Important output is saved
Repeatable
It can run again with predictable behavior
Testable
It can be run manually before scheduling
Recoverable
It creates backups before risky changes
Minimal
It changes only what must be changed
A bad shell workflow is clever but fragile. A good shell workflow is boring, explicit and safe.
For example, this is risky:
rm -rf $(find . -name "*.tmp")
This is better:
find . -type f -name "*.tmp" -print
Then:
find . -type f -name "*.tmp" -delete
Better still when learning:
find . -type f -name "*.tmp" -exec rm -i {} \;
The safest Linux users are not the slowest. They are the ones who build verification into their workflow.
Terminal, shell, commands and built-in help
Before individual commands make sense, the relationship between the terminal, the shell, command syntax and built-in documentation must be clear.
Understanding the terminal, shell and command
Many people use the words terminal, shell and command as if they were the same thing. They are related, but not identical.
Term
Meaning
Practical explanation
Terminal
The interface window where you type
The visible program that accepts keyboard input
Shell
The command interpreter
The program that reads and executes your commands
Command
The instruction you run
A program, shell built-in or script
Prompt
The text shown before your input
It often shows user, host and current directory
Argument
The target of a command
A filename, directory, user, service or option value
Option
A modifier that changes behavior
Usually begins with - or --
When you type this:
ls -lah /home/pat
you are typing into a terminal. The shell interprets the line. The command is ls. The options are -lah. The argument is /home/pat.
The shell then runs the ls program and displays the result in the terminal.
A typical shell prompt may look like this:
pat@server:~/projects$
This can be understood as:
Prompt part
Meaning
pat
Current user
server
Hostname of the machine
~/projects
Current directory
$
Normal user prompt
If the prompt ends with #, it often means you are acting as the root user.
root@server:/etc#
This deserves attention. A root shell can change or damage the entire system.
The basic structure of a Linux command
Most Linux commands follow a predictable structure.
command options arguments
A simple example:
ls
This runs the command ls without extra options or arguments.
A more specific example:
ls -lah /var/log
This command has three parts.
Part
Value
Meaning
Command
ls
List directory contents
Options
-lah
Long format, show hidden files, human-readable sizes
Argument
/var/log
The directory to list
Options change how a command behaves. Arguments tell the command what to work on.
Short options usually begin with one dash.
ls -l
Long options usually begin with two dashes.
ls --all
Several short options can often be combined.
ls -l -a -h
can usually be written as:
ls -lah
This is why Linux commands may look cryptic at first. They are compact, but the logic is consistent.
Command
Expanded meaning
ls -l
List in long format
ls -a
List all files including hidden files
ls -h
Show human-readable sizes
ls -lah
Combine long format, all files and readable sizes
Not every command supports the same options. You should not assume that -h, -r or -f always means the same thing in every command. Many common patterns exist, but the manual is the final authority.
Getting help from Linux itself
One of the most important commands is man, which opens the manual page for another command.
man ls
Inside a manual page, you can usually use these keys:
Key
Action
Space
Move forward one page
b
Move backward one page
/word
Search for a word
n
Go to next search result
q
Quit
The manual page may seem dense, but it teaches you how Linux documents itself.
Most commands also support quick help.
ls --help
Another useful tool is type, which tells you what kind of command you are running.
type cd
type ls
type grep
Example output may show that cd is a shell built-in while ls is an external program.
cd is a shell builtin
ls is /usr/bin/ls
grep is /usr/bin/grep
This matters because some commands are built directly into the shell, while others are separate executable files.
You can also locate commands with:
command -v ls
which ls
A good learning habit is this:
command --help
man command
type command
command -v command
Replace command with the command you are learning.
type, command, which and whereis
Linux offers several ways to identify commands.
type ls
This tells whether ls is a shell built-in, alias, function or external command.
command -v ls
This prints the path or command resolution.
which ls
This often prints the path of the executable.
whereis ls
This can show binary, source and manual locations.
Comparison:
Command
Best use
type command
Understand how the shell interprets a command
command -v command
Script-friendly command lookup
which command
Quick executable path lookup
whereis command
Locate binary and related manual/source paths
Examples:
type cd
type ls
type grep
command -v bash
whereis bash
You may see that cd is a shell built-in, while grep is an external program.
This distinction matters because built-ins affect the current shell. For example, cd must be a shell built-in because it changes the current directory of the shell itself.
man pages are the built-in university of Linux
The command man opens the manual page for a command.
man find
Manual pages are not always beginner-friendly, but they are precise and complete. A serious Linux user should learn how to read them.
A typical manual page has sections such as:
Section
Purpose
NAME
Short command name and summary
SYNOPSIS
Syntax structure
DESCRIPTION
What the command does
OPTIONS
Available options
EXAMPLES
Sometimes included
FILES
Related files
SEE ALSO
Related commands
AUTHOR
Author information
BUGS
Known issues or reporting information
Inside man, use:
Key
Action
Space
Next page
b
Previous page
/word
Search for word
n
Next search result
N
Previous search result
q
Quit
Example workflow:
man chmod
Search inside manual:
/recursive
Press n to move to the next occurrence.
Read the synopsis carefully. For example, syntax like this:
chmod [OPTION]... MODE[,MODE]... FILE...
has meaning.
Symbol
Meaning
[OPTION]
Optional option
...
Can be repeated
MODE
Permission mode
FILE
Target file or directory
Manual pages become easier once you understand their notation.
apropos searches manual descriptions
If you do not know the command name, use apropos.
apropos password
This searches manual page descriptions for the word password.
Another example:
apropos network
If apropos gives poor results, the manual database may need updating:
sudo mandb
The command apropos is useful when you know what you want to do but not which command does it.
Example:
apropos "disk usage"
You may discover commands such as du or related tools.
help for shell built-ins
Some commands are shell built-ins, not external programs. For Bash built-ins, use help.
help cd
help export
help history
You can also use:
type cd
If it says:
cd is a shell builtin
then help cd is often more relevant than searching for an external executable.
This matters for commands such as:
Built-in
Purpose
cd
Change shell directory
echo
Print text
export
Export variables to environment
history
Show shell history
alias
Define command shortcuts
jobs
Show shell jobs
fg
Bring job to foreground
bg
Resume job in background
The shell itself is a tool, and built-ins are part of that tool.
The Linux filesystem, paths and essential directories
Linux commands operate inside a structured filesystem. Paths, core directories and file-like system interfaces explain where data lives and how Linux exposes system state.
The Linux filesystem as a tree
The most important structural idea in Linux is that the filesystem is a tree. Everything starts at /, called the root directory. Every file, folder, device and mounted disk appears somewhere under this root.
This is different from Windows, where drives are commonly shown as C:, D: and so on. Linux does not organize the system around drive letters. It organizes everything under one unified tree.
The root directory is written as:
/
A full path may look like this:
/home/pat/Documents/report.txt
This path can be read from left to right.
Path part
Meaning
/
Start at the root directory
home
Enter the home area
pat
Enter the user folder named pat
Documents
Enter the Documents directory
report.txt
Open or refer to the file
Linux paths are case-sensitive. This means:
Documents
documents
DOCUMENTS
are three different names.
This is a common source of beginner errors. If a folder is named Downloads, this command will fail:
cd downloads
The correct command is:
cd Downloads
Absolute paths and relative paths
A path tells Linux where something is. There are two main types: absolute paths and relative paths.
Path type
Description
Example
Absolute path
Starts from / and gives the full location
/home/pat/Documents/file.txt
Relative path
Starts from the current directory
Documents/file.txt
An absolute path always begins with /.
cd /home/pat/Documents
This command works no matter where you currently are, because it gives the full address.
A relative path does not begin with /.
cd Documents
This command depends on your current directory. If you are in /home/pat, it means /home/pat/Documents. If you are in /var/log, it means /var/log/Documents.
This is why pwd is so important. Before running commands that change files, always know your current location.
pwd
Example output:
/home/pat
A few special path symbols are used constantly.
Symbol
Meaning
Example
.
Current directory
find . -name "*.txt"
..
Parent directory
cd ..
~
Current user’s home directory
cd ~
/
Root directory
cd /
-
Previous directory with cd
cd -
Examples:
cd .
This technically means stay in the current directory.
cd ..
This moves one level up.
cd ~
This moves to your home directory.
cd /
This moves to the root directory.
cd -
This returns to the previous directory.
Essential Linux directories
Linux directories are not random. They follow a long-established structure. You do not need to memorize every directory immediately, but you should understand the major ones because they appear constantly in commands, logs, configuration and administration.
Directory
Purpose
Practical importance
/
Root of the entire filesystem
Everything begins here
/home
Home directories of normal users
Personal files, documents, scripts and user configuration
/root
Home directory of the root user
Administrator’s private home folder
/etc
System configuration files
Network, services, users, package sources and system settings
/var
Variable data that changes over time
Logs, caches, mail, databases and web data
/var/log
Log files
One of the most important troubleshooting locations
/tmp
Temporary files
Short-lived files created by users and programs
/usr
Installed software and shared system resources
Applications, libraries and documentation
/usr/bin
Many user commands
Common executable programs
/usr/sbin
Many administrative commands
System administration tools
/bin
Essential basic commands
Critical commands needed during boot and recovery
/sbin
Essential system commands
Administrative commands needed during boot and repair
/boot
Bootloader and kernel files
Needed for system startup
/dev
Device files
Disks, partitions, terminals and hardware interfaces
/proc
Virtual process and kernel information
Live system data exposed as files
/sys
Kernel and hardware information
Device and driver information
/media
Mount point for removable media
USB drives and external disks may appear here
/mnt
Temporary manual mount point
Common place for manually mounted filesystems
/opt
Optional third-party software
Some manually installed applications live here
The most important practical distinction is between user space and system space.
Your home directory is usually safe for personal work.
/home/pat
System directories require caution.
/etc
/usr
/var
/boot
/dev
A useful rule is: inside your home directory you are usually managing your own data; outside your home directory you may be changing the operating system.
The root directory and the root user are not the same thing
Beginners often confuse / with the root user.
The root directory is the top of the filesystem.
/
The root user is the administrative superuser.
root
The root user’s home directory is:
/root
These are three related but different concepts.
Term
Meaning
/
Root directory of the filesystem
root
Administrative superuser account
/root
Home directory of the root user
This distinction matters. When someone says “go to root,” they may mean the root directory /. When someone says “run as root,” they mean run with administrative privileges. When someone says “root’s home,” they mean /root.
The deeper structure behind Linux commands
At this point, the command line is no longer just a set of tools. It is a coherent model of the operating system.
A beginner learns commands. An expert sees patterns.
Linux becomes powerful when you understand what is a file and what only looks like a file
One of the most important ideas in Linux is that many things are represented as files or file-like objects. This does not mean everything is a normal document stored on disk. It means the operating system exposes devices, processes, kernel information, terminals, sockets and system state through file paths.
This design gives the command line extraordinary power. You can inspect a process through /proc. You can inspect hardware through /sys. You can see devices in /dev. You can read logs in /var/log. You can configure services through text files under /etc. You can redirect output into files, read from files and connect commands through streams.
A beginner sees files as documents. A Linux administrator sees files as the interface of the operating system.
Location
What it represents
Practical meaning
/etc
Configuration files
System and service behavior
/var/log
Log files
Evidence of events and errors
/dev
Device files
Disks, terminals, random generators, hardware interfaces
/proc
Process and kernel information
Live runtime system state
/sys
Kernel device model
Hardware, drivers and low-level system information
/run
Runtime state
PID files, sockets, temporary service state
/tmp
Temporary files
Short-lived files used by users and programs
/var/tmp
Temporary files that may persist longer
Temporary data not always removed on reboot
The directories /proc and /sys are especially important because they are virtual filesystems. Their contents are not ordinary files stored on disk. They are generated by the kernel to expose live information.
For example:
cat /proc/cpuinfo
shows CPU information.
cat /proc/meminfo
shows memory information.
ls /proc
shows many numbered directories. Those numbers are process IDs.
If a process has PID 1234, then this directory may exist:
/proc/1234
Inside it, Linux exposes information about that running process.
ls -lah /proc/1234
This can show process command line, environment, file descriptors, current working directory and executable path.
This is why Linux troubleshooting often feels transparent. The system exposes itself through inspectable paths.
Understanding /proc through real examples
The /proc filesystem is one of the most useful places for understanding a running Linux system. It contains live information about processes and kernel state.
Show CPU information:
cat /proc/cpuinfo
Show memory information:
cat /proc/meminfo
Show kernel command line used at boot:
cat /proc/cmdline
Show mounted filesystems:
cat /proc/mounts
Show uptime directly from proc:
cat /proc/uptime
Show load average:
cat /proc/loadavg
Many normal commands read from /proc internally or present the same kind of information in a more readable way.
Command
Related proc information
uptime
/proc/uptime, /proc/loadavg
free
/proc/meminfo
ps
/proc/PID/ directories
mount
/proc/mounts
top
Process and memory information from /proc
This means Linux tools often transform raw system information into readable output.
For example, compare:
cat /proc/meminfo
with:
free -h
The first shows detailed raw memory fields. The second summarizes memory in a more human-friendly way.
Both are useful. The command is easier for daily work. The proc file is useful for deeper understanding and scripts.
Inspecting a running process through /proc
Suppose a process has PID 1234.
Show the command line that started it:
tr '\0' ' ' < /proc/1234/cmdline
Show the executable path:
readlink -f /proc/1234/exe
Show the process working directory:
readlink -f /proc/1234/cwd
Show open file descriptors:
ls -lah /proc/1234/fd
Show environment variables:
sudo tr '\0' '\n' < /proc/1234/environ
The null character \0 is used internally as a separator in some proc files, so tr converts it into spaces or new lines for readability.
This process inspection is extremely useful when you need to answer questions such as:
Question
Command
What command started this process?
tr '\0' ' ' < /proc/PID/cmdline
Which binary is running?
readlink -f /proc/PID/exe
What directory is it running from?
readlink -f /proc/PID/cwd
What files does it have open?
ls -lah /proc/PID/fd
What environment does it have?
tr '\0' '\n' < /proc/PID/environ
This is especially helpful when ps aux shows a process but not enough detail.
A process writing to a deleted file may show something like:
1 -> /var/log/myapp/old.log (deleted)
This explains why disk space may remain used even after deleting a large file.
Use lsof for a more readable view:
sudo lsof -p PID
Find deleted open files:
sudo lsof | grep deleted
This is one of the most important advanced disk troubleshooting commands.
Understanding /dev and device files
The /dev directory contains device files. These are special files that represent devices or kernel interfaces.
List some devices:
ls -lah /dev | head
Common examples:
Device
Meaning
/dev/sda
First detected disk in many systems
/dev/sda1
First partition on that disk
/dev/sdb
Another disk
/dev/null
Discards all data written to it
/dev/zero
Produces endless zero bytes
/dev/random
Random data source
/dev/urandom
Non-blocking random data source
/dev/tty
Current terminal
/dev/pts/0
Pseudo-terminal session
The special file /dev/null is used constantly in shell work.
Discard normal output:
command > /dev/null
Discard error output:
command 2> /dev/null
Discard everything:
command > /dev/null 2>&1
This is useful in scripts when you intentionally do not need output. But it can also hide errors, so use it carefully.
Example:
tar -tzf backup.tar.gz > /dev/null 2>&1
This tests whether the archive can be read without printing the file list. The exit status tells whether it succeeded.
echo $?
Understanding /run and runtime state
The /run directory stores runtime data. This data is created after boot and usually does not persist across reboots.
Examples include:
Runtime data
Purpose
PID files
Store process IDs for services
Sockets
Local communication endpoints
Lock files
Indicate that a resource is in use
Service state
Temporary status files
User sessions
Runtime information about logged-in users
List /run:
ls -lah /run
Look for service-related runtime files:
ls -lah /run | head
Some services create sockets under /run.
Example:
find /run -type s 2> /dev/null | head
The -type s option in find searches for sockets.
Runtime files can explain how services communicate locally. For example, databases, PHP-FPM, system services and desktop sessions may use Unix sockets stored under /run.
A socket is not a normal text file. It is a communication endpoint.
Check file type:
ls -l /run/some-socket
You may see the first character s, meaning socket.
Understanding /etc as the configuration center
The /etc directory is where most system-wide configuration lives. It is one of the most important directories on a Linux system.
Examples:
Path
Purpose
/etc/hosts
Local hostname mappings
/etc/hostname
Static hostname on many systems
/etc/fstab
Persistent filesystem mounts
/etc/passwd
User account information
/etc/group
Group information
/etc/shadow
Protected password hash information
/etc/ssh/sshd_config
SSH server configuration
/etc/systemd/system/
Custom systemd unit files
/etc/apt/sources.list
APT repository configuration
/etc/apt/sources.list.d/
Additional APT source files
/etc/nginx/
Nginx configuration
/etc/apache2/
Apache configuration on Debian-based systems
Because /etc controls system behavior, editing it requires discipline.
systemctl status service-name
journalctl -u service-name -n 50
Never treat /etc as an ordinary personal notes folder. It is the control panel of the operating system.
The difference between configuration, state and data
Linux systems contain different kinds of information. Mixing them up causes bad backups, bad migrations and bad repairs.
Type
Meaning
Common location
Configuration
Defines behavior
/etc, application config directories
State
Current runtime condition
/run, service memory, PID files
Data
User or application content
/home, /var/lib, /var/www, databases
Logs
Records of events
/var/log, journal
Cache
Rebuildable temporary data
/var/cache, application cache folders
Binaries
Executable programs
/bin, /usr/bin, /usr/sbin
Libraries
Shared code used by programs
/lib, /usr/lib
This matters for backups.
A website may require:
Component
Example
Code files
/var/www/site
Uploaded files
/var/www/site/uploads
Configuration
/etc/nginx, application .env file
Database
MySQL, PostgreSQL, SQLite or another database
Scheduled jobs
User crontab or systemd timers
Service definitions
/etc/systemd/system
SSL certificates
Often under /etc/letsencrypt or service-specific paths
A backup of /var/www/site alone may not be enough.
A migration plan should identify all required components.
Understanding /var/lib as application state
The directory /var/lib often stores application state and persistent data managed by services.
Examples may include:
Path
Possible purpose
/var/lib/mysql
MySQL or MariaDB data
/var/lib/postgresql
PostgreSQL data
/var/lib/docker
Docker data
/var/lib/apt
APT package state
/var/lib/systemd
systemd state
/var/lib/dpkg
Debian package database
Do not casually delete content from /var/lib. It may contain databases, package metadata or service state.
Check size:
sudo du -h --max-depth=1 /var/lib | sort -h
If /var/lib/docker is huge, the answer is not simply:
sudo rm -rf /var/lib/docker
That can destroy containers, images and volumes.
Instead, use the appropriate application tools.
The same logic applies to databases. Do not delete database files manually unless you understand the database engine and recovery plan.
Understanding /var/cache
The directory /var/cache stores cache data. Cache is usually rebuildable, but it can still matter.
Check size:
sudo du -h --max-depth=1 /var/cache | sort -h
APT package cache:
sudo du -sh /var/cache/apt
Clean APT cache:
sudo apt clean
Application caches may live under /var/cache, but do not delete them blindly. Some services expect cache directories to exist with correct ownership.
A safe cache cleanup process is:
sudo du -h --max-depth=1 /var/cache | sort -h
Then identify the application. Read documentation or service status. Stop the service if needed. Clean only the correct cache. Restart or reload. Verify logs.
Cache is generally less critical than data, but uncontrolled deletion can still cause service disruption.
Understanding /var/log as evidence
The directory /var/log is not just storage. It is evidence.
Before deleting logs, consider whether you need them for troubleshooting, security analysis or compliance.
Check log size:
sudo du -h --max-depth=1 /var/log | sort -h
Find large logs:
sudo find /var/log -type f -size +100M -exec ls -lh {} \;
Search errors:
sudo grep -R -i "error" /var/log 2> /dev/null | head
Check recent logs:
journalctl -p warning -n 100
If logs are huge, ask why.
Symptom
Possible cause
One log file grows rapidly
Application error loop
Many compressed logs exist
Retention too long
Journal uses large space
systemd journal retention not limited
Auth log grows quickly
SSH brute force attempts
Web access log huge
High traffic or bot activity
Error log huge
Misconfiguration or application failure
Deleting logs may free space but hide the cause.
A safer approach is to archive, truncate if necessary and fix the underlying problem.
Working with journal and text logs together
Some systems use both traditional log files and systemd journal logs. You may need both.
The exact logging location depends on the distribution, service and configuration.
A good habit is to check both service logs and application-specific logs.
Understanding system boot at a practical level
Boot is the process that starts the Linux system. You do not need to know every low-level detail at first, but you should understand the practical sequence.
A simplified boot flow:
Stage
Meaning
Firmware
Hardware initializes and loads bootloader
Bootloader
Loads Linux kernel and initial RAM disk
Kernel
Initializes hardware and core system
init system
Starts user space services
systemd target
Brings system to multi-user or graphical state
Services
Network, SSH, web servers, databases and other daemons start
On many modern systems, systemd manages services during boot.
Check boot logs:
journalctl -b
Check previous boot:
journalctl -b -1
List boots:
journalctl --list-boots
Check failed services after boot:
systemctl --failed
Check boot time summary:
systemd-analyze
Show slow services:
systemd-analyze blame
Show critical chain:
systemd-analyze critical-chain
This helps diagnose slow boot or services delaying startup.
Safety principles before changing the system
Administrative power is useful only when it is controlled. The safest Linux work happens when every edit, deletion, restart or installation is preceded by inspection and followed by verification.
Normal user privileges and sudo
Linux is designed around users and permissions. A normal user should be able to manage personal files but should not accidentally damage system files. Administrative changes require elevated privileges.
The most common way to run a command with administrative privileges is sudo.
sudo apt update
This means: run apt update as a superuser, if the current user is allowed to use sudo.
Common tasks that usually require sudo include installing software, editing system configuration, restarting system services, changing ownership of system files and mounting disks.
Task
Needs sudo?
Example
List files in your home directory
Usually no
ls ~/Documents
Create a folder in your home directory
Usually no
mkdir ~/projects
Edit your own text file
Usually no
nano notes.txt
Install software
Yes
sudo apt install htop
Update package lists
Yes
sudo apt update
Edit system configuration
Yes
sudo nano /etc/hosts
Restart a service
Yes
sudo systemctl restart apache2
Change ownership of system files
Yes
sudo chown user:group file
Mount a disk manually
Usually yes
sudo mount /dev/sdb1 /mnt/usb
A dangerous beginner habit is using sudo for everything. That is not necessary and not safe. Use sudo only when the task requires it.
Compare these two commands:
rm notes.txt
This removes a file in the current directory if your user has permission.
sudo rm notes.txt
This removes it with elevated privileges. If you are in the wrong directory, the risk is higher.
The more power a command has, the more carefully it should be checked.
How to read command examples correctly
Many command examples use placeholders. A placeholder is not meant to be typed literally. It represents something you must replace.
Example:
cp
You should not type and exactly. You replace them with real paths.
Correct example:
cp report.txt /home/pat/Documents/
Common placeholders include:
Placeholder
Meaning
Real example
A file name
notes.txt
A directory name
/home/pat/projects
A username
pat
A hostname or IP address
192.168.1.10
A package name
htop
A service name
apache2
A command to run
ls -lah
When you see:
ssh @
you should type something like:
ssh pat@192.168.1.10
Understanding placeholders prevents many beginner mistakes.
Your first safety workflow
Before learning many commands, learn this safety workflow:
pwd
ls -lah
This shows where you are and what is there.
Add one more command when working with directories:
tree -L 2
If tree is not installed, use:
find . -maxdepth 2
A safe inspection sequence is:
pwd
ls -lah
tree -L 2
du -sh .
This gives four layers of understanding.
Command
What it tells you
pwd
Current location
ls -lah
Files, folders, permissions, owners, sizes and hidden entries
tree -L 2
Structure two levels deep
du -sh .
Total size of the current directory
This sequence does not modify anything. It is safe. It is also professional. Many serious mistakes happen because users act before inspecting.
The safest way to learn Linux commands
The safest way to learn Linux commands is to create a practice directory in your home folder and experiment there.
mkdir -p ~/linux-command-lab
cd ~/linux-command-lab
pwd
This may make the web server own everything, including code files. If the application is compromised, an attacker may be able to modify application code more easily.
A better model may be:
Area
Owner
Permission idea
Application code
deploy:www-data
Web server can read, deploy user can write
Uploads
www-data:www-data or controlled group
Web server can write
Cache
www-data:www-data
Web server can write
Configuration secrets
restricted owner and group
Only required users can read
Scripts
deploy or root
Executable only where needed
There is no universal permission model for every application, but the principle is universal: do not give write access where read access is enough.
Dangerous commands and why they are dangerous
Some commands are not bad, but they are dangerous when used without context.
Command pattern
Risk
rm -rf path
Deletes recursively without confirmation
chmod -R 777 path
Gives everyone full access
chown -R user:group path
Changes ownership for many files
find / -exec rm {} \;
Can delete across the system
rsync --delete
Removes destination files not in source
dd if=... of=...
Can overwrite disks
mkfs
Creates filesystem and destroys existing data
kill -9
Forces process termination without cleanup
sudo command
Runs with elevated privileges
> file
Overwrites file content
truncate -s 0 file
Empties file content
Before running dangerous commands, use a safety checklist.
hostname
whoami
pwd
ls -lah
For file deletion:
find /target/path -type f -name "*.tmp" -print
Then:
find /target/path -type f -name "*.tmp" -delete
For rsync --delete:
rsync -av --delete --dry-run source/ destination/
Then:
rsync -av --delete source/ destination/
For recursive ownership changes:
ls -ld /target/path
find /target/path -maxdepth 2 -exec ls -ld {} \; | head
Then apply only if correct.
A dangerous command should never be the first command in a workflow.
Operational discipline: one change at a time
When fixing a Linux problem, make one meaningful change at a time.
systemctl status SERVICE
journalctl -u SERVICE -n 100
sudo ss -tulpn | grep PORT
Permission diagnosis template:
whoami
id
ls -l FILE
namei -l FILE
Backup template:
tar -czf NAME-$(date +%Y-%m-%d-%H-%M).tar.gz SOURCE
tar -tzf NAME-$(date +%Y-%m-%d-%H-%M).tar.gz | head
Templates are safer than rewriting complex commands from memory every time.
Navigation and inspection commands
Navigation and inspection commands answer the first operational questions: where am I, what is here and what does it look like?
pwd: know your current location
The command pwd means print working directory.
pwd
Example output:
/home/pat/projects
This tells you that your terminal is currently working inside /home/pat/projects.
The current directory matters because many commands operate relative to it.
If you type:
rm test.txt
Linux looks for test.txt in the current directory.
If you type:
cp report.txt backup/
Linux looks for report.txt and backup/ relative to where you are now.
This is why pwd should become automatic before file operations.
Use pwd before:
Operation
Why
Removing files
To avoid deleting from the wrong directory
Moving files
To understand source and target
Copying files
To avoid copying wrong data
Running scripts
To know where relative paths resolve
Editing configuration
To avoid editing the wrong file
Creating archives
To avoid archiving the wrong folder
Example:
pwd
ls -lah
rm -i old-notes.txt
This is much safer than running rm blindly.
cd: move through the filesystem
The command cd means change directory.
Go to a specific absolute path:
cd /home/pat/Documents
Go to a directory relative to your current location:
cd Documents
Go to your home directory:
cd
or:
cd ~
Go to the root directory:
cd /
Go one level up:
cd ..
Go two levels up:
cd ../..
Return to the previous directory:
cd -
This command is useful when switching between two directories.
Example:
cd /var/log
pwd
cd /etc
pwd
cd -
pwd
The last cd - returns you to /var/log.
Understanding cd .. is essential. If you are in:
/home/pat/projects/linux/scripts
and run:
cd ..
you move to:
/home/pat/projects/linux
Run it again:
cd ..
Now you are in:
/home/pat/projects
Linux path navigation is logical once you think in levels.
Common cd mistakes
The cd command is simple, but beginners often run into predictable problems.
Mistake
Example
Why it fails
Correct approach
Wrong letter case
cd downloads
The folder is Downloads
cd Downloads
Spaces not quoted
cd My Folder
Shell sees two arguments
cd "My Folder"
Trying to enter a file
cd notes.txt
Files are not directories
Use cat, nano or less
Wrong current directory
cd project
project is not inside current directory
Use absolute path
Missing permissions
cd /root
Normal user cannot enter
Use appropriate privileges only when justified
Handling spaces:
cd "My Folder"
or:
cd My\ Folder
The backslash tells the shell that the space belongs to the folder name.
However, for command-line work, names without spaces are usually easier.
Better:
my-folder
my_folder
project-backup
Less convenient:
My Folder
Project Backup Final
New Documents Copy
Spaces are allowed, but they require more care.
ls: list files and directories
The command ls lists directory contents.
Basic use:
ls
List a specific directory:
ls /home/pat/Documents
Long format:
ls -l
Show hidden files:
ls -a
Human-readable sizes:
ls -h
Common combined form:
ls -lah
This is one of the most useful commands in Linux.
A typical ls -lah output may look like this:
drwxr-xr-x 5 pat pat 4.0K May 14 10:20 .
drwxr-xr-x 25 pat pat 4.0K May 14 09:55 ..
-rw-r--r-- 1 pat pat 1.2K May 14 10:18 notes.txt
-rwxr-xr-x 1 pat pat 842 May 14 10:19 backup.sh
drwxr-xr-x 2 pat pat 4.0K May 14 10:20 logs
This output contains a lot of information.
Column
Example
Meaning
Type and permissions
-rw-r--r--
File type and access rights
Link count
1
Number of hard links
Owner
pat
User owner
Group
pat
Group owner
Size
1.2K
File size
Date and time
May 14 10:18
Last modification time
Name
notes.txt
File or directory name
The first character tells you the type.
First character
Meaning
-
Regular file
d
Directory
l
Symbolic link
b
Block device
c
Character device
s
Socket
p
Named pipe
This means:
-rw-r--r--
is a regular file.
drwxr-xr-x
is a directory.
lrwxrwxrwx
is a symbolic link.
Understanding the first column of ls -l is the beginning of understanding Linux permissions.
Hidden files in Linux
In Linux, hidden files are not hidden through a special property. They are hidden because their names begin with a dot.
Examples:
.bashrc
.profile
.ssh
.config
.cache
The normal ls command does not show them.
ls
To show hidden files, use:
ls -a
To show hidden files with details and readable sizes:
ls -lah
Hidden files often store user configuration.
Hidden file or directory
Common purpose
.bashrc
Bash shell configuration
.profile
Login shell environment settings
.ssh
SSH keys and configuration
.config
User application configuration
.cache
User cache files
.local
User-local applications and data
Do not delete hidden files casually. Some of them are important for login, shell behavior, SSH access or application settings.
Practical ls patterns
The ls command becomes more useful when you learn common patterns.
Command
Purpose
ls
Simple list
ls -l
Detailed list
ls -a
Include hidden files
ls -h
Human-readable sizes
ls -lah
Detailed list with hidden files and readable sizes
ls -lt
Sort by modification time, newest first
ls -ltr
Sort by modification time, oldest first
ls -R
List recursively
ls *.txt
Show only files ending in .txt
ls /var/log
List another directory
ls -ld folder
Show information about the folder itself, not its contents
The command:
ls -ld /var/log
shows information about the directory /var/log itself.
The command:
ls -l /var/log
shows what is inside /var/log.
This difference matters when checking permissions on a directory.
tree: see the structure clearly
The command tree displays a directory structure visually.
This is much easier to understand than a flat list when working with projects.
Show a specific directory:
tree /home/pat/projects
Limit depth:
tree -L 2
Show only directories:
tree -d
Show hidden files too:
tree -a
Show human-readable sizes:
tree -h
Combine options:
tree -a -h -L 2
If tree is not installed:
sudo apt update
sudo apt install tree
A useful alternative using find:
find . -maxdepth 2
This lists files and directories up to two levels below the current directory.
Creating, reading, copying, moving and deleting files
Most real Linux work begins with files. Creating, reading, editing, copying, moving, linking and deleting data should be done with enough context to avoid accidental damage.
mkdir: create directories
The command mkdir creates directories.
mkdir test
This creates a directory named test in the current location.
Create a directory using an absolute path:
mkdir /home/pat/test
Create multiple directories:
mkdir documents scripts backups
Create nested directories:
mkdir -p projects/linux/scripts
The -p option means create parent directories if they do not exist.
Without -p, this can fail:
mkdir projects/linux/scripts
if projects or projects/linux does not already exist.
With -p, Linux creates the full structure.
mkdir -p projects/linux/scripts
This is especially useful in scripts because it avoids errors when parent directories are missing.
Common mkdir examples:
Command
Meaning
mkdir logs
Create logs in current directory
mkdir ~/backups
Create backups in home directory
mkdir -p ~/projects/app/logs
Create nested directory structure
mkdir dir1 dir2 dir3
Create three directories
mkdir -m 755 public
Create directory with specific permissions
The command mkdir -m sets permissions during creation.
mkdir -m 700 private
This creates a directory accessible only to the owner.
touch: create empty files and update timestamps
The command touch creates an empty file if the file does not exist.
touch notes.txt
If the file already exists, touch updates its modification timestamp.
Unlike cp, mv does not require -r for directories.
A practical difference:
Command
What happens
cp file backup/
Original file remains
mv file backup/
Original file is moved
mv old new
File or directory is renamed
Be careful when moving files across filesystems. Moving inside the same filesystem is usually very fast because Linux changes directory references. Moving across disks may require copying data and deleting the original.
rm: remove files and directories
The command rm removes files.
rm file.txt
Remove multiple files:
rm file1.txt file2.txt file3.txt
Ask before removing:
rm -i file.txt
Remove a directory recursively:
rm -r folder
Force deletion:
rm -f file.txt
Recursive forced deletion:
rm -rf folder
This is powerful and dangerous.
Option
Meaning
-r
Recursive, remove directories and their contents
-f
Force, do not ask
-i
Ask before every removal
-I
Ask once before removing many files
A safer beginner command is:
rm -ri folder
This removes recursively but asks for confirmation.
Before deleting a directory, inspect it:
pwd
ls -lah
du -sh folder
tree -L 2 folder
Then remove:
rm -ri folder
Do not run destructive commands copied from the internet unless you understand every part.
This approach is slower but safer when you are uncertain.
For huge numbers of files, moving all into one directory may cause name collisions or performance issues. In that case, use a structured cleanup or direct deletion after proper backups. But the review-directory concept is useful for small and medium cleanup tasks.
Understanding diff and patch
The command diff shows differences between files.
diff -u old.conf new.conf
The -u option produces unified diff format, widely used in development and administration.
Save patch:
diff -u old.conf new.conf > change.patch
Apply patch:
patch old.conf < change.patch
In system administration, you may not use patch every day, but diff -u is extremely useful for reviewing configuration changes.
Searching files, filtering text and processing output
Searching and text processing turn the terminal into an analytical tool. These commands find files, filter logs and extract meaning from structured text.
grep: search text with precision
The command grep searches for text patterns in files or command output.
Basic search:
grep "error" app.log
This displays every line in app.log that contains error.
Case-insensitive search:
grep -i "error" app.log
Show line numbers:
grep -n "error" app.log
Search recursively in a directory:
grep -r "database" /var/www/html
Ignore case recursively and show line numbers:
grep -rin "database" /var/www/html
Invert the search:
grep -v "Notice" app.log
This shows lines that do not contain Notice.
Count matching lines:
grep -c "error" app.log
Show filenames containing matches:
grep -l "error" *.log
Show context around matches:
grep -A 2 -B 4 "Fatal error" app.log
This shows two lines after and four lines before each matching line.
One of the most important differences between a casual Linux user and a confident Linux user is the ability to find things precisely. On a graphical desktop, searching often means typing part of a filename into a search box. In the Linux command line, searching can be much more powerful. You can search by name, extension, type, size, modification time, ownership, permissions and even execute another command on every result.
The main command for this task is find.
find
A simple example:
find /home/pat -iname "report.txt"
This searches inside /home/pat for a file or directory named report.txt, ignoring uppercase and lowercase differences.
The structure is logical.
Part
Meaning
find
Run the search tool
/home/pat
Start searching inside this path
-iname
Match name case-insensitively
"report.txt"
The name pattern to search for
The command find is not only useful when you have lost a file. It is also useful when auditing systems, cleaning old logs, locating large files, finding broken project structures, checking modified files after deployment and preparing backups.
A graphical search tool usually answers the question: “Where is this file?”
The find command can answer deeper questions:
Question
Example command
Where is this file?
find /home -iname "report.txt"
Where are all PHP files?
find /var/www -type f -iname "*.php"
Which files are larger than 100 MB?
find /home -type f -size +100M
Which files changed in the last two days?
find /home -type f -mtime -2
Which empty directories exist?
find . -type d -empty
Which log files can be compressed?
find /var/log -type f -iname "*.log" -size +10M
Which files belong to a specific user?
find /var/www -user www-data
The real value of find is not just that it searches. The real value is that it can search with conditions that matter to administrators.
Searching by filename with find
The most basic use of find is searching by filename.
find /home/pat -name "test.txt"
This searches inside /home/pat for an exact name match.
The -name option is case-sensitive. That means test.txt, Test.txt and TEST.txt are different.
To ignore case, use -iname.
find /home/pat -iname "test.txt"
This can match:
test.txt
Test.txt
TEST.txt
For beginners, -iname is often safer because real files are not always named consistently.
Search for all text files:
find /home/pat -iname "*.txt"
Search for all PHP files:
find /var/www -iname "*.php"
Search for all configuration files:
find /etc -iname "*.conf"
The * symbol is a wildcard. It means any characters.
Pattern
Meaning
"*.txt"
Anything ending in .txt
"log-*"
Anything starting with log-
"*.conf"
Anything ending in .conf
"backup*.tar.gz"
Anything starting with backup and ending in .tar.gz
Always quote patterns like "*.txt" when using find.
This is better:
find . -iname "*.txt"
This can behave unexpectedly:
find . -iname *.txt
The reason is that the shell may expand *.txt before find receives it. Quoting keeps the pattern intact.
Searching by file type
Linux directories can contain many kinds of filesystem objects: regular files, directories, symbolic links, devices, sockets and named pipes. When searching, you often want only one type.
Find only regular files:
find /home/pat -type f
Find only directories:
find /home/pat -type d
Find only symbolic links:
find /home/pat -type l
Combine type and name:
find /var/www -type f -iname "*.php"
This means: find only regular files under /var/www whose names end in .php.
Find directories named cache:
find /var/www -type d -iname "cache"
Find symbolic links:
find /usr/local/bin -type l
The -type filter is important because names alone can be misleading. A directory can be named backup.txt. A file can be named logs. Search by type when the distinction matters.
Type option
Meaning
-type f
Regular file
-type d
Directory
-type l
Symbolic link
-type b
Block device
-type c
Character device
-type s
Socket
-type p
Named pipe
For most everyday work, f, d and l are the most common.
Searching by size
Disk space problems are common on Linux servers. A log grows unexpectedly. A backup script creates repeated archives. A user uploads large files. A database dump remains forgotten in a project directory. The find command helps locate files by size.
Find files larger than 100 MB:
find /home -type f -size +100M
Find files smaller than 1 KB:
find /home -type f -size -1k
Find files exactly 10 MB in size is less common, but possible:
find /home -type f -size 10M
The symbols matter.
Size expression
Meaning
+100M
Larger than 100 MB
-100M
Smaller than 100 MB
100M
Exactly 100 MB unit match
+1G
Larger than 1 GB
+500k
Larger than 500 KB
A practical command for finding large files:
find /home -type f -size +500M -exec ls -lh {} \;
This finds files larger than 500 MB and displays them with readable sizes.
Explanation:
Part
Meaning
find /home
Search inside /home
-type f
Only regular files
-size +500M
Larger than 500 MB
-exec ls -lh {} \;
Run ls -lh on each result
A more advanced version:
find /home -type f -size +500M -printf "%s %p\n" | sort -nr | head -20
This prints file sizes and paths, sorts them by size and shows the largest 20.
The lesson is important: when disk space is low, do not delete randomly. First locate the largest files.
Searching by modification time
Time-based searching is essential for administration. You may need to know which files changed recently, which logs are old, which backups should be removed or which files were modified after a deployment.
Find files modified in the last two days:
find /home -type f -mtime -2
Find files modified more than 30 days ago:
find /var/log -type f -mtime +30
Find files modified exactly around 7 days ago:
find /home -type f -mtime 7
The signs matter.
Expression
Meaning
-mtime -2
Modified less than 2 days ago
-mtime +30
Modified more than 30 days ago
-mtime 7
Modified 7 days ago according to find’s day rounding
For minute-level precision, use -mmin.
Find files modified in the last 60 minutes:
find /home/pat -type f -mmin -60
Find files modified more than 120 minutes ago:
find /tmp -type f -mmin +120
This is very useful when troubleshooting.
Example: after updating a web application, find recently changed files:
find /var/www/html -type f -mmin -30
Example: find old compressed logs:
find /var/log -type f -iname "*.gz" -mtime +30
A safe cleanup workflow:
find /var/log -type f -iname "*.gz" -mtime +30
Review the output first. Then, only when you are sure:
sudo find /var/log -type f -iname "*.gz" -mtime +30 -delete
The order matters. Inspect first. Delete only after verification.
Running commands on find results
The find command becomes extremely powerful when combined with -exec. This allows you to run another command on every result.
Basic structure:
find -exec {} \;
The {} placeholder represents each found item.
Example:
find /var/log -iname "*.log.gz" -exec ls -lh {} \;
This lists each matching compressed log file.
Remove matching files:
find /var/log -iname "*.log.gz" -exec rm {} \;
This is powerful but dangerous.
A safer method is to print first:
find /var/log -iname "*.log.gz"
Then list with details:
find /var/log -iname "*.log.gz" -exec ls -lh {} \;
find ~/pdf-collection -type f -iname "*.pdf" | wc -l
This workflow uses mkdir, find, cp, ls and wc together.
That is real command-line productivity.
Key takeaways
At this point, the command line becomes more than navigation and file reading. You now have the tools to search deeply, archive safely, analyze disk usage, mount filesystems and control permissions.
The most important commands in this section are:
Command
Essential role
find
Search by name, type, size, time and execute actions
tar
Create and extract Linux archives
zip
Create ZIP archives
unzip
Extract ZIP archives
df
Show free and used filesystem space
du
Show file and directory disk usage
mount
Attach a filesystem to the directory tree
umount
Detach a mounted filesystem
chmod
Change permissions
chown
Change ownership
date
Display and format date and time
uptime
Show system runtime and load average
The deeper lesson is this: Linux administration is a sequence of careful questions.
Where is the file? Use find.
How large is it? Use du or ls -lh.
Which filesystem is full? Use df.
Who owns it? Use ls -l.
Who can access it? Read the permissions.
Can it be executed? Check x.
Should it be archived? Use tar.
Should it be deleted? Inspect first, then act.
The more carefully you ask, the safer and more powerful your command line work becomes.
wc: counting lines, words, characters and bytes
The command wc means word count, but it can count more than words.
Basic use:
wc file.txt
Example output:
20 150 900 file.txt
The columns usually mean:
Column
Meaning
First
Lines
Second
Words
Third
Bytes
Last
Filename
Count lines only:
wc -l file.txt
Count words only:
wc -w file.txt
Count characters:
wc -m file.txt
Count bytes:
wc -c file.txt
wc is especially useful after pipes.
Count matching lines:
grep -i "error" app.log | wc -l
Count files found by find:
find /var/www -type f | wc -l
Count logged-in users:
who | wc -l
Count installed packages:
dpkg -l | wc -l
The command wc -l is one of the simplest ways to turn text output into a number.
cut: extracting columns from structured text
The command cut extracts parts of each line. It is useful when text is separated by delimiters such as colons, commas, tabs or spaces.
A classic example uses /etc/passwd.
cut -d : -f 1 /etc/passwd
This prints the first field of each line, using : as the delimiter.
Explanation:
Part
Meaning
cut
Run field extraction
-d :
Use colon as delimiter
-f 1
Print first field
/etc/passwd
Input file
The file /etc/passwd contains user account records. The first field is the username.
Extract usernames and home directories:
cut -d : -f 1,6 /etc/passwd
Extract only characters 1 to 10:
cut -c 1-10 file.txt
Extract the second column from a CSV file:
cut -d ',' -f 2 data.csv
Common options:
Option
Meaning
-d
Delimiter
-f
Field number
-c
Character positions
--complement
Select everything except specified fields
Example:
cut -d ',' -f 1,3 data.csv
This prints fields 1 and 3 from a comma-separated file.
cut is simple and fast, but it is not a complete CSV parser. Real CSV files can contain quoted commas, line breaks inside fields and escaping rules. For simple delimiter-based text, cut is excellent. For complex CSV, use more suitable tools.
awk: a complete text processing language
The command awk is more powerful than cut. It can select fields, apply conditions, calculate values, format output and behave like a small programming language.
These are real server investigation patterns. They show why text processing is central to Linux administration.
awk for calculations
awk can also calculate.
Suppose sizes.txt contains numbers:
10
25
30
Sum them:
awk '{sum += $1} END {print sum}' sizes.txt
Output:
65
Average:
awk '{sum += $1; count++} END {print sum / count}' sizes.txt
Print total size from a command:
du -sk * | awk '{sum += $1} END {print sum " KB"}'
This is where awk becomes more than a column extractor. It becomes a compact data processing tool.
sed: editing text streams
The command sed is a stream editor. It reads text, applies editing rules and prints the result.
Basic substitution:
sed 's/old/new/' file.txt
This replaces the first occurrence of old with new on each line in the output. It does not modify the file unless you use in-place editing.
Replace all occurrences on each line:
sed 's/old/new/g' file.txt
The g means global within each line.
Delete lines beginning with #:
sed '/^#/d' config.conf
This removes comment lines from the output.
Delete empty lines:
sed '/^$/d' file.txt
Print only a range of lines:
sed -n '10,20p' file.txt
This prints lines 10 through 20.
In-place editing:
sed -i 's/old/new/g' file.txt
In-place editing with backup:
sed -i.backup 's/old/new/g' file.txt
This creates file.txt.backup.
The backup form is much safer when learning.
sed examples for configuration cleanup
Show a configuration file without comments and empty lines:
sed '/^#/d; /^$/d' config.conf
This applies two commands:
sed rule
Meaning
/^#/d
Delete lines beginning with #
/^$/d
Delete empty lines
This is useful when reading long configuration files.
Example:
sed '/^#/d; /^$/d' /etc/ssh/sshd_config
Be careful: this prints a cleaned view. It does not show inline comments and it does not explain inherited defaults. Still, it can make large configuration files easier to inspect.
Replace a domain in a file:
sed -i.backup 's/old.example.com/new.example.com/g' config.conf
Then compare:
diff config.conf.backup config.conf
If wrong, restore:
mv config.conf.backup config.conf
A professional habit with sed -i is to make a backup first or use the built-in backup extension.
sort and uniq: organizing and counting text
The command sort sorts lines.
sort file.txt
Reverse sort:
sort -r file.txt
Numeric sort:
sort -n numbers.txt
Human-readable size sort:
sort -h sizes.txt
Sort by a specific column:
sort -k 2 file.txt
The command uniq removes adjacent duplicate lines.
uniq file.txt
Important detail: uniq only removes duplicates that are next to each other. This is why it is often used after sort.
This saves found files into found.txt and permission errors into denied.txt.
Example saving both output and errors:
find /etc -name "*.conf" &> result.txt
Redirection is essential for logging, scripting and automation.
The shell is more than a place where commands are typed
The Linux shell is not only a command launcher. It is an environment that can remember commands, expand patterns, redirect output, connect programs, store variables, repeat work, run scripts and automate system behavior.
This is where Linux begins to feel different from a normal graphical operating system. In a graphical interface, you usually perform one action at a time. In the shell, you can describe a whole workflow as text. That workflow can be repeated, saved, scheduled, shared, inspected and improved.
A command such as this is useful:
grep -i "error" app.log
But the shell becomes much more powerful when commands are combined:
This sorts processes by CPU usage and shows the top 10.
The pipe is central to Linux because it lets each command remain simple while allowing the workflow to be complex.
Redirection controls where output goes
Most commands have output. By default, normal output appears on the terminal. Errors also appear on the terminal, but technically they use a different stream.
This stores successful results in found.txt and permission errors in denied.txt.
Append errors to a log file:
command 2>> errors.log
Discard output:
command > /dev/null
Discard errors:
command 2> /dev/null
Discard everything:
command > /dev/null 2>&1
The special file /dev/null is a data sink. Anything written to it disappears.
Use /dev/null carefully. It is useful in scripts, but hiding errors can make troubleshooting harder.
Redirection and sudo can be confusing
A common beginner problem appears when writing to protected files.
This often fails:
sudo echo "test" > /etc/example.conf
Why? Because sudo applies to echo, but the redirection > is handled by your current shell before sudo writes the file. The shell itself may not have permission to write into /etc.
Use tee instead:
echo "test" | sudo tee /etc/example.conf
Append with tee -a:
echo "another line" | sudo tee -a /etc/example.conf
Hide tee output if needed:
echo "test" | sudo tee /etc/example.conf > /dev/null
This pattern is important for writing into protected files from command pipelines.
Examples:
echo "127.0.0.1 localtest" | sudo tee -a /etc/hosts
printf "Hello\nWorld\n" | sudo tee /opt/example.txt
The core lesson: sudo does not automatically apply to shell redirection.
xargs converts input into command arguments
The command xargs takes input lines and turns them into arguments for another command.
A simple example:
find . -name "*.tmp" | xargs rm
This finds .tmp files and passes them to rm.
However, this can break when filenames contain spaces or unusual characters. A safer version uses null-separated output.
find . -name "*.tmp" -print0 | xargs -0 rm
Explanation:
Part
Meaning
find . -name "*.tmp"
Find matching files
-print0
Separate results with null characters
xargs -0
Read null-separated input safely
rm
Remove files
Preview before deleting:
find . -name "*.tmp" -print0 | xargs -0 ls -lh
Then remove:
find . -name "*.tmp" -print0 | xargs -0 rm
Another example: count lines in all .log files.
find . -name "*.log" -print0 | xargs -0 wc -l
The command xargs is useful, but it should be handled carefully. For many tasks, find -exec is easier to read.
Compare:
find . -name "*.tmp" -exec rm {} \;
and:
find . -name "*.tmp" -print0 | xargs -0 rm
Both can work. xargs may be more efficient for large numbers of files because it can pass many files to one command invocation.
tee: display output and save it at the same time
The command tee reads input and writes it both to the screen and to a file.
command | tee output.txt
Example:
ls -lah | tee listing.txt
This displays the listing and saves it to listing.txt.
Append instead of overwrite:
command | tee -a output.txt
Use with protected files:
echo "example" | sudo tee /etc/example.conf
Use in logs:
sudo apt update | tee apt-update.log
Save both output and errors:
command 2>&1 | tee command.log
Example:
sudo apt upgrade 2>&1 | tee upgrade.log
This is useful during maintenance because you can watch output and keep a record.
Understanding standard input in practical commands
Standard input is the data a command reads. Many commands can read from files or from standard input.
Example:
cat file.txt
The file is an argument.
But this uses standard input:
cat < file.txt
Pipes provide standard input:
cat file.txt | grep error
Here, grep reads from standard input.
Equivalent:
grep error file.txt
Some commands are commonly used with standard input.
sort < names.txt
or:
cat names.txt | sort
A here-document sends standard input:
cat << 'EOF'
Line one
Line two
EOF
Understanding input makes pipes and scripts easier to reason about.
Understanding shell globbing versus find
Globbing is expansion performed by the shell.
ls *.log
This matches files in the current directory.
find searches recursively and with conditions.
find . -type f -name "*.log"
Comparison:
Feature
Shell glob
find
Current directory matching
Excellent
Yes
Recursive search
Limited unless using shell options
Excellent
Search by size
No
Yes
Search by time
No
Yes
Search by owner
No
Yes
Execute action on results
Limited
Excellent
Handles huge trees
Shell may hit argument limits
Better
Use globbing for simple current-directory tasks.
ls *.txt
rm -i *.tmp
Use find for deeper controlled searches.
find /var/log -type f -name "*.gz" -mtime +30 -exec ls -lh {} \;
Argument list too long
Sometimes shell expansion creates too many arguments.
Example:
rm *.log
If there are too many .log files, you may see:
Argument list too long
This happens because the shell expands *.log into a huge list before running rm.
This is another reason find is essential for serious file operations.
Archives, compression and backup basics
Archives, compression and backups connect file operations with operational safety. A useful archive is not only created, but also inspected, moved, verified and restorable.
Archiving and compression in Linux
Linux systems often use archives for backups, transfers, deployments and log storage. The most important command is tar.
The name originally meant tape archive, but today it is used for packaging files and directories into archive files.
There is an important distinction:
Concept
Meaning
Archiving
Combining multiple files into one file
Compression
Reducing the size of data
Archive file
One file containing many files
Compressed archive
An archive that is also compressed
The command tar can create an archive and optionally compress it.
Common archive extensions:
Extension
Meaning
.tar
Tar archive, usually not compressed
.tar.gz
Tar archive compressed with gzip
.tgz
Short form of .tar.gz
.tar.bz2
Tar archive compressed with bzip2
.tar.xz
Tar archive compressed with xz
.zip
ZIP archive, common on Windows
A .tar.gz file is extremely common in Linux.
Creating tar archives
Create a gzip-compressed tar archive:
tar -czvf archive.tar.gz /home/pat/project
This command creates archive.tar.gz from /home/pat/project.
The options can be understood like this:
Option
Meaning
-c
Create an archive
-z
Compress with gzip
-v
Verbose output
-f
Use the following filename
The -f option must be followed by the archive filename.
This is correct:
tar -czvf project-backup.tar.gz project/
This is wrong or confusing:
tar -czv project-backup.tar.gz project/
Without -f, tar does not know that project-backup.tar.gz is the archive file name in the intended way.
A quieter version without verbose output:
tar -czf project-backup.tar.gz project/
For scripts, less verbose output is often preferred.
Create an uncompressed tar archive:
tar -cf archive.tar project/
Create a bzip2-compressed archive:
tar -cjvf archive.tar.bz2 project/
Create an xz-compressed archive:
tar -cJvf archive.tar.xz project/
Compression
Option
Common extension
General note
gzip
-z
.tar.gz
Fast and widely used
bzip2
-j
.tar.bz2
Often smaller than gzip, slower
xz
-J
.tar.xz
Strong compression, often slower
none
no compression option
.tar
Archive only
For most practical use, .tar.gz is a safe default.
Extracting tar archives
Extract a .tar.gz archive:
tar -xzvf archive.tar.gz
Options:
Option
Meaning
-x
Extract
-z
Use gzip
-v
Verbose output
-f
Archive filename follows
A quieter version:
tar -xzf archive.tar.gz
Extract into a specific directory:
tar -xzf archive.tar.gz -C /home/pat/extracted
The destination directory must already exist.
mkdir -p /home/pat/extracted
tar -xzf archive.tar.gz -C /home/pat/extracted
List archive contents without extracting:
tar -tzf archive.tar.gz
This is a very important safety command.
Before extracting an archive from an unknown source, inspect its structure:
Use cron for simple user tasks. Use systemd timers when you want service-style visibility, missed-run handling and better integration.
Permissions, ownership, users and groups
Permissions and ownership explain many Linux problems that look mysterious at first. Users, groups, modes, ACLs and sudo policy determine who can access what and under which conditions.
Understanding Linux permissions
Linux permissions control who can read, write and execute files. This is one of the most important security concepts in the system.
Run:
ls -l
Example:
-rw-r--r-- 1 pat pat 1200 May 14 10:18 notes.txt
-rwxr-xr-x 1 pat pat 842 May 14 10:19 backup.sh
drwxr-xr-x 2 pat pat 4096 May 14 10:20 scripts
The first column contains type and permissions.
Take this example:
-rwxr-xr--
Break it down:
Part
Meaning
-
Regular file
rwx
Owner permissions
r-x
Group permissions
r--
Others permissions
The permission letters mean:
Letter
Meaning for files
Meaning for directories
r
Read file content
List directory contents
w
Modify file content
Create, delete or rename entries inside directory
x
Execute file as a program
Enter or traverse directory
The meaning of execute permission is different for files and directories.
For a file, execute means you can run it as a program or script.
For a directory, execute means you can enter it and access items inside it if you know their names.
This is why directory permissions can be confusing. A directory needs execute permission to be usable.
Permission groups: owner, group and others
Every file has three permission categories.
Category
Meaning
Owner
The user who owns the file
Group
The group assigned to the file
Others
Everyone else
Example:
-rw-r--r-- 1 pat editors 1200 May 14 10:18 article.txt
This means:
Field
Value
Owner
pat
Group
editors
Owner permissions
rw-
Group permissions
r--
Others permissions
r--
So pat can read and write. Members of editors can read. Everyone else can read.
A more restrictive example:
-rw------- 1 pat pat 1200 May 14 10:18 private.txt
Only pat can read and write this file.
A script example:
-rwxr-xr-x 1 pat pat 842 May 14 10:19 backup.sh
The owner can read, write and execute. Group and others can read and execute.
Symbolic permissions are readable and good for learning.
chmod with numeric permissions
Numeric permissions are compact and common in administration.
Each permission has a number:
Permission
Number
Read
4
Write
2
Execute
1
Add the numbers together.
Permission set
Calculation
Value
r--
4
4
rw-
4 + 2
6
r-x
4 + 1
5
rwx
4 + 2 + 1
7
---
0
0
A numeric permission has three digits.
Digit position
Applies to
First digit
Owner
Second digit
Group
Third digit
Others
Examples:
Permission
Meaning
644
Owner read/write, group read, others read
600
Owner read/write, nobody else has access
755
Owner full access, group and others read/execute
700
Owner full access, nobody else has access
775
Owner and group full access, others read/execute
777
Everyone can read, write and execute
Set file to 644:
chmod 644 file.txt
Set script to 755:
chmod 755 script.sh
Set private key to 600:
chmod 600 ~/.ssh/id_rsa
Set private directory to 700:
chmod 700 ~/.ssh
Important warning: do not use chmod 777 as a universal fix. It gives everyone full access and can create serious security problems.
chmod on directories
Directory permissions deserve special attention.
For directories:
Permission
Meaning
Read
Can list directory contents
Write
Can create, delete or rename entries
Execute
Can enter or traverse the directory
A common directory permission is 755.
chmod 755 public-directory
This means:
Category
Permission
Meaning
Owner
rwx
Can enter, list and modify
Group
r-x
Can enter and list
Others
r-x
Can enter and list
A private directory often uses 700.
chmod 700 private-directory
This means only the owner can access it.
For web directories, permissions must be chosen carefully. Giving write access to the wrong user can create security risks. Giving too little access can break uploads, cache generation or application updates.
A common safe starting pattern for many web projects is:
find /var/www/site -type d -exec chmod 755 {} \;
find /var/www/site -type f -exec chmod 644 {} \;
This sets directories to 755 and files to 644.
Do not apply this blindly to every application. Some applications need writable cache or upload directories.
chown: change owner and group
The command chown changes file owner and group.
Change owner:
sudo chown pat file.txt
Change owner and group:
sudo chown pat:www-data file.txt
Change ownership recursively:
sudo chown -R pat:www-data /var/www/site
The -R option means recursive. It changes ownership for the directory and everything inside it.
This is powerful and must be used carefully.
Common ownership examples:
Command
Meaning
sudo chown pat file.txt
Make pat the owner
sudo chown pat:editors file.txt
Owner pat, group editors
sudo chown :www-data file.txt
Change group only
sudo chown -R pat:pat folder
Recursively change owner and group
Check ownership:
ls -l file.txt
Check directory ownership:
ls -ld folder
The difference matters:
ls -l folder
shows the contents of the folder.
ls -ld folder
shows the folder itself.
Permission and ownership troubleshooting
Permission problems are common. The error messages usually look like this:
Permission denied
or:
Operation not permitted
A good troubleshooting workflow:
whoami
pwd
ls -ld .
ls -l target-file
This tells you who you are, where you are, the permissions of the current directory and the permissions of the target file.
If a script will not run:
ls -l script.sh
chmod +x script.sh
./script.sh
If a directory cannot be entered:
ls -ld directory
Check whether execute permission exists.
If a web application cannot write to a cache directory:
ls -ld cache
ls -l cache
You may need to adjust owner, group or permissions depending on which user the web server runs as.
For example:
sudo chown -R www-data:www-data cache
or:
sudo chmod -R 775 cache
But do not guess. First identify the correct service user.
ps aux | grep apache
ps aux | grep nginx
Permission fixes should be precise, not random.
The danger of recursive chmod and chown
Recursive permission changes can affect thousands of files.
This command:
sudo chmod -R 777 /var/www/site
may appear to “fix” a permission problem, but it creates a major security problem.
This command:
sudo chown -R pat:pat /
would be catastrophic because it would attempt to change ownership across the entire filesystem.
Before recursive changes, always verify the target.
pwd
ls -ld /var/www/site
Then use precise commands.
A safer web project example:
sudo find /var/www/site -type d -exec chmod 755 {} \;
sudo find /var/www/site -type f -exec chmod 644 {} \;
If that fails, the web server may also fail to write cache files.
Use sudo -u as a diagnostic tool, not only as an administration shortcut.
Understanding users and groups in file access
Linux file access is decided by user, group and others permissions.
Example:
-rw-r----- 1 root www-data 1200 May 14 10:20 config.php
This means:
Category
Permission
Owner root
Read and write
Group www-data
Read
Others
No access
If the web server runs as www-data, it can read the file. Other users cannot.
Now consider:
drwxr-x--- 2 root www-data 4096 May 14 10:20 private
This directory allows:
Category
Access
Owner root
Enter, list, modify
Group www-data
Enter and list
Others
No access
If a user is not root and not in www-data, they cannot enter.
To troubleshoot path access, use:
namei -l /var/www/site/private/config.php
This shows permissions for every path component.
Example output may show:
drwxr-xr-x root root /
drwxr-xr-x root root var
drwxr-xr-x root root www
drwxr-x--- root www-data site
-rw-r----- root www-data config.php
This helps reveal where access is blocked.
A file can have correct permissions, but if a parent directory blocks execute access, the file is still inaccessible.
passwd and changing passwords
The command passwd changes a user password.
Change your own password:
passwd
Change another user’s password as administrator:
sudo passwd username
Lock a password for an account:
sudo passwd -l username
Unlock:
sudo passwd -u username
On servers using SSH keys, password login may be disabled, but account passwords can still matter for local login, sudo policies or recovery workflows depending on configuration.
Password changes should be handled carefully, especially on production systems. Always know whether the account is used by humans, services or automation.
adduser, usermod and group membership
Create a user on Debian-based systems:
sudo adduser newuser
Add a user to a group:
sudo usermod -aG groupname username
Example: add user to sudo group:
sudo usermod -aG sudo pat
Example: add user to www-data group:
sudo usermod -aG www-data pat
The -aG combination is important.
Option
Meaning
-a
Append to supplementary groups
-G
Specify supplementary groups
Without -a, you may replace the user’s supplementary groups instead of adding to them. That can remove important access.
After group changes, the user usually needs to log out and log back in for the new group membership to appear.
Check:
id pat
or:
groups pat
Practical workflow: investigate permission denied
Suppose a user cannot read:
/var/www/site/uploads/image.jpg
Start with identity:
whoami
id
Check the file:
ls -l /var/www/site/uploads/image.jpg
Check the parent directory:
ls -ld /var/www/site/uploads
Check the whole path:
namei -l /var/www/site/uploads/image.jpg
Test as the service user if relevant:
sudo -u www-data ls -l /var/www/site/uploads/image.jpg
If the issue is missing directory execute permission, changing only the file will not help. One of the parent directories must allow traversal.
If it fails, the application will likely fail too.
This is the connection between processes and permissions.
Advanced permissions explain many problems that basic chmod does not solve
Basic Linux permissions are built around three actions and three identity classes.
The actions are read, write and execute. The identity classes are owner, group and others. This model explains many everyday problems, but real Linux systems often need more detail. Web servers, shared directories, deployment users, backup users, scripts, SSH keys and service accounts all depend on permissions behaving predictably.
A beginner may know that chmod 755 gives executable access and chmod 644 is common for files. A professional user asks deeper questions.
Who owns the file?
Which group owns it?
Which user runs the process that needs access?
Does every parent directory allow traversal?
Is the execute bit missing on a directory?
Is the file controlled by ACL rules?
Is a special permission bit active?
Is the default creation mask causing new files to have unexpected permissions?
Is a service writing files as a different user than expected?
Linux permission problems often look simple from the outside, but the cause may be one level deeper than the file itself.
Review of standard permission logic
Run:
ls -l file.txt
Example output:
-rw-r----- 1 pat www-data 1200 May 14 10:20 file.txt
This line tells you the file type, permissions, owner, group, size, time and name.
The permission part is:
-rw-r-----
Break it down:
Part
Meaning
-
Regular file
rw-
Owner can read and write
r--
Group can read
---
Others have no access
The owner is:
pat
The group is:
www-data
This means user pat can read and write. Members of group www-data can read. Everyone else has no access.
For directories, execute permission has a special meaning. It means the user can enter or traverse the directory.
Example:
ls -ld /var/www/site
Output:
drwxr-x--- 5 deploy www-data 4096 May 14 10:20 /var/www/site
This means:
Category
Permission
Practical effect
Owner deploy
rwx
Can enter, list and modify
Group www-data
r-x
Can enter and list
Others
---
Cannot access
If a web server runs as www-data, it can read files inside this directory only if the files and all parent directories allow it.
This is why the command below is extremely useful:
namei -l /var/www/site/index.html
It shows permissions for each path component, not only the final file.
umask controls default permissions for new files
When a user or process creates a new file, Linux does not usually create it with full open permissions. The final permissions are influenced by a value called umask.
Show current umask:
umask
Example output:
0022
The umask removes permissions from the default creation mode.
Typical defaults are:
Object type
Base permission before umask
New file
666
New directory
777
If the umask is 022, then:
Object
Base
Minus umask
Result
File
666
022
644
Directory
777
022
755
That is why new files are often created as 644 and new directories as 755.
If the umask is 002, then:
Object
Base
Minus umask
Result
File
666
002
664
Directory
777
002
775
This is common in collaborative group environments where group members should be able to write.
Set umask temporarily:
umask 002
Create a file and directory:
touch test-file
mkdir test-directory
ls -l
ls -ld test-directory
The permissions will reflect the active umask.
Why umask matters:
Situation
Why umask matters
Deployment user creates files
New files may not be group-writable
Web app creates cache files
Runtime permissions may differ from expected
Shared folders
Group collaboration may fail
Backup scripts
Restored or generated files may have different access
Cron jobs
Non-interactive environment may use different umask
This ensures new files created by the script follow the intended permission pattern.
setgid directories keep group ownership consistent
A common problem in shared directories is inconsistent group ownership.
Suppose a project directory is owned by group www-data.
ls -ld /var/www/site
Output:
drwxrwxr-x 5 deploy www-data 4096 May 14 10:20 /var/www/site
If user deploy creates a new file, the file may be owned by the user’s primary group, not necessarily www-data.
The setgid bit on a directory helps solve this. When setgid is active on a directory, new files and subdirectories created inside it inherit the directory’s group.
Set setgid on a directory:
sudo chmod g+s /var/www/site
Check:
ls -ld /var/www/site
Output may show:
drwxrwsr-x 5 deploy www-data 4096 May 14 10:20 /var/www/site
The s in the group execute position indicates setgid.
Apply to directories recursively:
sudo find /var/www/site -type d -exec chmod g+s {} \;
A practical collaborative web directory setup may look like this:
Owner and group can write, others can read and enter
2775
Same as 775, plus setgid on directory
Use this carefully. It is useful for shared project directories, but it should not be applied blindly across the system.
Sticky bit protects shared directories
The sticky bit is commonly used on shared writable directories such as /tmp.
Check /tmp:
ls -ld /tmp
Typical output:
drwxrwxrwt 10 root root 4096 May 14 10:20 /tmp
The t at the end means sticky bit.
A directory with sticky bit allows multiple users to create files, but users cannot delete files owned by other users unless they have appropriate privileges.
This is important for shared temporary directories.
Set sticky bit:
sudo chmod +t shared-directory
Numeric form:
sudo chmod 1777 shared-directory
A permission of 1777 means:
Part
Meaning
1
Sticky bit
777
Everyone can read, write and enter
This is appropriate for directories like /tmp, not for ordinary web application directories.
Do not use 1777 as a lazy fix for permission problems. It is for specific shared temporary use cases.
setuid is powerful and risky
The setuid bit allows an executable file to run with the permissions of the file owner, not the user who launched it.
A classic example is passwd.
ls -l /usr/bin/passwd
Output may look like:
-rwsr-xr-x 1 root root 68208 May 14 10:20 /usr/bin/passwd
The s in the owner execute position means setuid.
Why does passwd need this? A normal user must be able to change their password, but password data is stored in protected system files. The command needs controlled elevated privileges.
Set setuid:
sudo chmod u+s executable-file
Numeric form:
sudo chmod 4755 executable-file
The leading 4 means setuid.
Setuid is dangerous when applied incorrectly. A vulnerable setuid root program can become a serious security problem.
Find setuid files:
sudo find / -perm -4000 -type f -exec ls -lh {} \; 2> /dev/null
This command searches for files with setuid bit set.
You do not need to modify setuid files in normal administration unless you know exactly why.
ACLs provide permissions beyond owner group others
Standard permissions allow one owner, one group and others. Sometimes this is not flexible enough. Linux ACLs, access control lists, allow more detailed permission rules.
Check ACLs:
getfacl file.txt
Set ACL for a user:
setfacl -m u:alice:rw file.txt
This gives user alice read and write access to file.txt.
Set ACL for a group:
setfacl -m g:editors:rw file.txt
Remove ACL for a user:
setfacl -x u:alice file.txt
Set default ACL on a directory:
setfacl -m d:g:editors:rwx shared-directory
Default ACLs apply to newly created files and directories inside that directory.
Show ACLs:
getfacl shared-directory
If ls -l shows a plus sign after permissions, ACLs exist.
Example:
-rw-r-----+ 1 pat www-data 1200 May 14 10:20 file.txt
The + means extended ACLs are present.
ACLs are powerful, but they can also make permission troubleshooting more complex. If normal chmod and chown do not explain access behavior, check ACLs.
getfacl /path/to/file
sudo is controlled by policy
The sudo command does not magically give everyone root access. It checks policy. That policy is usually configured through the sudoers system.
The safest way to edit sudoers is:
sudo visudo
Do not edit the sudoers file with a normal editor unless you know exactly what you are doing. visudo checks syntax before saving, reducing the risk of locking out administrative access.
A common sudoers rule looks like:
%sudo ALL=(ALL:ALL) ALL
This means members of group sudo can run commands as any user and group on any host, after authentication.
Check your groups:
id
If your user is in the sudo group, sudo access may be granted depending on configuration.
Add user to sudo group:
sudo usermod -aG sudo username
The user usually needs to log out and back in for group membership to apply.
Test sudo access:
sudo -v
Run a command:
sudo whoami
Expected output:
root
List allowed sudo commands:
sudo -l
This shows what the current user is allowed to run through sudo.
This allows user deploy to reload Nginx without a password, but only that command.
This is much safer than giving full passwordless root access.
Bad rule:
deploy ALL=(ALL) NOPASSWD: ALL
This allows everything without a password. It may be too broad for many situations.
Better rules are specific.
User
Allowed command
Reason
deploy
Reload web server
Needed after deployment
backup
Run backup script
Needed for scheduled backups
monitor
Read status commands
Needed for monitoring
Least privilege applies to sudo too.
Understanding file ownership after extraction
When extracting archives, ownership can matter.
Extract as normal user:
tar -xzf archive.tar.gz
Files may be owned by the extracting user depending on archive and options.
Extract as root:
sudo tar -xzf archive.tar.gz -C /
This may preserve original numeric ownerships, which can be desired for system restores but dangerous if the archive is untrusted.
Inspect archive first:
tar -tzvf archive.tar.gz | head -30
The -v option shows details such as permissions and ownership.
Safer extraction into test directory:
mkdir -p /tmp/extract-test
tar -xzf archive.tar.gz -C /tmp/extract-test
ls -lah /tmp/extract-test
Never extract an unknown archive directly into /, /etc, /usr or a production directory without inspection.
Package management and software installation
Package management is the safest normal way to install, update, remove and investigate software on Debian-based Linux systems.
Package management is the safest way to install software on Linux
Installing software on Linux should usually be done through a package manager. A package manager is not only a download tool. It is a controlled system for finding software, installing it, updating it, verifying dependencies, removing it and keeping the operating system consistent.
On Debian-based systems such as Debian, Ubuntu, Linux Mint, Raspberry Pi OS and many server distributions derived from Debian, the most common package tool is apt.
When you install a program through apt, Linux does more than place one executable file somewhere on the disk. It checks repositories, reads package metadata, resolves dependencies, downloads the correct package versions, installs required libraries, registers the package in the local package database and often configures service files, documentation and default settings.
This matters because Linux software rarely exists as one isolated file. A web server, programming language, database system or desktop application may depend on many libraries and supporting packages.
For example, when you install a package such as apache2, the system may also install required dependencies. When you remove it later, the package manager knows what was installed and can manage it cleanly.
Concept
Meaning
Package
A software unit managed by the operating system
Dependency
Another package required by a package
Repository
A trusted server or source containing packages
Package list
Local index of available packages
Package database
Local record of installed packages
Upgrade
Installing newer versions of installed packages
Remove
Uninstalling a package
Purge
Removing a package and its configuration files
Autoremove
Removing dependencies no longer needed
A beginner may think installing software means downloading a random file from a website. On Linux, that should not be the first habit. The first habit should be: search the package manager.
apt search package-name
Then install using:
sudo apt install package-name
This approach is safer, easier to update and easier to undo.
Understanding repositories
A repository is a software source. It contains packages prepared for your distribution and version. Your system does not automatically know every software package in the world. It knows the repositories configured on that machine.
Repository configuration is commonly stored in files under:
/etc/apt/sources.list
and:
/etc/apt/sources.list.d/
You do not need to edit these files for normal package installation, but it is useful to know they exist.
When you run:
sudo apt update
your system contacts configured repositories and downloads fresh package lists. It does not upgrade your software yet. It only refreshes the local knowledge of what is available.
When you run:
sudo apt upgrade
your system installs newer versions of packages that are already installed, if upgrades are available.
This distinction is essential.
Command
What it does
What it does not do
sudo apt update
Refreshes package lists
Does not install upgrades
sudo apt upgrade
Upgrades installed packages
Does not refresh package lists by itself
sudo apt install package
Installs a package
Does not automatically know newest lists unless update was run
sudo apt remove package
Removes a package
May leave configuration files
sudo apt purge package
Removes package and configuration
Does not remove all user data
sudo apt autoremove
Removes unused dependencies
Does not remove manually installed needed packages
A correct update workflow usually looks like this:
sudo apt update
sudo apt upgrade
This means: first refresh the list of available software versions, then upgrade installed packages.
apt update explained deeply
The command:
sudo apt update
updates the local package index. It asks the repositories: what packages are available, what versions exist and what dependencies are defined?
It does not install new package versions by itself.
This is why the name can be confusing for beginners. In everyday language, “update” often means making software newer. In apt, update means updating the package information database.
A typical workflow before installing software is:
sudo apt update
sudo apt install htop
This ensures that the package manager uses current repository metadata.
If you skip apt update, installation may still work, but your system may use outdated package information. On a server that has not been updated in a long time, this can lead to errors such as package not found, version conflicts or outdated dependency information.
A good habit:
sudo apt update
before:
sudo apt install package-name
and before:
sudo apt upgrade
Common output may include repository lines, package list downloads and information about packages that can be upgraded.
After apt update, the system may say that packages can be upgraded. That means you should decide whether to run:
sudo apt upgrade
or a more complete upgrade command depending on the system policy.
For a beginner or general server maintenance workflow, this is the standard safe starting point:
sudo apt update
sudo apt upgrade
apt upgrade explained deeply
The command:
sudo apt upgrade
upgrades installed packages to newer available versions from configured repositories.
It uses the package lists refreshed by apt update.
The command may show a list of packages to be upgraded and ask for confirmation. You can review the packages before accepting.
Automatic yes:
sudo apt upgrade -y
The -y option answers yes automatically. This can be useful in scripts, but it should not be used blindly on important production systems unless you understand the update policy.
Manual confirmation is safer when learning.
sudo apt upgrade
A good maintenance workflow:
sudo apt update
apt list --upgradable
sudo apt upgrade
The command:
apt list --upgradable
shows which packages can be upgraded before actually upgrading them.
This can reduce installed dependencies, especially on minimal servers or containers. However, it may also omit useful supporting packages. Use it only when you understand the package.
Reinstall a package:
sudo apt install --reinstall package-name
This can help if files belonging to a package were accidentally removed or damaged.
apt search and finding the correct package name
Package names are not always obvious. The program name and package name may differ. This is why apt search is important.
Search for OpenJDK packages:
apt search openjdk
Filter search results:
apt search openjdk | grep jre
Search for Apache-related packages:
apt search apache2
Search for PHP packages:
apt search php
Search can produce many results. Filtering with grep makes it more useful.
apt search php | grep mysql
This searches packages related to PHP and filters lines containing mysql.
This shows how package management connects to text filtering. apt search produces output. grep narrows it.
apt remove and apt purge
The command:
sudo apt remove package-name
removes a package, but usually leaves configuration files behind.
Example:
sudo apt remove vim
This removes the vim package.
If you want to remove the package and system-level configuration files, use:
sudo apt purge vim
The difference matters.
Command
Removes program files
Removes system configuration
apt remove
Yes
Usually no
apt purge
Yes
Yes
After removing packages, unused dependencies may remain. Clean them with:
sudo apt autoremove
A common cleanup workflow:
sudo apt remove package-name
sudo apt autoremove
A more complete removal workflow:
sudo apt purge package-name
sudo apt autoremove
Important detail: user-specific configuration files in home directories are usually not removed by apt purge.
For example, if an application created settings under:
/home/pat/.config/
those may remain because they belong to the user, not the system package database.
This is usually good because removing a package should not casually delete personal user data.
dpkg and manual package installation
The command dpkg is a lower-level Debian package tool. While apt manages repositories and dependencies, dpkg directly installs, removes and queries .deb packages.
However, be careful with random .deb files. Installing packages outside official repositories can introduce compatibility, security and update problems.
Use manual .deb installation when:
Situation
Reason
The software vendor provides an official .deb package
Common for some commercial tools
The package is not available in your repository
Manual installation may be necessary
You understand the source and trust it
Security matters
You know how updates will be handled
Manual packages may not update automatically unless they add a repository
List installed packages:
dpkg -l
Filter installed packages:
dpkg -l | grep php
Show files installed by a package:
dpkg -L package-name
Find which package owns a file:
dpkg -S /path/to/file
Example:
dpkg -S /usr/bin/ls
This can tell you which package provides the ls command.
The package manager is also a system investigation tool.
Practical package workflow for a new server
When starting with a fresh Debian-based server, a common first workflow is:
sudo apt update
sudo apt upgrade
sudo apt install htop tree curl wget unzip zip rsync
This updates the system and installs useful administration tools.
A slightly more careful version:
sudo apt update
apt list --upgradable
sudo apt upgrade
sudo apt install htop tree curl wget unzip zip rsync
This keeps the package database fresh, reviews upgrades, applies them and removes unused dependencies.
Practical workflow: install and start a web server
Install Apache:
sudo apt update
sudo apt install apache2
Start Apache:
sudo systemctl start apache2
Enable Apache at boot:
sudo systemctl enable apache2
Check status:
systemctl status apache2
Check listening ports:
sudo ss -tulpn | grep ':80'
Create a test file:
echo "Apache is working" | sudo tee /var/www/html/test.html
The command tee writes input to a file. With sudo, it can write to protected locations.
Why not simply use this?
sudo echo "Apache is working" > /var/www/html/test.html
Because redirection is handled by the shell before sudo applies to echo. The shell may not have permission to write into /var/www/html.
This works:
echo "Apache is working" | sudo tee /var/www/html/test.html
This is an important Linux lesson: sudo applies to the command, not automatically to shell redirection.
Practical workflow: install, inspect and remove a package
Install a tool:
sudo apt update
sudo apt install tree
Verify command path:
command -v tree
Use it:
tree -L 2
Check package status:
dpkg -l | grep tree
Show package information:
apt show tree
Show installed files:
dpkg -L tree
Remove it:
sudo apt remove tree
Remove unused dependencies:
sudo apt autoremove
This workflow teaches the full lifecycle: search, install, verify, inspect, use and remove.
Practical workflow: safe system update on a small server
A small server maintenance sequence can look like this:
who
uptime
df -h
systemctl --failed
sudo apt update
apt list --upgradable
sudo apt upgrade
sudo apt autoremove
systemctl --failed
Explanation:
Command
Reason
who
See logged-in users
uptime
Check load and runtime
df -h
Ensure enough disk space
systemctl --failed
See current service problems
apt update
Refresh package lists
apt list --upgradable
Review available upgrades
apt upgrade
Apply upgrades
apt autoremove
Remove unused dependencies
systemctl --failed
Confirm no new failed services
If a reboot is needed:
sudo reboot
After reconnecting:
uptime
systemctl --failed
This is controlled maintenance, not random updating.
Practical workflow: inspect a package and its files
Check whether a package is installed:
dpkg -l | grep nginx
Show package details:
apt show nginx
Show installed files:
dpkg -L nginx
Find which package owns a file:
dpkg -S /usr/sbin/nginx
Check service:
systemctl status nginx
This connects package management and service management.
If a configuration file is missing, dpkg -L package can show where it should be. If a binary exists but you do not know where it came from, dpkg -S can identify its package.
apt troubleshooting
Package management can fail for several reasons: network problems, repository issues, locks, broken dependencies, interrupted installations or insufficient disk space.
Start with disk space:
df -h
Update package lists:
sudo apt update
If update fails, read the error. It may be DNS, repository, GPG key, TLS, proxy or network related.
Check internet:
ping -c 4 8.8.8.8
getent hosts deb.debian.org
Fix broken dependencies:
sudo apt -f install
Reconfigure interrupted packages:
sudo dpkg --configure -a
Clean package cache:
sudo apt clean
Remove unused dependencies:
sudo apt autoremove
Check apt logs:
ls -lah /var/log/apt
cat /var/log/apt/history.log
A package lock error may look like another apt process is running. Check processes:
ps aux | grep apt
ps aux | grep dpkg
Do not delete lock files casually. First check whether a real package process is active. Killing or interrupting package operations can damage package state.
A safe recovery sequence after interrupted package work:
This attempts to finish configuration, repair dependencies, refresh package lists and continue upgrades.
Understanding apt remove purge autoremove and clean
These apt commands remove different things.
Command
Purpose
sudo apt remove package
Remove installed package files
sudo apt purge package
Remove package and system configuration files
sudo apt autoremove
Remove unused dependency packages
sudo apt clean
Clear downloaded package cache
sudo apt autoclean
Remove obsolete package files from cache
Example:
sudo apt remove nginx
This removes Nginx package files, but may leave configuration.
sudo apt purge nginx
This removes Nginx and its system configuration files.
sudo apt autoremove
This removes packages installed as dependencies that are no longer needed.
sudo apt clean
This frees space used by cached .deb files.
Check package cache size:
sudo du -sh /var/cache/apt
Clean it:
sudo apt clean
Check again:
sudo du -sh /var/cache/apt
This can help on small systems with limited disk space.
dpkg database investigation
List installed packages:
dpkg -l
Search installed packages:
dpkg -l | grep nginx
Show files installed by a package:
dpkg -L nginx
Find which package owns a file:
dpkg -S /usr/sbin/nginx
Package file lists help answer questions like:
Where did this binary come from?
Which package installed this configuration file?
Which files belong to this package?
Is this command installed by the package I think it is?
Example:
command -v nginx
dpkg -S "$(command -v nginx)"
This finds the executable path and then asks which package owns it.
Processes, services and system control
Processes, services and system control commands show what is running, what failed, which programs own resources and how long-running system components are managed.
date: working with system time in commands
The command date displays the current date and time.
date
Custom format:
date +%Y-%m-%d
Example output:
2026-05-14
Useful date formats:
Command
Example output
date +%Y-%m-%d
2026-05-14
date +%H:%M:%S
10:30:45
date +%Y-%m-%d-%H-%M
2026-05-14-10-30
date +"%Y-%m-%d %H:%M:%S"
2026-05-14 10:30:45
The date command becomes powerful when used in filenames.
System has been running for 5 days and 3 hours 21 minutes
2 users
Number of logged-in users
load average
System load over 1, 5 and 15 minutes
Show boot time:
uptime -s
Example:
2026-05-09 07:21:14
Uptime is useful because some problems appear after restarts, while others appear after long runtime.
If a server rebooted unexpectedly, uptime -s can help confirm when.
System management begins with understanding services
A Linux system runs many background programs. These programs may provide networking, logging, scheduling, databases, web servers, SSH access, printing, monitoring or other functionality.
A background program of this kind is often called a daemon. The managed unit used to start, stop, restart or inspect it is commonly called a service.
Examples of services:
Service
Typical purpose
ssh
Remote shell access
apache2
Apache web server
nginx
Nginx web server
mysql
MySQL or MariaDB database service
cron
Scheduled task service
rsyslog
System logging service
docker
Container engine
php-fpm
PHP FastCGI process manager
On many modern Linux distributions, services are managed by systemd using the systemctl command. Older or compatibility commands may use service.
Both forms are still seen in documentation.
Traditional style:
sudo service apache2 restart
Modern systemd style:
sudo systemctl restart apache2
Both may work on many systems, but systemctl gives more detailed control and status information on systemd-based systems.
service command
The service command is a traditional way to manage services.
Start a service:
sudo service apache2 start
Stop a service:
sudo service apache2 stop
Restart a service:
sudo service apache2 restart
Reload configuration:
sudo service apache2 reload
Check status:
sudo service apache2 status
The difference between restart and reload matters.
Action
Meaning
Start
Start a stopped service
Stop
Stop a running service
Restart
Stop and start again
Reload
Reload configuration without full restart, if supported
Status
Show current service state
Reload is usually less disruptive than restart, but not every service supports reload in the same way.
For example, after changing a web server configuration, you may try:
sudo service apache2 reload
If a full restart is needed:
sudo service apache2 restart
A safe configuration workflow is:
sudo apache2ctl configtest
sudo service apache2 reload
The first command tests Apache configuration before applying changes.
systemctl command
The systemctl command is the primary management tool for systemd services.
Start a service:
sudo systemctl start apache2
Stop a service:
sudo systemctl stop apache2
Restart a service:
sudo systemctl restart apache2
Reload configuration:
sudo systemctl reload apache2
Check status:
systemctl status apache2
Enable service at boot:
sudo systemctl enable apache2
Disable service at boot:
sudo systemctl disable apache2
Check whether service is enabled:
systemctl is-enabled apache2
Check whether service is active:
systemctl is-active apache2
Show failed services:
systemctl --failed
This is extremely useful after boot problems or service failures.
Command
Purpose
systemctl status service
Show service state and recent logs
systemctl start service
Start now
systemctl stop service
Stop now
systemctl restart service
Restart now
systemctl reload service
Reload configuration
systemctl enable service
Start automatically at boot
systemctl disable service
Do not start automatically at boot
systemctl --failed
Show failed units
The word enable does not mean start immediately. It means configure the service to start automatically during boot.
The --now option enables and starts in one command.
update-rc.d and service startup on older systems
The original cheat sheet includes update-rc.d, which is used on older Debian-style init systems and compatibility layers.
Enable SSH at boot:
sudo update-rc.d ssh enable
Disable SSH at boot:
sudo update-rc.d -f ssh remove
On modern systemd systems, the equivalent is usually:
sudo systemctl enable ssh
and:
sudo systemctl disable ssh
Understanding this helps when reading older tutorials. Many Linux guides remain useful, but commands evolve.
Older command
Modern equivalent
sudo service apache2 start
sudo systemctl start apache2
sudo service apache2 stop
sudo systemctl stop apache2
sudo service apache2 restart
sudo systemctl restart apache2
sudo service apache2 reload
sudo systemctl reload apache2
sudo update-rc.d ssh enable
sudo systemctl enable ssh
sudo update-rc.d -f ssh remove
sudo systemctl disable ssh
A serious Linux user should recognize both styles.
Checking service status properly
When a service is not working, do not restart it blindly forever. First inspect its status.
systemctl status apache2
This may show:
Information
Meaning
Loaded
Whether the service unit file exists and is loaded
Active
Whether the service is running, stopped or failed
Main PID
Main process ID
Tasks
Number of related processes
Memory
Memory used by the service
Logs
Recent service log messages
If status shows failed, look at the log lines at the bottom. They often contain the real reason.
For deeper logs, use:
journalctl -u apache2
Follow logs live:
journalctl -u apache2 -f
Show recent logs:
journalctl -u apache2 -n 50
Although journalctl was not one of the central commands in the PDF cheat sheet, it is essential on modern systemd systems because it reads the systemd journal.
A professional service troubleshooting workflow:
systemctl status apache2
journalctl -u apache2 -n 50
sudo apache2ctl configtest
sudo systemctl restart apache2
systemctl status apache2
This reads status, checks logs, tests configuration, restarts and verifies.
Rebooting Linux safely
The command:
sudo reboot
restarts the system.
It is simple, but it should not be used casually on production machines. Rebooting disconnects users, stops services and interrupts running tasks.
Before rebooting a server, consider checking:
who
uptime
systemctl --failed
df -h
These commands show logged-in users, system runtime, failed services and disk space.
A more careful reboot workflow:
who
uptime
sudo reboot
If you are connected over SSH, your session will disconnect. Wait for the server to come back, then reconnect.
ssh user@server
After reconnecting:
uptime
systemctl --failed
This confirms the system booted and shows whether any services failed.
shutdown command
The command shutdown stops or restarts the system at a specific time.
Shut down immediately:
sudo shutdown -h now
Shut down at 20:00:
sudo shutdown -h 20:00
Reboot after one minute:
sudo shutdown -r +1
Cancel a scheduled shutdown:
sudo shutdown -c
Options:
Option
Meaning
-h
Halt or power off
-r
Reboot
now
Immediately
+10
In 10 minutes
20:00
At 20:00 system time
-c
Cancel scheduled shutdown
A useful maintenance message:
sudo shutdown -r +10 "System will reboot for maintenance in 10 minutes"
This warns logged-in users.
On personal machines, immediate reboot is usually fine. On servers, scheduled shutdown is more professional.
Process management: every running program has a process
A process is a running instance of a program. When you start a shell, open an editor, run a web server or execute a script, Linux represents that activity as one or more processes.
Processes have process IDs, owners, states, memory usage, CPU usage and parent-child relationships.
The main commands for basic process management are:
Command
Purpose
ps
Show process list
ps aux
Show detailed process list
top
Interactive real-time process viewer
htop
More user-friendly process viewer
kill
Send a signal to a process by PID
killall
Send a signal to processes by name
pgrep
Find process IDs by name
pkill
Kill processes by name or pattern
The PDF cheat sheet includes ps, kill, killall and htop. These are core commands for understanding what is running on a Linux system.
ps aux explained
The command:
ps aux
shows running processes.
A simplified output may look like this:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 167000 12000 ? Ss May14 0:03 /sbin/init
www-data 842 0.1 1.5 300000 65000 ? S May14 1:20 apache2
pat 2510 0.0 0.1 12000 5000 pts/0 Ss 10:20 0:00 bash
pat 2601 0.0 0.1 15000 4000 pts/0 R+ 10:30 0:00 ps aux
Important columns:
Column
Meaning
USER
User owning the process
PID
Process ID
%CPU
CPU usage percentage
%MEM
Memory usage percentage
VSZ
Virtual memory size
RSS
Physical memory currently used
TTY
Terminal associated with process
STAT
Process state
START
Start time
TIME
CPU time used
COMMAND
Command that started the process
Find processes related to Apache:
ps aux | grep apache
Find PHP processes:
ps aux | grep php
Find Nginx processes:
ps aux | grep nginx
A common beginner question is why grep itself appears in results.
Example:
pat 3001 0.0 0.0 grep apache
This happens because the grep apache command itself contains the word apache.
A common workaround:
ps aux | grep '[a]pache'
This searches for apache without matching the grep command line in the same way.
Another modern tool:
pgrep -a apache
This shows process IDs and command lines matching apache.
Process states
The STAT column in ps aux tells you the process state.
Common process states:
State
Meaning
R
Running or runnable
S
Sleeping, waiting for an event
D
Uninterruptible sleep, often waiting on I/O
T
Stopped
Z
Zombie process
s
Session leader
+
Foreground process group
A sleeping process is not necessarily a problem. Many services spend most of their time waiting for requests.
A zombie process is a process that has finished but still has an entry in the process table because its parent has not collected its exit status. Occasional zombies may appear briefly. Persistent zombies may indicate a parent process problem.
A process in uninterruptible sleep can be more serious, especially if related to disk or network filesystem issues.
For everyday administration, you do not need to memorize every process state immediately, but you should know that ps gives more than a list. It gives clues.
top and htop
The command top shows real-time system activity.
top
It displays CPU usage, memory usage, load average and active processes.
The command htop is a more user-friendly alternative.
htop
If it is not installed:
sudo apt update
sudo apt install htop
Why many administrators prefer htop:
Feature
Benefit
Color display
Easier to read
Interactive scrolling
Easier navigation
Process tree view
Better understanding of relationships
Search function
Quickly locate processes
Kill from interface
Stop processes interactively
CPU and memory bars
Fast visual overview
Inside htop, common keys include:
Key
Action
Arrow keys
Move through process list
F3
Search
F4
Filter
F5
Tree view
F6
Sort
F9
Kill selected process
F10
Quit
You should not kill processes randomly from htop. First identify what they are and why they are running.
A good process investigation workflow:
uptime
htop
ps aux | grep process-name
systemctl status service-name
This connects system load, process view, specific process search and service status.
kill: stopping a process by PID
The command kill sends a signal to a process. Despite the name, it does not always mean immediate destruction. By default, it sends a termination signal asking the process to stop.
Basic syntax:
kill PID
Example:
kill 12345
This sends the default TERM signal.
Force kill:
kill -9 12345
The -9 signal is SIGKILL. It forces the kernel to stop the process. The process cannot clean up, save state or close files gracefully.
Use kill -9 only when normal termination fails.
A better sequence:
kill 12345
sleep 2
ps -p 12345
kill -9 12345
This first asks the process to stop normally, checks whether it is still running and only then forces termination if needed.
Common signals:
Signal
Number
Meaning
SIGTERM
15
Ask process to terminate gracefully
SIGKILL
9
Force process to stop immediately
SIGHUP
1
Hangup, often used to reload configuration
SIGINT
2
Interrupt, similar to Ctrl + C
Send a named signal:
kill -TERM 12345
Force kill:
kill -KILL 12345
Reload-like signal for some processes:
kill -HUP 12345
Whether HUP reloads configuration depends on the program.
killall: stopping processes by name
The command killall sends a signal to processes by name.
Example:
killall php
This sends the default termination signal to processes named php.
Force kill:
killall -9 php
Use this carefully. It may stop more processes than intended.
A safer approach is to inspect first:
pgrep -a php
Then decide whether to use:
killall php
or kill a specific PID:
kill 12345
Comparison:
Command
Target
kill 12345
One process by PID
killall php
All processes with the name php
pkill pattern
Processes matching a pattern
A common beginner mistake is using killall without realizing how broad it can be.
On a shared or production server, prefer precise process IDs unless you are certain.
Process control with Ctrl + C, Ctrl + Z, jobs, fg and bg
Not every process needs to be killed with kill. Some are controlled directly from the terminal.
Stop a running foreground command:
Ctrl + C
This sends an interrupt signal.
Suspend a foreground command:
Ctrl + Z
This pauses it and returns you to the shell.
Show background and stopped jobs:
jobs
Resume a stopped job in the foreground:
fg
Resume a stopped job in the background:
bg
Example:
ping 8.8.8.8
Press:
Ctrl + Z
Then:
jobs
bg
The command continues in the background.
Bring it back:
fg
Stop it:
Ctrl + C
This is useful, but for long-running remote work, tools like screen or tmux are safer because they survive SSH disconnections.
Understanding logs in service management
Services often fail for reasons visible in logs. Linux logs may be stored in different places depending on the service and distribution.
Logs are often the difference between guessing and knowing.
The relationship between packages, services and processes
A strong Linux user understands that packages, services and processes are related but different.
Layer
Example
Meaning
Package
apache2 package
Installed software files managed by package manager
Service
apache2.service
Managed background unit
Process
/usr/sbin/apache2 process
Currently running program instance
Port
:80 or :443
Network endpoint used by service
Log
/var/log/apache2/error.log
Record of events and errors
Installing a package does not always mean the service is running.
A service being enabled does not always mean it is currently active.
A process running does not always mean it is listening on the expected port.
A port listening does not always mean the application is healthy.
You need commands for each layer.
Question
Command
Is the package installed?
`dpkg -l
Is the service active?
systemctl is-active apache2
Is the service enabled at boot?
systemctl is-enabled apache2
What processes are running?
`ps aux
Is the port listening?
`sudo ss -tulpn
What do logs say?
journalctl -u apache2 -n 50
This multi-layer thinking is essential for real troubleshooting.
Understanding foreground and background commands
When you run a command normally, it occupies the terminal until it finishes.
Example:
sleep 60
The terminal waits for 60 seconds.
Run it in the background:
sleep 60 &
The & symbol starts the command in the background.
Show jobs:
jobs
Bring a job to foreground:
fg
Background execution is useful for simple tasks, but it is not enough for important long-running remote tasks. If your SSH connection closes, background jobs may still end depending on how they were started.
For reliable long sessions, use:
screen
or:
tmux
The command screen is included in the original command set, and it remains useful for long-running administration work.
nohup for simple persistent commands
Another tool for keeping commands running after logout is nohup.
Example:
nohup ./long-script.sh > script.log 2>&1 &
This runs the script in the background and redirects output into script.log.
Breakdown:
Part
Meaning
nohup
Ignore hangup signal after logout
./long-script.sh
Command to run
>
Redirect standard output
script.log
Output file
2>&1
Redirect errors to the same place as output
&
Run in background
For beginners, screen is often easier because you can reattach and see the session interactively.
For scripts and automation, nohup can be useful.
watch: repeat a command automatically
The command watch repeatedly runs another command and displays the output.
Basic usage:
watch date
This refreshes every 2 seconds by default.
Run every 10 seconds:
watch -n 10 date
Watch disk space:
watch -n 5 df -h
Watch memory:
watch -n 5 free -h
Watch a directory:
watch -n 2 ls -lah
Watch listening ports:
watch -n 2 "ss -tulpn"
Use quotes when the watched command contains spaces, pipes or complex syntax.
Example:
watch -n 2 "ps aux | grep apache"
This repeats the full pipeline.
The watch command is useful during deployments, file transfers, log growth, service restarts and disk cleanup.
lsof: list open files
The command lsof means list open files.
lsof
This can produce a lot of output because Linux treats many things as files: regular files, directories, sockets, devices and network connections.
Find processes using a file:
lsof /path/to/file
Find what uses a port:
sudo lsof -i :80
Find what uses port 443:
sudo lsof -i :443
Find open deleted files:
sudo lsof | grep deleted
This is useful when disk space remains used after deleting large files.
Example workflow:
df -h
sudo lsof | grep deleted
If a large deleted file is still open by a process, restarting that process may release disk space.
Find network connections:
sudo lsof -i
The command lsof is advanced, but extremely valuable in troubleshooting.
netstat and ss
The command netstat appears in many older guides and cheat sheets. It shows network connections, routing tables and listening ports.
Show listening ports:
netstat -l
Show listening ports with process information:
sudo netstat -lp
Show continuously:
netstat -lpc
On many modern systems, ss is preferred.
Show listening TCP and UDP ports with process names:
sudo ss -tulpn
Breakdown:
Option
Meaning
-t
TCP
-u
UDP
-l
Listening sockets
-p
Show process
-n
Numeric addresses and ports
Check whether port 80 is listening:
sudo ss -tulpn | grep ':80'
Check SSH port:
sudo ss -tulpn | grep ':22'
netstat is still useful to recognize, but ss is usually the modern default.
dmesg: kernel messages
The command dmesg displays kernel ring buffer messages.
dmesg
It is useful for hardware, drivers, boot messages, disks, USB devices and kernel-level errors.
Search for errors:
dmesg | grep -i error
Search for USB events:
dmesg | grep -i usb
Follow kernel messages live:
sudo dmesg -w
A practical use case: plug in a USB drive, then run:
dmesg | tail -n 30
This may show which device name was assigned, such as /dev/sdb.
Another use case: disk problems.
dmesg | grep -i "I/O error"
Kernel messages are not everyday reading for beginners, but they are essential when diagnosing hardware or low-level system problems.
Key takeaways
At this point, the guide moves from file-level work into system-level administration. The command families now cover software installation, system updates, services, processes, long-running sessions and active server diagnosis.
The key commands in this section are:
Command
Essential role
apt update
Refresh package lists
apt upgrade
Upgrade installed packages
apt install
Install packages
apt remove
Remove packages
apt purge
Remove packages and configuration
apt search
Find packages
apt show
Display package details
dpkg -i
Install local .deb packages
dpkg -l
List installed packages
dpkg -L
Show files installed by a package
dpkg -S
Find which package owns a file
service
Traditional service management
systemctl
Modern service management
reboot
Restart the system
shutdown
Shut down or schedule restart
ps aux
List running processes
htop
Interactive process monitoring
kill
Stop process by PID
killall
Stop processes by name
screen
Keep terminal sessions alive
history
Show command history
watch
Repeat commands automatically
lsof
List open files and ports
netstat
Older network status tool
ss
Modern socket and port inspection
dmesg
Kernel and hardware messages
The deeper lesson is that Linux system administration is layered.
A package installs software. A service manages background behavior. A process is the running instance. A port exposes network access. A log explains what happened. A permission controls access. A filesystem stores the data. A command lets you inspect each layer.
Once you see those layers clearly, troubleshooting becomes much more precise. You no longer ask only “Why does it not work?” You ask better questions.
Is the package installed?
Is the service enabled?
Is it active?
Did it fail?
What do the logs say?
Is the process running?
Is the port listening?
Is the disk full?
Are permissions correct?
Was a configuration file changed recently?
Every one of those questions has a command. That is the real power of Linux.
Practical workflow: monitor a command repeatedly
Use watch to repeat commands.
Watch disk usage:
watch -n 5 df -h
Watch a directory during file transfer:
watch -n 2 "ls -lh /home/pat/downloads"
Watch memory:
watch -n 5 free -h
Watch top error count:
watch -n 10 "grep -i error app.log | wc -l"
Watch listening ports:
watch -n 2 "ss -tulpn"
The command must be quoted when it contains pipes or multiple words.
Without quotes, the shell may interpret only part of the command as the argument to watch.
Correct:
watch -n 10 "ps aux | grep nginx"
Risky or wrong:
watch -n 10 ps aux | grep nginx
The second form pipes the output of watch, not the intended repeated command.
Practical workflow: use lsof to solve file and port mysteries
Find what is using port 80:
sudo lsof -i :80
Find what is using port 443:
sudo lsof -i :443
Find processes using a mounted directory before unmounting:
sudo lsof +D /mnt/usb
This can help when umount says the target is busy.
Find deleted files still open:
sudo lsof | grep deleted
If disk space is not freed after deleting a large log, this may reveal the process still holding the file open.
The three load numbers represent 1-minute, 5-minute and 15-minute averages.
Load is not exactly CPU percentage. It represents processes running or waiting for CPU and sometimes uninterruptible I/O.
Interpreting load depends on CPU core count.
Check CPU count:
nproc
If a system has 1 CPU core, a load of 1.00 may mean it is fully loaded. If it has 8 cores, a load of 1.00 is usually light.
Basic interpretation:
Load compared with CPU cores
Meaning
Much lower than core count
Usually fine
Around core count
Busy but possibly normal
Much higher than core count
Potential overload
High load with low CPU
Possible I/O wait problem
Use htop or top to see more.
htop
If load is high, check CPU consumers:
ps aux --sort=-%cpu | head -15
Check memory:
free -h
Check disk I/O symptoms through logs:
dmesg | grep -i error
Load is a clue, not a conclusion.
Memory and swap basics
Check memory:
free -h
Example output:
total used free shared buff/cache available
Mem: 7.7Gi 2.1Gi 1.0Gi 120Mi 4.6Gi 5.2Gi
Swap: 2.0Gi 0.0Gi 2.0Gi
Important fields:
Field
Meaning
total
Total memory
used
Used memory
free
Completely unused memory
buff/cache
Memory used for buffers and cache
available
Estimated memory available for applications
Swap
Disk-based memory extension
Linux uses free memory for cache. This is normal. Do not panic just because free memory looks low. The available column is usually more useful.
Check swap devices:
swapon --show
High swap usage may indicate memory pressure, but some swap usage is not always a problem.
Find memory-heavy processes:
ps aux --sort=-%mem | head -15
Use htop for interactive inspection.
htop
Memory troubleshooting should ask whether usage is expected. Databases, search engines, Java applications and caches may use large amounts of memory by design.
journalctl is the modern log reader for systemd systems
Many modern Linux systems use systemd. The logs managed by systemd are read with journalctl.
Then relative paths inside the application start from that directory.
In cron, avoid relative paths unless the script explicitly changes directory.
cd /home/pat/myapp || exit 1
Then run commands.
The current directory is an invisible dependency. Make it explicit.
Understanding signals beyond kill -9
The kill command sends signals. The default signal is usually SIGTERM, which asks a process to terminate gracefully.
Show available signals:
kill -l
Common signals:
Signal
Number
Meaning
SIGHUP
1
Hangup, often reload configuration
SIGINT
2
Interrupt, like Ctrl + C
SIGTERM
15
Graceful termination request
SIGKILL
9
Immediate forced termination
SIGSTOP
19 or system-dependent
Stop process
SIGCONT
18 or system-dependent
Continue stopped process
Graceful stop:
kill PID
Explicit graceful stop:
kill -TERM PID
Force stop:
kill -KILL PID
or:
kill -9 PID
Do not use kill -9 as the first option. It prevents cleanup. Files may remain incomplete. Locks may remain. Temporary state may not be cleaned.
For managed services, prefer:
sudo systemctl stop service-name
or:
sudo systemctl restart service-name
Systemd understands the service better than a random PID kill.
Process trees explain parent and child relationships
Processes can start other processes. This creates a process tree.
Show process tree with ps:
ps auxf
Install and use pstree if available:
sudo apt install psmisc
pstree -p
The -p option shows PIDs.
A web server may have a master process and worker processes. A shell may start scripts. A service manager may start a daemon.
Why process trees matter:
Situation
Why tree helps
Killing parent may stop children
Useful or dangerous depending on context
Worker processes belong to master
Restart service instead of killing workers
Script started many child commands
Need to understand full activity
Zombie process exists
Parent relationship matters
High CPU child process
Need to know what started it
Example:
ps auxf | grep nginx
This may show a master Nginx process and worker processes.
Do not kill worker processes randomly if the service manager should handle them.
Nice and process priority
Linux processes have scheduling priority. The nice value influences CPU scheduling.
Show nice values:
ps -eo pid,ni,comm | head
Run a command with lower priority:
nice -n 10 tar -czf backup.tar.gz /large/source
Higher nice value means lower priority.
Renice a running process:
sudo renice 10 -p PID
This can be useful for CPU-heavy tasks such as compression or batch processing.
Do not use priority changes as a substitute for proper capacity planning, but they can reduce impact of background jobs.
Example backup with lower CPU priority:
nice -n 10 tar -czf backup.tar.gz /var/www/site
Combine with I/O priority if ionice is available:
ionice -c2 -n7 nice -n 10 tar -czf backup.tar.gz /var/www/site
This tries to make the backup less disruptive.
Understanding PID files
A PID file contains a process ID. Services sometimes use PID files to record which process is running.
Common locations:
/run/service-name.pid
/var/run/service-name.pid
/var/run is often a link to /run.
Read PID file:
cat /run/service-name.pid
Check process:
ps -p "$(cat /run/service-name.pid)" -f
If the PID file points to a process that no longer exists, it may be stale.
Systemd often manages processes without requiring traditional PID files, but many services and scripts still use them.
Storage, disks, mounts and filesystem usage
Storage troubleshooting starts with free space, directory usage, mounts, persistent mount configuration and temporary data.
Disk space with df
The command df shows free and used space on mounted filesystems.
Basic command:
df
Human-readable output:
df -h
The -h option means human-readable. It shows sizes in KB, MB, GB or TB instead of raw blocks.
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 31G 17G 65% /
/dev/sdb1 200G 120G 80G 60% /mnt/data
Important columns:
Column
Meaning
Filesystem
Device or filesystem source
Size
Total size
Used
Used space
Avail
Available space
Use%
Percentage used
Mounted on
Where it appears in the filesystem
Check a specific path:
df -h /var
Check the filesystem containing your current directory:
df -h .
The command df answers this question: how full is the filesystem?
It does not tell you which folder is responsible. For that, use du.
Disk usage with du
The command du shows how much space files and directories use.
Basic command:
du
This can produce a lot of output.
Human-readable summary of current directory:
du -sh .
Human-readable summary of a specific directory:
du -sh /home/pat
Show sizes of immediate items inside a directory:
du -h --max-depth=1 /home/pat
Sort by size:
du -h --max-depth=1 /home/pat | sort -h
Show largest items at the bottom:
du -h --max-depth=1 /home/pat | sort -h
Show largest items at the top with numeric kilobyte sorting:
du -ak /home/pat | sort -nr | head -20
The command from the cheat sheet style is especially useful:
du -ak | sort -nr | head -20
This finds the largest items under the current directory.
Command part
Purpose
du -ak
Show disk usage in kilobytes for all items
sort -nr
Sort numerically from largest to smallest
head -20
Show only the first 20 lines
A common disk investigation workflow:
df -h
du -sh /*
du -h --max-depth=1 /var | sort -h
du -h --max-depth=1 /var/log | sort -h
This starts broad and narrows down.
The difference between df and du is important.
Command
Answers
df -h
Which filesystem is full?
du -sh folder
How much space does this folder use?
du -h --max-depth=1
Which subfolder is large?
Use df first to identify the full filesystem. Use du next to find what is using space.
Why df and du may disagree
Sometimes df says a filesystem is full, but du does not show enough files to explain the usage. This can happen when a deleted file is still held open by a running process.
Linux allows a process to keep writing to a file even after the file name has been removed from the directory. The file disappears from normal listings, but the disk space is not released until the process closes it.
You can investigate this with lsof.
sudo lsof | grep deleted
If a huge deleted log file is still open, restarting the related service may release the space.
This is an advanced but very real Linux troubleshooting situation.
Mounting and unmounting filesystems
Linux does not use drive letters like C: or D:. A disk or partition becomes accessible by being mounted somewhere in the filesystem tree.
The command mount attaches a filesystem to a directory.
The command umount detaches it.
Mount a partition:
sudo mount /dev/sda1 /mnt/usb
Unmount it:
sudo umount /mnt/usb
The command is umount, not unmount.
The directory /mnt/usb is called a mount point. It must exist before mounting.
sudo mkdir -p /mnt/usb
sudo mount /dev/sda1 /mnt/usb
Check mounted filesystems:
mount
More readable disk overview:
lsblk
Example output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 50G 0 disk
└─sda1 8:1 0 50G 0 part /
sdb 8:16 1 32G 0 disk
└─sdb1 8:17 1 32G 0 part /mnt/usb
The command lsblk is very useful before mounting because it shows available block devices.
A safe USB mounting workflow:
lsblk
sudo mkdir -p /mnt/usb
sudo mount /dev/sdb1 /mnt/usb
df -h /mnt/usb
ls -lah /mnt/usb
Before unplugging or removing a mounted device:
sudo umount /mnt/usb
Unmounting ensures pending writes are completed and the filesystem is detached safely.
Practical workflow: investigate a full disk
A real Linux administrator often starts with a warning: disk usage is high. The goal is not to delete something quickly. The goal is to find the cause safely.
Start with filesystems:
df -h
Find which filesystem is full.
If / is full, inspect top-level directories:
sudo du -h --max-depth=1 / | sort -h
If /var is large:
sudo du -h --max-depth=1 /var | sort -h
If /var/log is large:
sudo du -h --max-depth=1 /var/log | sort -h
Find large log files:
sudo find /var/log -type f -size +100M -exec ls -lh {} \;
Check compressed old logs:
sudo find /var/log -type f -iname "*.gz" -mtime +30 -exec ls -lh {} \;
Only after review, remove old compressed logs:
sudo find /var/log -type f -iname "*.gz" -mtime +30 -delete
Check space again:
df -h
If space is still not freed, check deleted open files:
sudo lsof | grep deleted
This workflow shows professional Linux thinking: identify, narrow down, verify, act, verify again.
Disk usage and inode usage
Disk space has two important dimensions: blocks and inodes.
Check block usage:
df -h
Check inode usage:
df -i
A filesystem can have free space but no free inodes. This happens when there are too many small files.
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext4 50G 20G 28G 42% /
The filesystem type matters. Permissions, ownership, symlinks and special features behave differently across filesystems such as ext4, xfs, btrfs, nfs, cifs, vfat or exfat.
For example, FAT-based filesystems do not support Unix permissions in the same way as ext4.
If permissions behave strangely on a mounted drive, check filesystem type.
lsblk -f
df -Th
mount | grep /mnt/usb
fstab controls persistent mounts
The file:
/etc/fstab
defines filesystems that should mount automatically.
sudo umount /mnt/data
sudo mount -a
df -h /mnt/data
The nofail option can prevent boot failure if an external disk is missing, depending on the use case.
Understanding temporary directories
Linux has multiple temporary locations.
Directory
Typical behavior
/tmp
Temporary files, often cleaned on reboot or by policy
/var/tmp
Temporary files that may persist longer
/run
Runtime files, cleared on reboot
User cache
Usually under ~/.cache
Check /tmp:
du -sh /tmp
Find old files:
sudo find /tmp -type f -mtime +7 -exec ls -lh {} \;
Do not blindly run:
sudo rm -rf /tmp/*
Some running programs may be using files there.
A safer cleanup:
sudo find /tmp -type f -mtime +7 -delete
sudo find /tmp -type d -empty -delete
Still use caution. Temporary does not always mean safe to delete right now.
Networking, SSH and remote file transfer
Networking, SSH and remote transfer commands make Linux useful across servers, hosting environments and production systems.
Networking in Linux begins with understanding interfaces
Networking in Linux starts with a simple question: how does this machine connect to the outside world?
A Linux system can have one network interface or many. A laptop may have Wi-Fi, Ethernet, Bluetooth networking and virtual interfaces created by VPNs or containers. A server may have one public interface, one private interface, loopback, bridge interfaces, Docker networks, VLANs or bonded interfaces. A Raspberry Pi may have Ethernet, Wi-Fi and USB networking. The command line gives you a precise way to see all of them.
A network interface is the system’s connection point to a network. It may be physical, such as an Ethernet card, or virtual, such as a bridge used by containers.
Common interface names include:
Interface name
Typical meaning
lo
Loopback interface used by the system to talk to itself
eth0
Traditional name for the first Ethernet interface
wlan0
Traditional name for the first Wi-Fi interface
enp0s3
Predictable modern Ethernet interface name
ens33
Another common modern Ethernet interface name
wlp2s0
Predictable modern Wi-Fi interface name
docker0
Docker bridge interface
br0
Bridge interface
tun0
Tunnel interface, often used by VPNs
The loopback interface lo is always important. It usually uses the IP address:
127.0.0.1
and the hostname:
localhost
This means the machine is communicating with itself. Many local services listen on loopback when they should be available only locally.
The first practical networking skill is learning how to inspect interfaces and addresses.
ip: the modern command for network inspection
The modern Linux command for network information is ip.
Show all network addresses:
ip a
or the longer form:
ip address
Typical output may include interface names, MAC addresses, IPv4 addresses, IPv6 addresses and interface states.
A simplified example:
2: enp0s3: mtu 1500
link/ether 08:00:27:12:34:56 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.50/24 brd 192.168.1.255 scope global dynamic enp0s3
inet6 fe80::a00:27ff:fe12:3456/64 scope link
This output contains several important pieces of information.
Field
Meaning
enp0s3
Interface name
UP
Interface is enabled
LOWER_UP
Physical or link-layer connection is active
link/ether
MAC address line
inet
IPv4 address
192.168.1.50/24
IPv4 address with subnet prefix
brd
Broadcast address
dynamic
Address was likely assigned by DHCP
inet6
IPv6 address
The /24 part is CIDR notation. It describes the subnet size. In many home and office networks, /24 corresponds to a subnet mask of:
255.255.255.0
This means addresses such as:
192.168.1.1
192.168.1.50
192.168.1.200
are usually in the same local network if they share the same /24 network.
Show only IPv4 addresses:
ip -4 a
Show only IPv6 addresses:
ip -6 a
Show a specific interface:
ip a show enp0s3
Bring an interface up:
sudo ip link set enp0s3 up
Bring an interface down:
sudo ip link set enp0s3 down
Show routing table:
ip route
Example output:
default via 192.168.1.1 dev enp0s3
192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.50
This tells you two important things.
Route
Meaning
default via 192.168.1.1
Traffic to other networks goes through gateway 192.168.1.1
192.168.1.0/24 dev enp0s3
Local network is reachable through interface enp0s3
The default gateway is essential. If your IP address is correct but there is no default route, the machine may communicate locally but not reach the internet.
ifconfig: the older network command
The command ifconfig is older and may not be installed by default on many modern distributions. It is still common in older tutorials, embedded systems and some minimal environments.
Basic use:
ifconfig
Show a specific interface:
ifconfig eth0
If the command is missing, the system may show:
ifconfig: command not found
On Debian-based systems, it can often be installed with:
sudo apt update
sudo apt install net-tools
However, the modern replacement is usually:
ip a
Comparison:
Older command
Modern equivalent
ifconfig
ip a
ifconfig eth0
ip a show eth0
route -n
ip route
netstat -tulpn
ss -tulpn
A serious Linux user should recognize ifconfig, but should become comfortable with ip.
Understanding private and public IP addresses
When inspecting network configuration, you will often see private IP addresses.
Common private IPv4 ranges are:
Range
Typical use
10.0.0.0/8
Large private networks
172.16.0.0/12
Medium private networks
192.168.0.0/16
Home and small office networks
Examples:
192.168.1.50
10.0.0.25
172.16.5.10
These addresses are not directly reachable from the public internet. They are used inside private networks.
A server may also have a public IP address if it is directly reachable from the internet. In cloud environments, the public IP may be assigned externally by the provider, while the operating system sees only a private address. That depends on the infrastructure.
You can inspect local addresses with:
ip a
You can inspect routes with:
ip route
Do not confuse local IP configuration with public internet identity. They may be different, especially behind NAT, routers, cloud load balancers or VPNs.
ping: testing basic connectivity
The command ping tests whether another host responds to ICMP echo requests.
Basic use:
ping 192.168.1.1
This sends packets continuously until stopped with:
Ctrl + C
Send only four packets:
ping -c 4 192.168.1.1
Test a domain:
ping example.com
Test a public IP address:
ping -c 4 8.8.8.8
The output may look like this:
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.25 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.10 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=1.18 ms
Important fields:
Field
Meaning
64 bytes
Size of response
from 192.168.1.1
Host that replied
icmp_seq
Packet sequence number
ttl
Time to live
time
Round-trip latency
At the end, ping prints statistics.
4 packets transmitted, 4 received, 0% packet loss
Packet loss is important. High packet loss indicates network instability.
Result
Meaning
0% packet loss
Good basic connectivity
50% packet loss
Unstable connection
100% packet loss
No ICMP response or blocked ICMP
Very high latency
Slow or distant connection
Unknown host
DNS resolution problem
Network unreachable
Routing or interface problem
However, ping is not a complete test. Some servers block ICMP. A failed ping does not always mean the server is down. It may only mean ICMP is blocked.
A better diagnostic approach uses several tests.
ip a
ip route
ping -c 4 192.168.1.1
ping -c 4 8.8.8.8
ping -c 4 example.com
This sequence checks local configuration, route, gateway reachability, internet IP reachability and DNS resolution.
Diagnosing internet connectivity step by step
When internet connectivity fails, do not guess. Test in layers.
First, check whether the system has an IP address:
ip a
Then check whether it has a default route:
ip route
Then ping the gateway:
ping -c 4 192.168.1.1
Replace 192.168.1.1 with your actual gateway from ip route.
Then ping an external IP:
ping -c 4 8.8.8.8
Then ping a domain:
ping -c 4 example.com
Interpretation:
What works
What fails
Likely issue
No IP address
Everything
DHCP, interface or cable/Wi-Fi problem
IP exists, no default route
Internet access
Routing problem
Gateway ping fails
External access
Local network problem
Gateway works, external IP fails
Internet routing, firewall or ISP problem
External IP works, domain fails
DNS problem
Domain works
Basic internet connectivity is working
Check DNS configuration:
cat /etc/resolv.conf
On many modern systems, DNS may be managed by systemd-resolved, NetworkManager or another service, so /etc/resolv.conf may point to a local stub resolver. Still, it is often useful for inspection.
A practical DNS test tool is getent.
getent hosts example.com
If this returns an IP address, name resolution is working through the system resolver.
hostname: identifying the machine
The command hostname displays the system hostname.
hostname
Example output:
webserver01
The hostname is the system’s local name. It may appear in prompts, logs, monitoring systems and network identification.
Set hostname temporarily:
sudo hostname webserver01
On modern systems, use:
sudo hostnamectl set-hostname webserver01
Show detailed hostname information:
hostnamectl
Example output may include static hostname, operating system, kernel and architecture.
A good hostname should be meaningful.
Poor hostname
Better hostname
server
web-prod-01
ubuntu
db-staging-01
test
backup-lab-01
raspberrypi
sensor-garage-01
Naming matters because logs and monitoring become much easier to understand when machines have clear names.
A hostname should usually indicate function, environment and number if there are multiple machines.
The command iwconfig shows wireless interface information. It is older, but still useful on systems where wireless tools are available.
Basic use:
iwconfig
Show one interface:
iwconfig wlan0
The output may include SSID, mode, frequency, access point, bit rate and signal quality.
Important fields:
Field
Meaning
ESSID
Wi-Fi network name
Mode
Managed, monitor or other wireless mode
Frequency
Wireless frequency
Access Point
MAC address of the access point
Bit Rate
Link speed
Signal level
Wireless signal strength
On many modern systems, Wi-Fi is managed through tools such as NetworkManager, nmcli, iw or desktop network managers. Still, iwconfig appears in many Linux learning materials and remains useful to recognize.
A practical wireless troubleshooting sequence:
ip a
iwconfig
ip route
ping -c 4 192.168.1.1
This checks whether the wireless interface exists, whether it has wireless association information, whether routing exists and whether the gateway responds.
ifup and ifdown
The commands ifup and ifdown enable or disable network interfaces using traditional network configuration systems.
Enable an interface:
sudo ifup eth0
Disable an interface:
sudo ifdown eth0
These commands are most relevant on systems using /etc/network/interfaces style configuration. On systems using NetworkManager, systemd-networkd or cloud network tools, they may not apply or may not behave as expected.
Modern alternatives may include:
sudo ip link set eth0 up
sudo ip link set eth0 down
or NetworkManager commands such as:
nmcli connection show
The key principle is that network management depends on the distribution and configuration stack. The command line helps inspect what is present.
If ifup and ifdown fail, do not assume the interface is broken. The system may be managed by a different network service.
wget: downloading files from the terminal
The command wget downloads files from URLs.
Basic use:
wget http://example.com/file.txt
Download and save with a different filename:
wget http://example.com/file.txt -O target.txt
The -O option means write output to a specific file.
Download into the current directory using the remote filename:
This can be useful on slow connections or shared servers.
A safe download workflow:
mkdir -p ~/downloads
cd ~/downloads
wget https://example.com/archive.tar.gz
ls -lh
tar -tzf archive.tar.gz | head
This downloads into a controlled directory, checks the file size and inspects archive contents before extraction.
curl compared with wget
Although the original command list focuses on wget, many Linux systems also use curl.
Basic curl use:
curl https://example.com
Save to file:
curl -o file.txt https://example.com/file.txt
Follow redirects:
curl -L -o file.txt https://example.com/file.txt
Show only HTTP headers:
curl -I https://example.com
Comparison:
Task
wget
curl
Download a file simply
Very convenient
Possible
Save remote file with same name
Default behavior
Needs option in many cases
Test HTTP APIs
Less common
Very common
Show headers
Possible
Very convenient
Use in scripts
Good
Excellent
Resume downloads
wget -c
curl -C -
For downloading files, wget is very comfortable. For API testing and HTTP inspection, curl is often preferred.
A Linux user should know both, but wget remains one of the easiest download tools.
ssh: remote login to Linux systems
The command ssh connects to another system securely over the network.
Basic syntax:
ssh user@host
Example:
ssh pat@192.168.1.10
This means: connect to host 192.168.1.10 as user pat.
Connect to a hostname:
ssh pat@webserver01
Connect using a specific port:
ssh -p 2222 pat@192.168.1.10
The default SSH port is 22, but servers may use a different port.
When connecting for the first time, SSH may ask whether you trust the host key.
Are you sure you want to continue connecting?
This protects against connecting to an unexpected machine. In professional environments, host keys should be verified carefully.
After successful connection, you receive a shell on the remote machine. Commands then run on that remote system, not your local computer.
This distinction is critical.
If you SSH into a server and run:
rm file.txt
you are deleting file.txt on the server, not on your local machine.
A safe habit after logging in:
hostname
whoami
pwd
This confirms where you are, which machine you are on and which user you are using.
SSH identity and who you are on the remote system
After logging in through SSH, verify identity.
whoami
Show hostname:
hostname
Show current directory:
pwd
Show logged-in users:
who
Show recent login information:
last | head
This matters because administrators often manage multiple servers. Accidentally running a command on production instead of staging can cause serious damage.
A helpful prompt or clear hostname reduces risk. If hostnames are unclear, fix them.
sudo hostnamectl set-hostname web-staging-01
A professional SSH habit:
hostname
whoami
uptime
This immediately tells you where you are, who you are and how long the system has been running.
SSH keys
SSH can authenticate with passwords or keys. SSH keys are usually safer and more convenient when managed properly.
A key pair contains:
Part
Purpose
Private key
Stays on your local machine and must be protected
Public key
Can be placed on servers you want to access
Generate an SSH key:
ssh-keygen
A common modern form:
ssh-keygen -t ed25519
This creates keys usually under:
~/.ssh/
Common files:
File
Meaning
~/.ssh/id_ed25519
Private key
~/.ssh/id_ed25519.pub
Public key
~/.ssh/authorized_keys
Public keys allowed to log into this account
~/.ssh/known_hosts
Known server host keys
~/.ssh/config
Optional client configuration
The private key must be protected.
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
If permissions are too open, SSH may refuse to use the key.
Copy a public key to a server:
ssh-copy-id pat@192.168.1.10
Then connect:
ssh pat@192.168.1.10
If ssh-copy-id is not available, you can manually append the public key to the remote user’s authorized_keys file, but that requires care.
SSH client configuration
If you connect to servers often, create an SSH config file.
Open:
nano ~/.ssh/config
Example:
Host webprod
HostName 192.168.1.10
User pat
Port 22
IdentityFile ~/.ssh/id_ed25519
Then connect using:
ssh webprod
This is easier than typing the full command every time.
More examples:
Host staging
HostName staging.example.local
User deploy
Port 2222
Host backup
HostName 10.0.0.50
User backupuser
IdentityFile ~/.ssh/backup_key
Set secure permissions:
chmod 600 ~/.ssh/config
SSH config improves productivity and reduces mistakes.
Running one remote command with ssh
SSH can run one command on a remote server without opening an interactive shell.
ssh pat@192.168.1.10 "uptime"
Check disk space remotely:
ssh pat@192.168.1.10 "df -h"
Check failed services:
ssh pat@192.168.1.10 "systemctl --failed"
Run multiple commands:
ssh pat@192.168.1.10 "hostname; uptime; df -h"
This is useful for quick administration and scripting.
Example for multiple servers:
for server in web1 web2 web3; do
echo "Checking $server"
ssh "$server" "hostname; uptime; df -h /"
done
This shows how SSH becomes an automation tool.
scp: copying files over SSH
The command scp copies files between machines using SSH.
Important detail: ssh uses lowercase -p for port, but scp uses uppercase -P.
Command
Port option
ssh
-p
scp
-P
This difference is a common source of mistakes.
rsync: efficient synchronization
The command rsync copies and synchronizes files. It is often better than scp for repeated transfers, backups and large directories because it can transfer only changed parts.
The --delete option is useful when the destination should be an exact mirror. It is dangerous when the destination contains files that are not present in the source but should be preserved.
Choosing between scp and rsync
Both scp and rsync copy files over SSH, but they serve different practical needs.
Situation
Better tool
Copy one small file quickly
scp
Copy a directory once
scp or rsync
Synchronize a directory repeatedly
rsync
Back up changed files
rsync
Preserve permissions and timestamps
rsync -a
Preview changes before transfer
rsync --dry-run
Delete destination files not in source
rsync --delete
Transfer over a custom SSH port
Both can do it
Use scp for simple transfers. Use rsync for serious synchronization.
If a service is running but not listening, check its configuration and logs.
systemctl status nginx
journalctl -u nginx -n 50
If another process is using the port:
sudo lsof -i :80
Then decide whether the conflict is expected or needs correction.
Practical workflow: check whether a remote port is reachable
From your local machine or another server, you can test TCP connectivity with tools such as nc if installed.
nc -vz server 80
Check HTTPS:
nc -vz server 443
If nc is not installed:
sudo apt install netcat-openbsd
You can also use curl for HTTP or HTTPS services.
curl -I http://server
For HTTPS:
curl -I https://server
Interpretation:
Test
What it proves
ss -tulpn on server
Service is listening locally
curl -I localhost on server
Local HTTP response works
curl -I http://server from another machine
Network path and service work externally
nc -vz server 80
TCP port is reachable
ping server
ICMP responds, but does not prove port availability
A common mistake is relying only on ping. A server can respond to ping while the web port is closed. A server can also block ping while the web port is open.
This workflow is professional because it preserves a recovery path.
Practical network checklist
When a Linux machine has network problems, use this checklist:
hostname
ip a
ip route
ping -c 4 127.0.0.1
ping -c 4
ping -c 4 8.8.8.8
getent hosts example.com
ping -c 4 example.com
Replace with the actual gateway from:
ip route
Then inspect listening services if the problem is inbound access:
sudo ss -tulpn
Check firewall rules if relevant:
sudo ufw status
On systems using UFW, this shows whether the firewall is active and which ports are allowed.
For service logs:
journalctl -u service-name -n 100
This checklist is not random. It moves from local identity to interface, route, local stack, gateway, internet, DNS, inbound ports and logs.
Basic firewall awareness with ufw
Many Debian and Ubuntu systems may use UFW, the uncomplicated firewall. It is not part of the original command set, but it is directly relevant to network troubleshooting.
Check status:
sudo ufw status
Verbose status:
sudo ufw status verbose
Allow SSH:
sudo ufw allow ssh
Allow HTTP:
sudo ufw allow 80/tcp
Allow HTTPS:
sudo ufw allow 443/tcp
Enable firewall:
sudo ufw enable
Important warning: before enabling a firewall on a remote server, make sure SSH is allowed.
sudo ufw allow ssh
sudo ufw enable
Otherwise, you may lock yourself out.
Check numbered rules:
sudo ufw status numbered
Delete a rule by number:
sudo ufw delete 3
Firewall troubleshooting must be careful because blocking access can make remote administration impossible.
Name resolution and /etc/hosts
Linux can resolve names through DNS and local files.
This means the system can resolve webserver01 to 192.168.1.10 locally.
Edit carefully:
sudo nano /etc/hosts
Test resolution:
getent hosts webserver01
The /etc/hosts file is useful for small labs, local development, temporary migrations and internal names. For larger environments, DNS is usually better.
This is a basic migration pattern. For databases, services and live applications, additional steps are required, but the file transfer logic remains fundamental.
Practical workflow: backup a remote directory with rsync
This prevents losing visibility if your SSH connection drops.
Key takeaways
The command-line foundation for Linux networking and remote administration is now in place.
The key commands are:
Command
Essential role
ip a
Show network interfaces and addresses
ip route
Show routing table
ifconfig
Older interface inspection command
iwconfig
Older wireless inspection command
ifup
Bring interface up using traditional configuration
ifdown
Bring interface down using traditional configuration
ping
Test basic reachability
hostname
Show or set system name
hostnamectl
Modern hostname and system identity tool
wget
Download files from the terminal
curl
Test URLs, APIs and HTTP headers
ssh
Secure remote login
scp
Simple file copy over SSH
rsync
Efficient file synchronization
ss
Inspect listening ports and sockets
lsof -i
Find processes using network ports
getent hosts
Test system name resolution
ufw
Manage simple firewall rules on systems using UFW
The deeper lesson is that networking must be diagnosed in layers.
First, check whether the machine has an interface and IP address. Then check whether it has a route. Then check whether the gateway responds. Then test external IP connectivity. Then test DNS. Then check whether the local service is listening. Then check whether remote clients can reach it. Then inspect firewall rules, logs and service configuration.
Remote administration adds another layer of responsibility. With SSH, scp and rsync, you can manage systems from anywhere. That power requires careful habits: verify the host, verify the user, preview destructive actions, keep backups, use screen for long tasks and test before restarting critical services.
A Linux system becomes much easier to manage when you understand how files, services, processes and networks connect. The terminal gives you visibility into every layer.
Practical workflow: check open network ports after deployment
After deploying a service, check listening ports:
sudo ss -tulpn
Filter expected port:
sudo ss -tulpn | grep ':8080'
Check process:
sudo lsof -i :8080
Check local response:
curl -I http://localhost:8080
Check service status:
systemctl status service-name
Check logs:
journalctl -u service-name -n 100
If the service listens on 127.0.0.1:8080, it is local-only. If it listens on 0.0.0.0:8080, it accepts connections on all IPv4 interfaces.
Restoration should be tested in a safe directory before touching production paths.
Automation, shell scripting and scheduled tasks
Automation turns tested command sequences into repeatable tools. History, cron, scripts, aliases, loops and timers help convert one-time work into reliable procedures.
screen: keeping terminal sessions alive
The command screen creates a virtual terminal session that can continue running even if you disconnect.
This is much safer than running critical long tasks in a normal SSH session.
Command history
The shell stores commands you have typed. The history command displays them.
history
Show the last 20 commands:
history | tail -n 20
Search history:
history | grep apt
Run the previous command again:
!!
A very common use:
apt update
If you forgot sudo, run:
sudo !!
This becomes:
sudo apt update
Run a command by history number:
!123
Be careful with this because history numbers change as new commands are added.
Delete one history entry:
history -d 123
Clear history:
history -c
History is useful for productivity, auditing your own work and repeating complex commands. It is also sensitive because commands may contain paths, server names or accidentally typed secrets.
Avoid typing passwords directly into commands because they may be stored in shell history.
Command history is your memory in the terminal
The command history shows commands previously typed in the shell.
This is useful because many real commands are long. Instead of retyping them, you can find and reuse them.
The shell also supports command repetition shortcuts.
Shortcut
Meaning
!!
Run the previous command again
sudo !!
Run the previous command again with sudo
!123
Run command number 123 from history
!grep
Run the most recent command beginning with grep
!?error
Run the most recent command containing error
The most famous example is forgetting sudo.
You type:
apt update
The system refuses because administrative privileges are required. Then you run:
sudo !!
The shell expands it to:
sudo apt update
This is convenient, but it must be used carefully. Repeating commands blindly can be dangerous, especially after deletion, permission or remote commands.
Before using history expansion on destructive commands, inspect the command first.
history | tail -n 10
Then decide whether it is safe to repeat.
History can also be a security risk
Shell history is useful, but it can accidentally store sensitive information.
Bad practice:
mysql -u root -pMySecretPassword
This places the password directly in shell history.
Better practice:
mysql -u root -p
This prompts for the password without storing it in the command line.
If sensitive data was typed accidentally, you can delete a specific history line.
First find it:
history | grep secret
Then delete by number:
history -d 1234
Clear all history:
history -c
However, clearing history does not solve every security problem. The command may already have been written to disk, captured by terminal logging, stored in process lists or recorded elsewhere. The better habit is to avoid placing secrets directly in command lines.
Sensitive values should be entered through prompts, protected files, environment mechanisms or secret management systems appropriate to the environment.
crontab schedules commands automatically
The command crontab manages scheduled tasks for a user.
List current cron jobs:
crontab -l
Edit cron jobs:
crontab -e
A cron line has five time fields followed by a command.
Cron runs commands in a limited environment. This is very important. A command that works in your interactive shell may fail in cron because environment variables, working directory or PATH are different.
This is much safer than running long maintenance in an ordinary SSH session.
Basic shell scripting turns workflows into tools
A shell script is a file containing commands.
Create a script:
nano hello.sh
Content:
#!/bin/bash
echo "Hello from Linux"
Make it executable:
chmod +x hello.sh
Run it:
./hello.sh
The first line:
#!/bin/bash
is called a shebang. It tells Linux which interpreter should run the file.
Without execute permission, this may fail:
Permission denied
Fix:
chmod +x hello.sh
A script does not need to be executable if you run it explicitly with Bash:
bash hello.sh
But executable scripts are more convenient.
Script safety basics
At the beginning of many scripts, you may see:
set -e
This tells the script to stop if a command fails.
A stricter pattern:
set -euo pipefail
Meaning:
Option
Meaning
-e
Exit if a command fails
-u
Treat unset variables as errors
-o pipefail
Fail pipeline if any command in it fails
Example:
#!/bin/bash
set -euo pipefail
SOURCE="/var/www/site"
DESTINATION="/home/pat/backups"
mkdir -p "$DESTINATION"
tar -czf "$DESTINATION/site.tar.gz" "$SOURCE"
echo "Backup completed"
This makes scripts safer, but it also requires more careful writing. A command that returns a nonzero status intentionally may stop the script unless handled.
For beginner scripts, start simple. As scripts become important, add safety options and logging.
Exit status explains success and failure
Every Linux command returns an exit status.
Check the last command’s exit status:
echo $?
Usually:
Exit status
Meaning
0
Success
Nonzero
Failure or special condition
Example:
ls /home
echo $?
If successful, output is likely:
0
Now try a missing path:
ls /path/that/does/not/exist
echo $?
The exit status will be nonzero.
This is essential in scripts.
if tar -tzf backup.tar.gz > /dev/null 2>&1; then
echo "Archive is readable"
else
echo "Archive verification failed"
fi
The if statement checks the command’s exit status.
if statements in shell scripts
A basic if statement:
if command; then
echo "Command succeeded"
else
echo "Command failed"
fi
Check whether a file exists:
if [ -f "/etc/hosts" ]; then
echo "File exists"
else
echo "File does not exist"
fi
Check whether a directory exists:
if [ -d "/var/www/site" ]; then
echo "Directory exists"
else
echo "Directory does not exist"
fi
Common test operators:
Test
Meaning
-f file
Regular file exists
-d directory
Directory exists
-e path
Path exists
-r file
File is readable
-w file
File is writable
-x file
File is executable
-s file
File exists and is not empty
Example backup check:
if [ ! -d "$SOURCE" ]; then
echo "Source directory does not exist: $SOURCE"
exit 1
fi
The ! means not.
for loops repeat actions
A for loop runs commands for multiple values.
for file in *.log; do
echo "$file"
done
Compress old-style log files:
for file in *.log; do
gzip "$file"
done
Loop through servers:
for server in web1 web2 web3; do
echo "Checking $server"
ssh "$server" "hostname; uptime; df -h /"
done
Loop through users:
for user in alice bob charlie; do
echo "User: $user"
done
Always quote variables unless you intentionally want word splitting.
Better:
echo "$file"
Riskier:
echo $file
For loops are the foundation of simple automation.
while loops process input line by line
A while loop can read a file line by line.
while read -r line; do
echo "$line"
done < file.txt
The -r option prevents backslash interpretation.
Read server names from a file:
while read -r server; do
echo "Checking $server"
ssh "$server" "uptime"
done < servers.txt
If servers.txt contains:
web1
web2
web3
the script checks each server.
This is a simple but powerful administration pattern.
aliases make frequent commands shorter
An alias creates a shortcut.
alias ll='ls -lah'
Now you can run:
ll
Show aliases:
alias
Remove an alias:
unalias ll
To make aliases permanent, add them to your shell configuration file, often:
~/.bashrc
Edit:
nano ~/.bashrc
Add:
alias ll='ls -lah'
alias gs='git status'
alias update='sudo apt update && sudo apt upgrade'
Reload:
source ~/.bashrc
Aliases are useful, but do not make destructive aliases too casual. For example, aliasing rm to rm -rf would be dangerous. A safer alias is:
alias rm='rm -i'
This asks before deletion, but it can also affect scripts or expectations in interactive use. Use aliases thoughtfully.
Practical workflow: create a reusable maintenance script
Create script:
nano ~/scripts/server-check.sh
Script content:
#!/bin/bash
set -euo pipefail
echo "Hostname:"
hostname
echo
echo "Uptime:"
uptime
echo
echo "Disk usage:"
df -h
echo
echo "Failed services:"
systemctl --failed || true
echo
echo "Top memory processes:"
ps aux --sort=-%mem | head -10
echo
echo "Top CPU processes:"
ps aux --sort=-%cpu | head -10
Make executable:
chmod +x ~/scripts/server-check.sh
Run:
~/scripts/server-check.sh
Why || true after systemctl --failed?
Some commands can return nonzero status even when the output is still useful. Since the script uses set -e, a nonzero status could stop the script. The || true prevents that specific command from stopping the script.
This script combines system identity, uptime, disk usage, failed services and top processes into one repeatable check.
Practical workflow: remote health check for multiple servers
Check disk:
df -h
Largest folders:
du -h --max-depth=1 /var | sort -h
Find large files:
find /var -type f -size +100M -exec ls -lh {} \;
Check failed services:
systemctl --failed
Follow Nginx logs:
journalctl -u nginx -f
This becomes your personal operational knowledge base.
You can search it:
grep -i "disk" ~/linux-notes/useful-commands.md
You can version it with Git later if needed.
Key takeaways
The focus now shifts from individual commands to workflows, scripts and scheduled automation.
The key commands and concepts are:
Tool or concept
Essential role
history
Reuse and inspect previous commands
!!
Repeat the previous command
!123
Run a command from history by number
Wildcards
Match groups of files
Quotes
Control shell interpretation
Variables
Store reusable values
$(command)
Insert command output into another command
`
`
>
Redirect output and overwrite
>>
Redirect output and append
2>
Redirect errors
2>&1
Merge errors with output
tee
Display and save output
xargs
Convert input into command arguments
wc
Count lines, words, characters and bytes
cut
Extract simple fields
awk
Process structured text and apply logic
sed
Edit and transform text streams
sort
Sort lines
uniq
Remove or count repeated adjacent lines
crontab
Schedule commands
screen
Keep terminal sessions alive
Shell scripts
Save workflows as reusable tools
Exit status
Detect success or failure
if
Add decisions to scripts
for
Repeat actions over a list
while read
Process files line by line
watch
Repeat a command visually
lsof
Identify open files and port usage
dmesg
Inspect kernel and hardware messages
The deeper lesson is that Linux is built around text streams. Logs are text. Configuration files are text. Command output is text. Scripts are text. Reports can be generated from text. Automation is often just carefully structured text moving through commands.
Once you understand pipes, redirection, quoting, variables, text processing and cron, the terminal stops being a manual interface only. It becomes a system for building your own tools.
Exit status tells whether a command succeeded
Every Linux command returns an exit status. This is a small numeric result that tells the shell whether the command succeeded or failed.
Check the exit status of the previous command:
echo $?
The general convention is:
Exit status
Meaning
0
Success
Nonzero
Failure, warning or special condition
Example:
ls /home
echo $?
If /home exists and is readable, the exit status is likely:
0
Now try:
ls /directory/that/does/not/exist
echo $?
The exit status will be nonzero.
This is important in scripts because scripts use exit status to decide what to do next.
Example:
if tar -tzf backup.tar.gz > /dev/null 2>&1; then
echo "Archive is valid"
else
echo "Archive is damaged or unreadable"
fi
The script does not need to parse text output. It checks whether tar succeeded.
This is a clean Linux pattern: let commands report success or failure through exit status.
This script records maintenance output. It is still interactive because apt upgrade may ask for confirmation. That is often desirable for manual maintenance.
For fully automated updates, more policy decisions are needed. Automation should not blindly upgrade critical production systems without testing and rollback planning.
Practical workflow: use aliases for safer inspection
Useful aliases can make inspection faster.
Open:
nano ~/.bashrc
Add:
alias ll='ls -lah'
alias la='ls -la'
alias lt='ls -lahtr'
alias disk='df -h'
alias ports='sudo ss -tulpn'
alias failed='systemctl --failed'
Reload:
source ~/.bashrc
Now:
ll
disk
failed
ports
Aliases should make safe inspection easier. Avoid aliases that hide dangerous behavior.
A dangerous alias would be:
alias cleanup='rm -rf *'
A safer alias:
alias rm='rm -i'
Even this should be used thoughtfully. Some experienced users prefer not to alias core commands because it can create surprises on other systems.
Keep an existing SSH session open while testing a new one.
Network troubleshooting as layered thinking
Network troubleshooting should be done in layers. Each layer answers a different question.
Layer
Question
Commands
Interface
Does the machine have a network interface and IP?
ip a
Link
Is the interface up?
ip link, ip a
Route
Does traffic know where to go?
ip route
Gateway
Can the local router be reached?
ping gateway
Internet IP
Can external IPs be reached?
ping 8.8.8.8
DNS
Can names be resolved?
getent hosts domain, ping domain
Local service
Is the service listening?
ss -tulpn, lsof -i :port
Remote service
Can another host reach the port?
curl, nc, browser
Logs
What does the service report?
journalctl, /var/log
This method avoids confusion. For example, if DNS fails, restarting the web server will not help. If the service is not listening, changing DNS will not help. If the interface has no IP, editing application configuration will not help.
A strong Linux user does not guess the layer. They test it.
Practical workflow: understand command failure with set -x
In shell scripts, set -x prints commands as they are executed. This is useful for debugging.
Example:
#!/bin/bash
set -x
SOURCE="/var/www/site"
DESTINATION="/home/pat/backups"
mkdir -p "$DESTINATION"
tar -czf "$DESTINATION/site.tar.gz" "$SOURCE"
Run the script and observe each expanded command.
You can enable debugging only for part of a script.
set -x
tar -czf "$ARCHIVE" "$SOURCE"
set +x
Do not use set -x around secrets because it may print sensitive values.
Troubleshooting Linux is a method, not a panic reaction
Linux troubleshooting is not about randomly restarting services, deleting files, changing permissions or copying commands from forums. Good troubleshooting is a method. It begins with observation, continues with narrowing the problem, then moves to controlled action and ends with verification.
A beginner often reacts to an error by immediately looking for a command that “fixes” it. An experienced Linux user first asks what layer is failing.
Is the problem in the filesystem?
Is the disk full?
Is the service stopped?
Is the process running but not responding?
Is the network interface down?
Is DNS failing?
Is a firewall blocking access?
Is a configuration file invalid?
Is a permission missing?
Is the command being run as the wrong user?
Is the script using the wrong working directory?
Is cron running with a different environment?
The terminal gives you tools to answer all of these questions. The real skill is choosing the right question first.
A professional Linux troubleshooting process usually follows this structure.
Stage
Purpose
Typical commands
Identify
Confirm what system, user and location you are working with
hostname, whoami, pwd, id
Observe
Check visible system state
uptime, df -h, free -h, systemctl --failed
Inspect
Read logs, files and service status
tail, grep, journalctl, systemctl status
Narrow down
Test one layer at a time
ip a, ip route, ping, ss, lsof, find
Act
Make the smallest necessary change
nano, systemctl reload, chmod, chown, apt install
Verify
Confirm the result after action
systemctl status, curl, df -h, tail, grep
Document
Save what was done
history, tee, notes, scripts
The most dangerous troubleshooting pattern is this:
This may appear to solve a symptom temporarily, but it creates security problems, removes evidence and may damage the system.
A better approach is:
hostname
whoami
pwd
df -h
systemctl status apache2
journalctl -u apache2 -n 100
ls -ld /var/www
ls -lah /var/www
This sequence gathers facts before making changes.
The deepest Linux troubleshooting principle is simple: do not destroy information before you understand it. Logs, permissions, timestamps, process states and configuration files are evidence. If you delete or overwrite them too early, you make the real cause harder to find.
Read the error message before searching for the solution
Many Linux errors already contain the answer, but beginners often ignore them because they look technical.
Consider this error:
Permission denied
This usually points to permissions, ownership, execution rights or user identity.
Start with:
whoami
pwd
ls -ld .
ls -l target-file
Another error:
No such file or directory
This does not always mean the file never existed. It may mean the path is wrong, the current directory is different, the filename case is wrong, a script interpreter path is invalid or a symbolic link points to a missing target.
Start with:
pwd
ls -lah
ls -l /full/path/to/file
Another error:
command not found
This may mean the package is not installed, the command name is wrong or the command is not in your PATH.
Start with:
type command-name
command -v command-name
echo "$PATH"
Another error:
Address already in use
This means a service tried to bind to a port that is already occupied.
Start with:
sudo ss -tulpn | grep ':80'
sudo lsof -i :80
Another error:
No space left on device
This may mean the filesystem is full, inodes are exhausted or a deleted file is still held open.
the shell searches directories in PATH until it finds an executable named ls.
Check where ls is found:
command -v ls
Example:
/usr/bin/ls
If your own script is in:
/home/pat/scripts
and that directory is not in PATH, you cannot run the script by name alone unless you use the full path or ./.
Run by full path:
/home/pat/scripts/backup.sh
Run from inside the script directory:
./backup.sh
Add a directory to PATH temporarily:
export PATH="$PATH:/home/pat/scripts"
To make it persistent, add it to ~/.bashrc:
nano ~/.bashrc
Add:
export PATH="$PATH:/home/pat/scripts"
Reload:
source ~/.bashrc
Be careful with PATH. Do not add untrusted writable directories to the beginning of PATH, especially on shared systems. If a malicious executable appears there with the same name as a common command, it may be run accidentally.
Environment variables shape command behavior
Environment variables are named values available to programs.
Cron failures are often environment failures, not command failures.
Practical workflow: investigate disk full correctly
When a disk is full, start with filesystems.
df -h
Also check inodes:
df -i
A filesystem can be full because of data blocks or because it has run out of inodes. Inodes represent filesystem entries. A huge number of tiny files can exhaust inodes even when space remains.
If normal space is full, inspect directories:
sudo du -h --max-depth=1 / | sort -h
If /var is large:
sudo du -h --max-depth=1 /var | sort -h
If logs are large:
sudo du -h --max-depth=1 /var/log | sort -h
Find large files:
sudo find /var -type f -size +500M -exec ls -lh {} \;
The logs usually contain the strongest clue. Read them before editing configuration.
Practical workflow: investigate why a script behaves differently under sudo
A script may work as a normal user but behave differently under sudo.
Check user:
whoami
sudo whoami
Check environment:
env | sort
sudo env | sort
Check home:
echo "$HOME"
sudo sh -c 'echo "$HOME"'
When using sudo, environment variables may be reset or changed. The working directory may remain the same, but home and PATH may differ depending on sudo policy.
If you did not make a backup, check package default examples or reinstall package configuration carefully. Do not immediately purge the package unless you understand consequences.
If using version control for /etc, restore from Git. Some administrators manage /etc with tools such as etckeeper, but even without that, the habit is clear: configuration should be backed up before editing.
Use quotes around complex watched commands. Without quotes, pipes may be interpreted outside watch.
Practical workflow: stop guessing with a checklist
When a Linux service fails, use a checklist.
hostname
whoami
uptime
df -h
systemctl status service-name
journalctl -u service-name -n 100
sudo ss -tulpn
ps aux | grep service-name
If configuration was changed:
find /etc -type f -mmin -60 -exec ls -lh {} \;
If files were deployed:
find /var/www/site -type f -mmin -60 -exec ls -lh {} \;
If permissions may be wrong:
namei -l /path/to/problem/file
If disk may be full:
df -h
df -i
This checklist does not fix everything. It prevents blind troubleshooting.
Practical workflow: document a repair session
Start a log file:
LOG="repair-$(date +%Y-%m-%d-%H-%M).log"
Record basic state:
{
date
hostname
whoami
uptime
df -h
systemctl --failed
} | tee "$LOG"
Append commands manually as you work:
echo "Checked nginx status" | tee -a "$LOG"
systemctl status nginx 2>&1 | tee -a "$LOG"
Append logs:
journalctl -u nginx -n 100 2>&1 | tee -a "$LOG"
At the end:
echo "Repair finished at $(date)" | tee -a "$LOG"
This creates a trace of what you observed and changed. On professional systems, this is valuable for post-incident review.
Practical workflow: safely run a risky command
A risky command is any command that deletes, overwrites, recursively changes permissions, changes ownership, restarts critical services or synchronizes with deletion.
Practical workflow: find configuration files for a service
If you do not know where a service stores configuration, use several methods.
Check package files:
dpkg -L nginx | grep conf
Check common directories:
find /etc -iname "*nginx*"
Check service definition:
systemctl cat nginx
Check process command line:
ps aux | grep nginx
Check manual:
man nginx
The command:
systemctl cat service-name
shows the systemd unit file content and can reveal startup commands and environment files.
This connects package data, filesystem search, service management and process inspection.
Practical workflow: detect recently failed commands in a script
Use set -e to stop on failure and add logging.
Example:
#!/bin/bash
set -euo pipefail
LOG="/home/pat/logs/script.log"
mkdir -p "$(dirname "$LOG")"
echo "$(date +"%Y-%m-%d %H:%M:%S") Script started" | tee -a "$LOG"
echo "Running backup" | tee -a "$LOG"
tar -czf /home/pat/backups/site.tar.gz /var/www/site 2>&1 | tee -a "$LOG"
echo "$(date +"%Y-%m-%d %H:%M:%S") Script finished" | tee -a "$LOG"
If the script fails, the log shows the last successful step.
For deeper debugging, temporarily add:
set -x
But avoid set -x around secrets.
Practical workflow: create a command review habit
Before pressing Enter on a serious command, read it aloud logically.
Example:
sudo find /var/log/myapp -type f -name "*.gz" -mtime +30 -delete
Read it as:
Find inside /var/log/myapp, only regular files, named *.gz, older than 30 days, and delete them.
If any part is not what you intend, do not run it.
Synchronize contents of local public into remote /var/www/site, preserve metadata, compress transfer, and delete files on destination that are not in source.
That last part is critical. If you are not sure, use:
if grep -qi "error" "$LOG_FILE"; then
grep -i "error" "$LOG_FILE" | wc -l
else
echo 0
fi
This is longer but explicit.
In scripts, clarity is better than cleverness.
Real-world Linux administration workflows
Real administration depends on repeatable workflows. Maintenance, backups, web server checks, SSH changes, incident response and server preparation all benefit from structured command sequences.
Linux administration is built from repeatable workflows
A Linux command becomes truly valuable when it becomes part of a repeatable workflow. Knowing that df -h shows disk space is useful. Knowing when to run it, how to interpret it, how to combine it with du, how to investigate a full filesystem and how to document the result is professional administration.
The same applies to almost every Linux command. A command is the smallest unit. A workflow is the real skill.
A workflow has a beginning, a purpose, a safe sequence and a verification step.
For example, this is only a command:
df -h
This is a workflow:
hostname
uptime
df -h
sudo du -h --max-depth=1 /var | sort -h
sudo find /var/log -type f -size +100M -exec ls -lh {} \;
sudo lsof | grep deleted
The workflow answers several connected questions.
Question
Command
Which machine am I on?
hostname
How long has it been running?
uptime
Which filesystem is full?
df -h
Which folder is using space?
du -h --max-depth=1
Which large files exist?
find -size +100M
Is deleted data still held open?
`lsof
This is the difference between command memory and operational understanding.
A Linux system is predictable when you approach it in layers. Files belong to users and groups. Services create processes. Processes open files and ports. Logs record events. Package managers install software. Cron schedules commands. SSH connects systems. The shell connects all these pieces.
Professional Linux work is not based on improvisation. It is based on repeatable inspection, careful change and verification.
A daily Linux server check
A daily server check does not need to be complicated. The purpose is to quickly detect obvious problems before they become serious incidents.
A practical daily check may include identity, uptime, disk space, memory, failed services, listening ports and recent critical logs.
~/scripts/daily-server-check.sh 2>&1 | tee ~/daily-server-check.txt
This is a simple but serious administrative habit. It creates awareness of the machine before problems become urgent.
A weekly Linux maintenance workflow
Weekly maintenance is deeper than a daily check. It should review updates, disk usage, logs, backups, services and security-sensitive access.
A safe weekly maintenance workflow begins with observation.
hostname
who
uptime
df -h
free -h
systemctl --failed
Then check package updates.
sudo apt update
apt list --upgradable
Then upgrade if appropriate.
sudo apt upgrade
sudo apt autoremove
Then verify service state.
systemctl --failed
journalctl -p err -n 100
Then check disk usage more deeply.
sudo du -h --max-depth=1 /var | sort -h
sudo du -h --max-depth=1 /home | sort -h
Then check large logs.
sudo find /var/log -type f -size +100M -exec ls -lh {} \;
Then verify backup location.
ls -lah /home/pat/backups
find /home/pat/backups -type f -mtime -7 -exec ls -lh {} \;
A weekly maintenance table may look like this.
Area
Command
Purpose
Users
who
See who is logged in
Runtime
uptime
Check load and uptime
Disk
df -h
Detect full filesystems
Memory
free -h
Check RAM and swap
Services
systemctl --failed
Detect failed services
Updates
apt list --upgradable
Review available updates
Logs
journalctl -p err -n 100
Review recent errors
Large files
find /var/log -size +100M
Detect oversized logs
Backups
find backups -mtime -7
Confirm recent backups exist
The important point is that maintenance must be repeatable. A checklist prevents omissions.
Safe package update policy
Updating Linux packages is important, but updates should still be done thoughtfully. A personal laptop, a lab Raspberry Pi and a production web server do not have the same risk profile.
For a personal machine, this may be acceptable:
sudo apt update
sudo apt upgrade
For a production server, it is better to inspect first.
The correct update policy depends on the environment.
Environment
Recommended attitude
Personal computer
Regular updates are usually straightforward
Development VM
Update often, snapshots are useful
Raspberry Pi project
Update carefully if it controls hardware
Small web server
Inspect before and after updates
Production server
Use maintenance windows and rollback planning
Critical infrastructure
Test updates before production deployment
The package manager is safe compared with random manual installation, but any system change can have side effects. Good administrators verify before and after.
Building a backup strategy with Linux commands
A backup is not a backup until it can be restored. Many users create archives. Fewer users verify them. Even fewer test restoration.
A serious Linux backup strategy answers five questions.
Question
Why it matters
What is being backed up?
Avoid missing important data
Where is the backup stored?
Avoid storing backup only on the same failing disk
How often is it created?
Define acceptable data loss
How is it verified?
Detect broken archives
How is it restored?
Ensure recovery is possible
A basic local backup command:
tar -czf /home/pat/backups/site-$(date +%Y-%m-%d).tar.gz /var/www/site
This creates a compressed archive of /var/www/site.
But a better workflow creates the destination, logs the process and verifies the archive.
mkdir -p /home/pat/backups
tar -czf /home/pat/backups/site-$(date +%Y-%m-%d).tar.gz /var/www/site
tar -tzf /home/pat/backups/site-$(date +%Y-%m-%d).tar.gz > /dev/null
ls -lh /home/pat/backups/site-$(date +%Y-%m-%d).tar.gz
This confirms the archive can be listed.
A better backup script:
#!/bin/bash
set -euo pipefail
SOURCE="/var/www/site"
BACKUP_DIR="/home/pat/backups"
LOG_DIR="/home/pat/logs"
DATE=$(date +%Y-%m-%d-%H-%M)
ARCHIVE="$BACKUP_DIR/site-$DATE.tar.gz"
LOG="$LOG_DIR/site-backup-$DATE.log"
mkdir -p "$BACKUP_DIR"
mkdir -p "$LOG_DIR"
echo "$(date +"%Y-%m-%d %H:%M:%S") Backup started" | tee -a "$LOG"
echo "Source: $SOURCE" | tee -a "$LOG"
echo "Archive: $ARCHIVE" | tee -a "$LOG"
tar -czf "$ARCHIVE" "$SOURCE" 2>&1 | tee -a "$LOG"
if tar -tzf "$ARCHIVE" > /dev/null 2>&1; then
echo "$(date +"%Y-%m-%d %H:%M:%S") Backup verified" | tee -a "$LOG"
else
echo "$(date +"%Y-%m-%d %H:%M:%S") Backup verification failed" | tee -a "$LOG"
exit 1
fi
ls -lh "$ARCHIVE" | tee -a "$LOG"
echo "$(date +"%Y-%m-%d %H:%M:%S") Backup finished" | tee -a "$LOG"
This script is still simple, but it includes destination creation, timestamping, logging and verification.
Local backups and remote backups are different
A local backup protects against accidental deletion, failed configuration changes or application-level mistakes. It does not protect well against disk failure, server destruction, theft or ransomware.
Use --delete only when you really want destination files removed if they no longer exist in the source.
Backup strategy comparison:
Backup type
Command example
Protects against
Weakness
Local archive
tar -czf backup.tar.gz source
Accidental file changes
Same-disk failure
Local sync
rsync -av source/ backup/
Fast local copy
Same-machine risk
Remote sync
rsync -avz source/ server:/backup/
Server data loss
Needs remote access
Remote archive
tar plus scp or rsync
Portable restore point
Larger transfers
Snapshot
Filesystem or provider tool
Fast rollback
Platform-specific
The best backup plan often combines more than one method.
Testing restoration
A backup must be tested. The simplest test is listing archive contents.
tar -tzf backup.tar.gz | head -30
A stronger test is extraction into a temporary directory.
mkdir -p /tmp/restore-test
tar -xzf backup.tar.gz -C /tmp/restore-test
tree -L 2 /tmp/restore-test
Check size:
du -sh /tmp/restore-test
If the archive contains a project, inspect key files.
find /tmp/restore-test -maxdepth 3 -type f | head -50
For a website backup, you may need to verify:
Component
Verification
Web files
Archive contains expected project files
Uploads
User-uploaded data exists
Configuration
Required config files exist
Permissions
Ownership and permissions can be restored
Database
Database dump exists and can be imported
Cron jobs
Scheduled tasks are documented
Services
Required packages and service names are known
A file backup alone may not be enough. Many applications need both files and databases.
A typical website backup may require:
tar -czf site-files.tar.gz /var/www/site
mysqldump database_name > database.sql
tar -czf full-site-backup.tar.gz site-files.tar.gz database.sql
Database backup commands depend on the database system and authentication setup, but the principle is universal: back up both application files and application data.
Retention: how many backups should be kept
If backups are never removed, they eventually fill the disk. If too few backups are kept, recovery options are limited.
A simple retention policy might keep daily backups for 7 days.
Find backups older than 7 days:
find /home/pat/backups -type f -name "site-*.tar.gz" -mtime +7 -exec ls -lh {} \;
Delete only after verifying:
find /home/pat/backups -type f -name "site-*.tar.gz" -mtime +7 -delete
A safer cleanup script logs what it removes.
#!/bin/bash
set -euo pipefail
BACKUP_DIR="/home/pat/backups"
LOG="/home/pat/logs/backup-cleanup.log"
mkdir -p "$(dirname "$LOG")"
echo "$(date +"%Y-%m-%d %H:%M:%S") Backup cleanup started" | tee -a "$LOG"
find "$BACKUP_DIR" -type f -name "site-*.tar.gz" -mtime +7 -exec ls -lh {} \; | tee -a "$LOG"
find "$BACKUP_DIR" -type f -name "site-*.tar.gz" -mtime +7 -delete
echo "$(date +"%Y-%m-%d %H:%M:%S") Backup cleanup finished" | tee -a "$LOG"
Logs are the memory of a Linux system. They explain what happened, when it happened and often why it happened. But logs can also grow until they fill a disk.
The most important log locations on many Linux systems are:
Location
Purpose
/var/log/syslog
General system logs on many Debian-based systems
/var/log/auth.log
Authentication events
/var/log/kern.log
Kernel messages on some systems
/var/log/apache2/
Apache logs
/var/log/nginx/
Nginx logs
/var/log/mysql/
MySQL or MariaDB logs on some systems
journalctl
systemd journal access
/var/log/apt/
Package management logs
Basic log inspection:
tail -n 100 /var/log/syslog
Follow live:
sudo tail -f /var/log/syslog
Search errors:
grep -i "error" /var/log/syslog
Read systemd journal:
journalctl -n 100
Show errors only:
journalctl -p err -n 100
Follow journal live:
journalctl -f
Show logs for one service:
journalctl -u nginx -n 100
Follow service logs:
journalctl -u nginx -f
Logs are useful only when you know how to filter them. A system may produce thousands of lines. The goal is not to read everything. The goal is to locate relevant evidence.
This workflow changes logs from noise into structured information.
Log rotation prevents uncontrolled growth
Log rotation means old logs are renamed, compressed and eventually removed according to policy. On many Linux systems, logrotate manages this.
Main configuration:
/etc/logrotate.conf
Service-specific configurations:
/etc/logrotate.d/
List configurations:
ls -lah /etc/logrotate.d/
Inspect one:
cat /etc/logrotate.d/nginx
Debug logrotate without changing anything:
sudo logrotate -d /etc/logrotate.conf
Force logrotate:
sudo logrotate -f /etc/logrotate.conf
Use force carefully. Debug mode is safer for inspection.
A typical logrotate configuration may define:
Directive
Meaning
daily
Rotate daily
weekly
Rotate weekly
rotate 7
Keep seven rotated logs
compress
Compress old logs
missingok
Do not fail if log is missing
notifempty
Do not rotate empty logs
create
Create a new log file after rotation
postrotate
Run commands after rotation
If logs grow too large, do not only delete them. Ask why rotation did not manage them.
Possible causes:
Cause
Explanation
Application writes outside expected log path
logrotate config does not cover it
Rotation frequency is too low
Logs grow faster than policy
Compression disabled
Old logs use more space
Service keeps old file handle
Service may need reload after rotation
Application log level too verbose
Too much unnecessary logging
Error loop
Application repeatedly logs the same failure
Log growth is often a symptom, not the root cause.
Truncating active logs safely
If a log file is huge and active, deleting it may not immediately free space if the process still holds the file open. Truncating keeps the same file but reduces its size.
ls -ld /var/www
ls -lah /var/www/site
namei -l /var/www/site/index.html
A web server problem is rarely “just one thing.” It may be a service problem, port problem, permission problem, configuration problem, DNS problem, firewall problem or application problem.
A complete web server down workflow
If a website is down, avoid random restarts. Use a layered workflow.
Check system identity and load.
hostname
uptime
df -h
free -h
Check service.
For Nginx:
systemctl status nginx
For Apache:
systemctl status apache2
Check configuration.
sudo nginx -t
or:
sudo apache2ctl configtest
Check ports.
sudo ss -tulpn | grep ':80'
sudo ss -tulpn | grep ':443'
Check local HTTP response.
curl -I http://localhost
Check logs.
journalctl -u nginx -n 100
or:
journalctl -u apache2 -n 100
Check web root.
ls -lah /var/www
find /var/www -maxdepth 3 -type f -mmin -60 -exec ls -lh {} \;
Check permissions.
namei -l /var/www/site/index.html
Only after diagnosis, restart or reload.
sudo systemctl reload nginx
or:
sudo systemctl restart nginx
Then verify.
systemctl status nginx
curl -I http://localhost
sudo ss -tulpn | grep ':80'
The workflow moves from system health to service state, configuration, ports, response, logs, files and permissions.
Web deployment with rsync
A simple deployment often means copying local build files to a remote web root.
Deployment should be previewed because --delete can remove destination files.
A safer deployment script:
#!/bin/bash
set -euo pipefail
SOURCE="./public/"
DESTINATION="deploy@server:/var/www/site/"
echo "Previewing deployment"
rsync -avz --delete --dry-run "$SOURCE" "$DESTINATION"
echo "If the preview is correct, run:"
echo "rsync -avz --delete \"$SOURCE\" \"$DESTINATION\""
For real automation, you can add a confirmation prompt.
read -r -p "Deploy now? Type yes to continue: " ANSWER
if [ "$ANSWER" = "yes" ]; then
rsync -avz --delete "$SOURCE" "$DESTINATION"
else
echo "Deployment cancelled"
fi
This prevents accidental deployment.
Permissions for web projects
Web permissions are one of the most common Linux problems. Too strict, and the application cannot read or write. Too open, and the server becomes insecure.
A common baseline for static files:
sudo find /var/www/site -type d -exec chmod 755 {} \;
sudo find /var/www/site -type f -exec chmod 644 {} \;
This means directories are enterable and listable, files are readable, and only owners can write.
For upload or cache directories, the web server may need write access.
systemctl --type=service --state=running
sudo ss -tulpn
Disable services that are not needed.
sudo systemctl disable --now service-name
Check failed services.
systemctl --failed
Review logs.
journalctl -p warning -n 100
Set up backups.
mkdir -p ~/backups
Create and test a backup script.
Security is not one command. It is a collection of habits: least privilege, updates, key-based access, firewall rules, minimal services, logs and backups.
Practical incident workflow
When something breaks, use a structured incident workflow.
First snapshot:
date
hostname
whoami
uptime
df -h
free -h
systemctl --failed
Then define the symptom.
Symptom
First commands
Website down
systemctl status nginx, curl -I localhost, ss -tulpn
SSH failing
systemctl status ssh, `ss -tulpn
Disk full
df -h, du, find, lsof deleted
High CPU
uptime, htop, ps --sort=-%cpu
High memory
free -h, ps --sort=-%mem, htop
Package failure
apt update, apt logs, disk space
Permission error
whoami, id, ls -l, namei -l
Cron failure
crontab -l, logs, absolute paths, environment
Collect evidence:
journalctl -n 200
or service-specific:
journalctl -u service-name -n 200
Make one change at a time.
Verify after each change.
Document what worked.
This is slower than guessing at first, but much faster when problems are serious.
Building a personal Linux operations folder
A useful professional habit is keeping your own operational scripts and notes.
Create structure:
mkdir -p ~/ops/{scripts,logs,reports,notes}
Create notes:
nano ~/ops/notes/linux-commands.md
Add commonly used commands.
Disk check:
df -h
du -h --max-depth=1 /var | sort -h
Service check:
systemctl status nginx
journalctl -u nginx -n 100
Ports:
sudo ss -tulpn
Large files:
find /var -type f -size +100M -exec ls -lh {} \;
Failed SSH logins:
grep -i "failed password" /var/log/auth.log
This is not a complete hardening guide, but it creates a controlled starting point.
A complete learning roadmap for Linux commands
Linux should be learned in layers.
Stage
Focus
Commands
Orientation
Where am I and what is here?
pwd, cd, ls, tree
File basics
Create, copy, move, remove
mkdir, touch, cp, mv, rm
Reading files
Inspect text
cat, less, head, tail
Searching
Find text and files
grep, find
Permissions
Understand access
chmod, chown, id, namei
Packages
Manage software
apt, dpkg
Services
Manage daemons
systemctl, service, journalctl
Processes
Inspect running programs
ps, htop, kill
Disk
Manage space
df, du, mount, umount
Networking
Diagnose connectivity
ip, ping, ssh, ss, rsync
Text processing
Analyze logs
awk, sed, cut, wc, sort, uniq
Automation
Repeat work
cron, scripts, screen, watch
Troubleshooting
Think in layers
all of the above
Do not try to learn everything at once. Learn workflows. Use a practice directory. Build small scripts. Read logs. Break problems into layers.
Key takeaways
Linux command knowledge becomes operational practice when it is organized into repeatable checks, backups, deployment steps and incident workflows.
The central lessons are:
Lesson
Meaning
Commands become valuable inside workflows
One command is useful, a repeatable process is stronger
Maintenance should be structured
Check identity, load, disk, memory, services, logs and updates
Backups must be verified
An archive that cannot be restored is not a backup
Logs must be managed
Logs are evidence, but uncontrolled logs can fill disks
Web servers are layered systems
Files, permissions, services, ports, logs and configuration interact
SSH must be handled carefully
Remote access mistakes can lock you out
Firewalls require access planning
Always allow SSH before enabling remote firewall rules
Permissions should follow least privilege
Avoid 777 and fix only what needs fixing
Dangerous commands need previews
Use --dry-run, find -print, ls, backups and confirmation
Troubleshooting should be evidence-based
Inspect first, change second, verify third
Linux expertise is not measured by how many commands someone can type from memory. It is measured by how safely and accurately they can move from symptom to cause to solution.
A strong Linux user does not simply know grep, find, chmod, systemctl, rsync or tar. A strong Linux user knows when to use them, how to combine them, how to avoid damage and how to prove that the result is correct.
Conclusion: building lasting Linux command competence
Linux command competence is not built by memorizing hundreds of isolated commands. It is built by understanding how Linux represents files, users, processes, services, storage, network connections, permissions and logs, and then using commands to inspect those structures before changing them.
A strong Linux workflow always starts with context. Confirm the current directory, inspect the target, read the relevant status or log, make the smallest safe change and verify the result. This habit matters more than knowing a long list of options by memory.
The commands in this guide form a practical operating language. Navigation commands show where you are. File commands change data. Search commands reveal patterns. Permission commands explain access. Package commands manage software. System commands control services. Network commands connect machines. Automation commands turn repeated work into reliable procedures.
When these layers come together, the command line stops feeling like a collection of cryptic instructions. It becomes a precise, observable and repeatable way to understand Linux systems.
The best long-term approach is simple: practice in a safe directory, read manual pages, build small workflows, keep notes, test backups, document commands that worked, and troubleshoot from evidence rather than from panic. That is how Linux commands become durable system knowledge.
Questions that turn Linux commands into real working knowledge
What are Linux commands?
Linux commands are text-based instructions used to interact with a Linux operating system through a terminal and a shell. A command can list files, move between directories, install software, inspect running processes, read logs, check disk space, manage permissions, connect to remote servers or automate repeated tasks. Linux commands are important because they expose the operating system in a precise and repeatable way. Instead of relying on a graphical interface, a user can run a clear instruction, copy it into documentation, place it in a script or repeat it on another server. This is why Linux commands are essential for system administration, web hosting, DevOps, cybersecurity, software development, cloud infrastructure and troubleshooting.
Why are Linux commands still important today?
Linux commands remain important because Linux powers servers, cloud instances, containers, routers, embedded devices, development environments and many production systems that do not use a graphical desktop. In these environments, the terminal is often the main control interface. Commands are also faster to repeat, easier to document and more precise than graphical actions. A command such as systemctl status nginx clearly checks the state of a service, while tail -f /var/log/syslog lets an administrator observe system events in real time. Linux command-line knowledge is not outdated. It is one of the core skills behind modern infrastructure, automation, hosting and technical troubleshooting.
What is the difference between a terminal, a shell and a command?
A terminal is the interface where you type commands and see output. A shell is the command interpreter that reads what you type, expands variables or wildcards, handles pipes and redirection, and starts programs. A command is the actual instruction being executed. For example, in ls -lah /var/log, the terminal is where you enter the line, the shell interprets it, ls is the command, -lah are options and /var/log is the argument. This distinction matters because many behaviors users experience are caused by the shell, not by the command itself. Quoting, wildcard expansion, environment variables and pipes are shell features that shape how commands run.
What is the best way to start learning Linux commands?
The best way to learn Linux commands is to start with context before memorization. A beginner should first understand the current directory, absolute paths, relative paths, command options, arguments and permissions. Then it makes sense to learn safe inspection commands such as pwd, ls, cd, cat, less, head, tail, man and tree. These commands teach how to move through the system and read information without changing anything. After that, file operations such as cp, mv, mkdir, touch and rm become easier to understand. Linux should be learned as a set of practical workflows, not as a random list of commands.
What does pwd do in Linux?
The pwd command means print working directory. It shows the full path of the directory where the current shell session is located. This matters because many Linux commands operate relative to the current directory. If you run rm notes.txt, Linux looks for notes.txt in the current location. If you run cp report.txt backup/, both paths may be interpreted relative to where you are. Running pwd before editing, moving, copying or deleting files is a simple but professional safety habit. It prevents many beginner mistakes because it confirms the context before any action is taken.
What does cd do in Linux?
The cd command changes the current directory. It is one of the most frequently used Linux commands because it lets users move through the filesystem. For example, cd /var/log moves to the system log directory, while cd Documents moves into a directory named Documents relative to the current location. Running cd without arguments usually returns the user to their home directory. cd .. moves one level up, and cd – returns to the previous directory. Since cd changes the context for later commands, it should often be followed by pwd and ls -lah before making changes.
What does ls do in Linux?
The ls command lists files and directories. By itself, it shows visible entries in the current directory. With options, it becomes much more informative. ls -l shows a long listing with permissions, ownership, size and modification time. ls -a includes hidden files that begin with a dot. ls -h shows human-readable sizes when combined with long format. A common practical form is ls -lah, which gives a clear overview of files, hidden entries, owners, permissions and sizes. ls is one of the first commands to run before copying, deleting, editing or troubleshooting files because it shows what actually exists.
What does tree do in Linux?
The tree command displays files and directories in a visual tree structure. It helps users understand how a directory is organized below the current level. For example, tree -L 2 shows two levels of structure, which is often enough to understand a project, backup folder or website directory without overwhelming output. tree is useful before deleting, archiving or moving a directory because it lets you inspect the structure first. It may not be installed by default on minimal systems. If it is unavailable, find . -maxdepth 2 can provide a similar overview, although the output is less visually organized.
What is an absolute path in Linux?
An absolute path is a full path that starts at the root directory /. For example, /home/pat/Documents/report.txt is an absolute path because it describes the complete location from the top of the filesystem tree. Absolute paths work no matter where the current shell is located. This makes them especially useful in scripts, system administration, documentation and scheduled tasks. If a script uses an absolute path, it does not depend on the current directory from which the script was started. Absolute paths reduce ambiguity and are safer when working with configuration files, backups, services, logs and system directories.
What is a relative path in Linux?
A relative path describes a location starting from the current working directory. For example, if the current directory is /home/pat, the relative path Documents/report.txt refers to /home/pat/Documents/report.txt. Relative paths are convenient because they are shorter, but they depend entirely on context. The same command can affect different files if it is run from different directories. This is why pwd is so important. Before using relative paths with commands such as rm, cp, mv, tar or rsync, users should confirm where they are. Relative paths are efficient for daily work, but they require awareness.
Why is the Linux filesystem described as a tree?
The Linux filesystem is described as a tree because everything begins at a single root directory written as /, and all files, directories, devices and mounted filesystems appear somewhere below it. Unlike systems that organize storage primarily by drive letters, Linux uses one unified hierarchy. A user’s home folder may be under /home, system configuration under /etc, logs under /var/log, programs under /usr/bin and devices under /dev. Additional disks and USB drives can be mounted into this same tree. Understanding the tree structure is essential because nearly every Linux command works with paths inside this hierarchy.
What is the root directory in Linux?
The root directory is the top-level directory of the Linux filesystem and is written as /. Every absolute path begins there. Directories such as /home, /etc, /var, /usr, /boot, /dev and /tmp are located under the root directory. The root directory should not be confused with the root user. The root directory is a filesystem location, while the root user is the administrative superuser account. This distinction is important because “go to root” may mean changing to /, while “run as root” means using administrative privileges. The two concepts are related but not the same.
What is the root user in Linux?
The root user is the administrative superuser account in Linux. It has the highest level of authority and can modify system files, install software, manage users, change ownership, restart services and remove protected data. This power is necessary for system administration, but it is also dangerous when used carelessly. A mistake made as root can damage the entire operating system. Modern Linux workflows often avoid direct root login and use sudo for specific administrative commands instead. This gives users elevated privileges only when needed and makes it easier to control and audit administrative actions.
What does sudo do in Linux?
The sudo command runs another command with elevated privileges, usually as the root user. For example, sudo apt update refreshes package lists with administrative rights. sudo is required for many system-level tasks, including installing software, editing files under /etc, restarting services, mounting filesystems and changing ownership of protected files. However, sudo should not be used automatically for every command. If a task can be done as a normal user, it is usually safer to do it that way. Using sudo unnecessarily increases the risk of damaging system files or hiding the real cause of permission errors.
Why should beginners avoid using sudo for everything?
Beginners should avoid using sudo for everything because it bypasses normal Linux permission protections. Those protections exist to prevent accidental damage to system files and other users’ data. When a command fails with “permission denied,” the correct response is not always to add sudo. The better response is to understand why permission was denied. The user may be in the wrong directory, targeting the wrong file, using the wrong account or trying to perform an action that should not be performed. sudo is powerful, but it should be used deliberately. A safe Linux user understands the task before elevating privileges.
What does man do in Linux?
The man command opens the manual page for another command. For example, man ls shows official documentation for the ls command, including its syntax, options and behavior. Manual pages are one of the most reliable sources of Linux command information because they are installed with the system and correspond to the tools available on that machine. Inside many manual pages, Space moves forward, b moves backward, /word searches and q exits. Although manual pages can be dense at first, learning to read them is one of the best ways to become independent on Linux. Good users do not memorize everything. They know how to find accurate information.
What does –help do in Linux commands?
Many Linux commands support the –help option, which prints a quick usage summary in the terminal. For example, ls –help shows common options and syntax for ls. This is useful when you need a fast reminder and do not want to open a full manual page. The output is usually shorter and less detailed than man, but it is often enough for basic usage. A practical learning workflow is to use command –help for quick reference, man command for deeper documentation and safe test files for experimentation. Not every command behaves identically, but –help is a widely used convention.
What does type do in Linux?
The type command shows how the shell interprets a command name. It can reveal whether a command is a shell built-in, alias, function, keyword or external executable. For example, type cd may show that cd is a shell built-in, while type grep may show that grep is located at /usr/bin/grep. This matters because built-ins affect the current shell directly, while external commands run as separate programs. type is also useful when a command behaves unexpectedly. An alias may be changing the default behavior, or a different executable may be found earlier in the PATH.
What does command -v do in Linux?
The command -v command shows how a command name will be resolved by the shell. For example, command -v python3 may return /usr/bin/python3. It is useful for checking whether a command exists, where it is located and whether the shell can find it through the PATH. In scripts, command -v is often preferred over which because it is more reliable in POSIX-style shell logic. If command -v nginx returns nothing, the shell cannot find an executable named nginx in the current command search path. This helps diagnose “command not found” errors.
What does mkdir do in Linux?
The mkdir command creates directories. For example, mkdir projects creates a directory named projects in the current location. With an absolute path, mkdir /home/pat/projects creates the directory at that exact location if the user has permission. The -p option is especially useful because it creates parent directories if they do not already exist. For example, mkdir -p projects/linux/scripts creates the full nested structure. mkdir is usually safe, but users should still understand where they are creating directories. Creating folders inside system locations such as /etc, /usr, /var or /opt may require administrative privileges and should be intentional.
What does touch do in Linux?
The touch command creates an empty file if it does not exist or updates the timestamp of an existing file. For example, touch notes.txt creates an empty file named notes.txt in the current directory. It is useful for placeholders, test files, scripts and timestamp-based workflows. However, touch does not open an editor and does not add content. If a user wants to write text immediately, an editor such as nano may be more practical. In scripts, touch is often used to ensure that a file exists before another command writes to it or checks its modification time.
What does nano do in Linux?
nano is a terminal-based text editor used to create and edit text files. It is popular with beginners because its basic keyboard shortcuts are visible at the bottom of the screen. For example, nano notes.txt opens notes.txt for editing or creates it if it does not exist. nano is often used for quick notes, shell scripts and configuration files. When editing protected system files, it may need sudo, such as sudo nano /etc/hosts. The main safety rule is to understand what file you are editing before saving. This is especially important under /etc, where configuration mistakes can affect services or networking.
What does cat do in Linux?
The cat command displays file contents in the terminal and can concatenate multiple files. For example, cat notes.txt prints the entire file to standard output. It is useful for short files, quick checks and simple content inspection. However, cat is not ideal for long files because it prints everything at once and can flood the terminal. For large files, logs or configuration files, less is usually better because it allows scrolling and searching. cat is also commonly used in pipelines, although many commands can read files directly. Use cat when you want fast complete output from a manageable text file.
What does less do in Linux?
The less command opens a file or command output in an interactive pager. It lets users scroll, search and inspect long text without dumping everything into the terminal at once. For example, less /var/log/syslog opens the system log for controlled reading. Inside less, /pattern searches, n moves to the next match and q exits. less is especially useful for logs, documentation, configuration files and long command output. It is safer and more practical than cat for large files because it does not overwhelm the terminal. Many manual pages and command outputs are displayed through a pager like less.
What does head do in Linux?
The head command displays the beginning of a file. By default, it usually shows the first ten lines. For example, head file.txt shows the start of the file, while head -n 20 file.txt shows the first twenty lines. head is useful when inspecting structured files because headers, column names or format examples often appear near the top. It is also useful for quickly checking whether a file contains the expected kind of data. Since head only reads data and does not modify files, it is a safe inspection command. It is commonly used before deeper filtering, parsing or processing.
What does tail do in Linux?
The tail command displays the end of a file. By default, it usually shows the last ten lines, which makes it especially useful for log files because the newest entries are commonly written at the bottom. For example, tail -n 50 /var/log/syslog shows the last fifty lines of the system log. The option -f follows a file as it grows, so tail -f /var/log/syslog displays new log entries in real time. This is one of the most practical Linux troubleshooting commands because it allows a user to restart a service, reproduce an error or trigger an application action and immediately watch what the system records.
What does cp do in Linux?
The cp command copies files and directories. For example, cp report.txt backup/ copies the file report.txt into the backup directory. To copy a directory and its contents, the recursive option is usually required, such as cp -r project project-backup. For more careful copying, cp -i asks before overwriting existing files, while cp -a preserves attributes such as permissions, timestamps and symbolic links more completely. cp is often used before editing configuration files because a backup copy gives the user a way to restore the previous version. The main safety rule is to verify both the source and the destination before running the command.
What does mv do in Linux?
The mv command moves or renames files and directories. For example, mv oldname.txt newname.txt renames a file, while mv report.txt archive/ moves the file into the archive directory. Unlike cp, mv does not leave the original file in place. This makes it efficient but also more risky when used without checking paths. If the destination already exists, the command may overwrite or replace data depending on the situation and options. The interactive form mv -i can reduce risk by asking before overwriting. mv is also commonly used in deployment and configuration workflows where a prepared file is moved into its final location.
What does rm do in Linux?
The rm command removes files. For example, rm old.txt deletes the file named old.txt from the current directory. To remove directories, users often add recursive options, but this should be done carefully because command-line deletion usually does not move files to a trash folder. Once data is removed with rm, recovery may be difficult or impossible. Safer habits include running pwd to confirm the current location, using ls -lah to inspect the target, checking directory size with du -sh and using rm -i for confirmation. rm is a normal Linux command, but it should be treated as a final action after inspection.
Why is rm -rf dangerous?
rm -rf is dangerous because it combines recursive deletion with forced deletion. The -r option removes directories and everything inside them, while -f suppresses many warnings and ignores missing files. If the target path is wrong, too broad or unexpectedly expanded by the shell, the command can remove a large amount of data very quickly. The risk becomes much greater when used with sudo because administrative privileges may allow system files to be deleted as well. Before using rm -rf, inspect the target with pwd, ls -lah, du -sh and preferably tree or find. Professional Linux users respect this command because it obeys exactly what was typed.
What does grep do in Linux?
The grep command searches text for lines that match a pattern. For example, grep “error” app.log prints lines from app.log that contain the word error. It is one of the most important Linux commands because Linux stores a great deal of useful information as text, including logs, configuration files, command output and scripts. Common options include grep -i for case-insensitive search, grep -r for recursive search through directories, grep -n for line numbers and grep -v for inverted matches. grep is especially powerful in pipelines because it can filter large command outputs into only the lines that matter.
What does find do in Linux?
The find command searches the filesystem for files and directories based on criteria such as name, type, size, modification time, ownership and location. For example, find /home/pat -name “*.txt” searches for text files under /home/pat. find . -type f lists files below the current directory, while find . -type d lists directories. It can also find large files with -size, recently changed files with -mtime and run commands on results with -exec. find is essential for cleanup, troubleshooting, audits, automation and file discovery because it searches through directory trees with precise conditions.
What is the difference between grep and find?
grep searches inside text, while find searches for filesystem objects such as files and directories. If you need to locate files by name, type, size or modification date, use find. If you need to locate lines of text inside files or command output, use grep. For example, find /var/log -name “*.log” finds log files, while grep “permission denied” /var/log/syslog searches for a phrase inside a log file. The two commands are often used together. A user may first use find to locate a set of files and then use grep to search inside those files.
What are pipes in Linux?
Pipes send the output of one command into another command as input. The pipe character is |. For example, ps aux | grep nginx sends the list of running processes into grep, which filters lines containing nginx. Pipes are one of the most important ideas in Linux because they allow small commands to be combined into custom workflows. One command can list data, another can filter it, another can sort it, another can count it and another can save it. This is why Linux commands are powerful even when individual commands are simple. The real strength often comes from combining tools correctly.
What is output redirection in Linux?
Output redirection sends command output to a file instead of displaying it in the terminal. The > operator writes output to a file and replaces existing content, while >> appends output to the end of a file. For example, ls -lah > listing.txt saves a directory listing into listing.txt, and date >> activity.log appends the current date to activity.log. Redirection is useful for reports, logs, automation and scripts. It must be used carefully because > can overwrite an existing file. If preserving previous content matters, >> is safer, but the user should still confirm which file is being written.
What is standard error in Linux?
Standard error, often called stderr, is a separate output stream used for error messages. Standard output, or stdout, is used for normal command results. This separation allows a command to send useful results to one place and error messages to another. For example, normal output can be saved to a file while errors remain visible in the terminal. In shell syntax, > redirects standard output, while 2> redirects standard error. Understanding standard error is important in scripts and troubleshooting because a command may fail even when its normal output file looks empty. Error streams help explain what went wrong and where.
An exit code is a numeric status returned by a command when it finishes. In general, an exit code of 0 means success, while a non-zero exit code means an error or special condition occurred. The most recent exit code is stored in $?. For example, after running a command, echo $?
shows whether it succeeded. Exit codes are essential in shell scripting because they allow scripts to make decisions. A script can continue only if a command succeeded, stop when a command fails or report failure to another tool. Exit codes are one of the reasons Linux commands work well in automation.
What does awk do in Linux?
awk is a text processing language commonly used to extract, filter and transform structured text. It is especially useful when working with columns, logs, CSV-like files or command output. For example, awk ‘{print $1}’ access.log prints the first whitespace-separated field from each line. awk can also apply conditions, calculate totals, format output and process records line by line. While grep is excellent for finding matching lines, awk is better when the user needs specific fields or more complex logic. It is one of the classic Linux tools for turning raw text into meaningful information.
What does sed do in Linux?
sed is a stream editor used to transform text. It can replace text, delete lines, print selected ranges and perform automated edits. A common use is substitution, such as sed ‘s/old/new/g’ file.txt, which prints the file content with occurrences of old replaced by new. By default, sed usually writes transformed output to the terminal without changing the original file. In-place editing requires an option such as -i, which should be used carefully. sed is powerful in scripts because it can apply repeatable text transformations to logs, configuration files and generated output, but every expression should be tested before modifying important files.
What does cut do in Linux?
The cut command extracts selected parts of each line from text input. It is commonly used to extract fields or character ranges. For example, cut -d: -f1 /etc/passwd uses the colon as a delimiter and prints the first field, which contains usernames. cut is useful when the input has a consistent structure and delimiter. It is simpler than awk, which makes it convenient for quick field extraction. However, cut is less flexible when spacing is inconsistent or when conditional logic is needed. In those cases, awk is usually more appropriate. cut is best for simple, predictable text extraction tasks.
What does wc do in Linux?
The wc command counts lines, words, bytes or characters. For example, wc -l file.txt counts the number of lines in a file. In pipelines, wc -l is often used to count how many results another command produced. For example, find . -type f | wc -l counts files below the current directory. wc is useful in administration, scripting, data validation and log analysis. It can answer practical questions such as how many errors appeared, how many files match a condition or whether an export contains the expected number of lines. It is simple, but it becomes powerful when combined with pipes.
What does sort do in Linux?
The sort command sorts lines of text. It can sort alphabetically, numerically, in reverse order or by specific fields depending on options. For example, sort names.txt sorts names alphabetically, while sort -n numbers.txt sorts numbers numerically. In pipelines, sort is often used before uniq so repeated values appear next to each other and can be counted. This is common in log analysis, where users may extract IP addresses, sort them and count repeated entries. sort does not normally modify the original file unless output is redirected or special options are used. It is primarily an organization and analysis tool.
What does uniq do in Linux?
The uniq command filters adjacent duplicate lines. It is usually used after sort because duplicate lines must be next to each other for uniq to detect them reliably. For example, sort names.txt | uniq prints unique sorted names. With uniq -c, it counts repeated lines, which is useful for frequency analysis. A common log analysis pattern is to extract values, sort them, count them with uniq -c and sort the counts again. This can reveal the most common IP addresses, errors, URLs, usernames or repeated events. uniq is simple, but it is very effective in command pipelines.
What does tar do in Linux?
The tar command creates and extracts archive files. An archive combines many files and directories into one file. With compression, tar archives often use extensions such as .tar.gz or .tgz. For example, tar -czf backup.tar.gz project/ creates a compressed archive of the project directory, while tar -xzf backup.tar.gz extracts it. tar is widely used for backups, software distribution, log packaging and migration. The common options include c for create, x for extract, z for gzip compression and f for filename. Users should inspect paths carefully before extracting archives because files may be placed in unexpected locations.
What is the difference between tar, gzip and zip?
tar creates an archive by combining files and directories into one file, but it does not necessarily compress them by itself. gzip compresses a file or stream. A .tar.gz file is usually a tar archive compressed with gzip. zip combines archiving and compression in a format commonly used across Windows, macOS and Linux. On Linux systems, .tar.gz is common for backups, source code and software packages because it preserves Unix-style structure well. zip is often convenient when sharing files with users on different operating systems. The right choice depends on compatibility, preservation needs and workflow.
What does chmod do in Linux?
The chmod command changes file and directory permissions. Permissions decide who can read, write or execute a file. For example, chmod +x script.sh makes a script executable, while chmod 644 file.txt sets a common permission mode for a regular text file. For directories, execute permission has a special meaning because it allows users to enter or traverse the directory. chmod is essential for scripts, web hosting, shared directories and security. It should be used precisely. Broad commands such as recursive permission changes or chmod 777 can create security problems or break applications if the user does not understand the effect.
What does chown do in Linux?
The chown command changes the owner and optionally the group of a file or directory. For example, sudo chown pat:www-data file.txt changes the owner to pat and the group to www-data. Ownership is central to Linux permissions because access is evaluated differently for the owner, group and others. chown is often used in web hosting, deployment, shared directories and service configuration. It usually requires administrative privileges when changing ownership to another user. Recursive ownership changes with chown -R should be used carefully because they can affect many files at once. Wrong ownership can break services or expose sensitive data.
What is the difference between permissions and ownership in Linux?
Ownership defines who owns a file and which group is associated with it. Permissions define what the owner, group and others can do with that file. For example, a file may be owned by pat, assigned to the group www-data and configured so the owner can write while the group can read. chown changes ownership, while chmod changes permission bits. Both must be understood together. If a web server cannot read a file, the problem may be ownership, permissions or parent directory access. A good troubleshooting workflow checks the user running the process, the file owner, the group and the permission mode.
What does umask do in Linux?
umask controls the default permissions assigned to newly created files and directories. It does not change existing files. Instead, it subtracts permissions from the system’s default creation mode. For example, a common umask of 022 allows the owner to write while preventing group and others from writing by default. This matters for security and collaboration because it affects how open or restrictive new files are. If files are created with permissions that are too broad, sensitive data may be exposed. If they are too restrictive, services or team members may not be able to access them. umask explains many default permission behaviors.
What are users and groups in Linux?
Users represent accounts that can own files and run processes. Groups are collections of users used to manage shared access. Every file has an owner and a group, and permissions are evaluated for the owner, the group and everyone else. Processes also run as users, which affects what files and ports they can access. This is why services such as web servers and databases often run under dedicated service accounts instead of root. Groups make collaboration safer because they allow several users or services to share access without making files open to everyone. Understanding users and groups is essential for Linux permissions, security and administration.
What does apt do in Linux?
The apt command manages software packages on Debian-based Linux distributions such as Debian, Ubuntu and Linux Mint. It can update package information, install software, upgrade installed packages, remove software and search available packages. Common examples include sudo apt update, sudo apt upgrade, sudo apt install package-name and sudo apt remove package-name. apt is important because it does more than download software. It also works with repositories, dependency information, package versions and system integration. A user should understand that installing software through apt is usually safer and more maintainable than downloading random installation files from the internet, because the package manager can track installed files and future updates.
What is the difference between apt update and apt upgrade?
apt update refreshes the local package index from configured software repositories. It does not install new versions by itself. It only tells the system what package versions are currently available. apt upgrade installs available updates for packages that are already installed on the system. This distinction is important because users often think apt update updates the operating system, but it only updates package information. A normal maintenance workflow on Debian-based systems often begins with sudo apt update, then continues with sudo apt upgrade. On production servers, upgrades should be reviewed carefully because package updates may restart services, change dependencies or require configuration decisions.
What does dpkg do in Linux?
dpkg is a lower-level package management tool used on Debian-based Linux systems. While apt works with repositories and resolves dependencies, dpkg directly installs, removes and queries individual .deb package files. For example, sudo dpkg -i package.deb installs a local Debian package file. If dependencies are missing, dpkg may report errors instead of automatically resolving them the way apt usually does. dpkg -l lists installed packages and is often combined with grep to search for a specific package. For everyday package management, apt is usually more convenient, but dpkg remains important for manual installations, package audits and troubleshooting broken package states.
What does systemctl do in Linux?
systemctl controls and inspects services and system units managed by systemd. It can start, stop, restart, reload, enable, disable and check the status of services. For example, systemctl status nginx shows whether the Nginx service is active, failed or inactive, while sudo systemctl restart nginx restarts it. sudo systemctl enable nginx configures the service to start automatically at boot. systemctl is one of the most important commands for modern Linux administration because many background services, including web servers, databases, SSH, scheduled units and system components, are managed by systemd. It should generally be preferred over older service-management commands on systemd-based systems.
What is the difference between restarting and reloading a Linux service?
Restarting a service stops it and starts it again. Reloading a service asks it to re-read configuration without fully stopping, if the service supports that behavior. For example, a web server may support reload so configuration changes can be applied with less disruption than a full restart. However, not every service supports reload, and not every change can be applied safely without restarting. A professional workflow is to test the configuration first when the service provides a test command, then reload if appropriate, then verify status and logs. Restarting is more disruptive but often more complete. Reloading is gentler, but it depends on the service.
What does journalctl do in Linux?
journalctl reads logs collected by the systemd journal. It is essential on modern Linux systems because many service messages, boot logs and system events are stored in the journal. For example, journalctl -u nginx shows logs for the Nginx service, while journalctl -f follows new log entries in real time. journalctl -u service-name -f is especially useful when restarting a service or reproducing an error because it shows new messages as they appear. Compared with reading one static log file, journalctl can filter by service, boot, priority and time range. It is one of the strongest troubleshooting tools in systemd-based Linux administration.
What does ps do in Linux?
The ps command shows running processes. A common form is ps aux, which lists processes from all users with details such as user, process ID, CPU usage, memory usage, start time and command. ps is useful when a user needs to know whether a program is running, which user started it, what command launched it or which process ID should be inspected. Unlike top or htop, ps shows a snapshot at one moment rather than a live updating view. It is often combined with filtering commands such as grep, although commands such as pgrep may be cleaner for finding processes by name.
What does top do in Linux?
The top command displays a live, updating view of system activity. It shows running processes, CPU usage, memory usage, load average, task counts and other system information. top is useful when a system feels slow and the user needs to identify which processes are consuming resources. It can show whether high CPU usage comes from one process, whether memory is under pressure or whether the system is handling an unusual workload. top is available on many Linux systems by default, which makes it reliable during troubleshooting. Although htop is often more comfortable to use, top remains a standard command every administrator should understand.
What does htop do in Linux?
htop is an interactive process viewer similar to top, but usually more readable and easier to control. It shows CPU, memory, swap and process information in a visual terminal interface. Users can sort processes, search, filter and send signals more conveniently than with basic top. htop is especially useful for beginners because it makes system activity easier to understand. However, it may not be installed by default on minimal servers, so it may need to be installed through a package manager. htop does not replace understanding process behavior, but it provides a practical live view of which programs are using system resources.
What does kill do in Linux?
The kill command sends a signal to a process. Despite its name, it does not always immediately destroy a process. By default, it usually sends a termination signal, asking the process to stop gracefully. For example, kill 12345 sends a signal to the process with the process ID 12345. If a process does not respond, kill -9 12345 sends SIGKILL, which forces termination and cannot be handled by the process. This should be a last resort because it does not allow clean shutdown or cleanup. A careful workflow is to identify the process, understand its purpose and attempt graceful termination before forcing it.
What does killall do in Linux?
The killall command sends a signal to processes by name rather than by process ID. For example, killall php sends a signal to all processes named php. This can be convenient, but it can also be risky because it may affect more processes than intended. On a shared server or production machine, several unrelated tasks may use the same command name. Before using killall, it is safer to inspect matching processes with tools such as ps, pgrep or htop. killall is useful when the target is clear, but precise process IDs are safer when only one specific process should be stopped.
What is a process ID in Linux?
A process ID, often shortened to PID, is a unique number assigned to a running process. Commands such as ps, top, htop, pgrep and systemctl status can show process IDs. PIDs matter because many administrative actions target a specific process. For example, kill 12345 sends a signal to the process with PID 12345. A PID exists only while the process is running, and Linux may reuse that number later for a different process. This means users should verify the current process before acting on a PID. Process IDs are central to understanding how Linux tracks and controls running programs.
The df command reports disk space usage at the filesystem level. The common form df -h shows mounted filesystems with human-readable sizes, used space, available space and usage percentage. It helps answer the question: which filesystem is full or nearly full?
This is important because a full filesystem can cause package updates, logs, databases, uploads and services to fail. df does not tell which directory is responsible for the usage. It shows the filesystem overview. After identifying a full filesystem with df -h, administrators usually use du to investigate which directories or files are consuming space.
What does du do in Linux?
The du command estimates disk usage for files and directories. For example, du -sh /var/log shows the total size of the /var/log directory in a human-readable summary. It is commonly used after df -h shows that a filesystem is full. While df identifies the full filesystem, du helps locate the directories responsible for the usage. A practical troubleshooting pattern is to check major directories one level at a time, then drill down into the largest one. du is a read-only inspection command, but it may require administrative privileges to inspect directories that the current user cannot access.
What is the difference between df and du?
df reports used and available space at the filesystem level, while du reports estimated usage by files and directories. If a server has a full disk, df -h tells which filesystem has no space left. Then du helps identify what directory or file tree is using the space. The two commands can sometimes appear to disagree. One common reason is that a large file has been deleted but is still held open by a running process. In that case, du may not show the file because it no longer has a visible name, while df still shows the space as used. Commands such as lsof can help diagnose that situation.
What does mount do in Linux?
The mount command attaches a filesystem to a directory in the Linux filesystem tree. A disk partition, USB drive, network share or image file can be mounted so its contents appear under a mount point such as /mnt/usb or /mnt/backup. This reflects the Linux design where storage devices are integrated into one directory hierarchy instead of being represented primarily as drive letters. Mounting usually requires administrative privileges. It should be done carefully because mounting a filesystem over an existing directory can temporarily hide the original contents of that directory. mount is a core command for storage administration, recovery work and removable media handling.
What does umount do in Linux?
The umount command detaches a mounted filesystem from the Linux directory tree. For example, sudo umount /mnt/usb unmounts the filesystem mounted at /mnt/usb. This is important before removing external drives because Linux may cache writes for performance. A proper unmount helps ensure that pending data is written and the filesystem is cleanly detached. If umount reports that the target is busy, some process may still be using files inside the mounted filesystem. In that case, tools such as lsof or fuser can help identify the process. Forced unmounts should be avoided unless truly necessary because they can risk data integrity.
What does ip do in Linux?
The ip command displays and manages network configuration. It is the modern replacement for many older tools such as ifconfig and route. Common uses include ip addr to show network interfaces and IP addresses, ip link to show interface state and ip route to show routing information. The ip command is essential for diagnosing network problems because it shows whether an interface has an address, whether it is up and which gateway is being used. New Linux users may still find older tutorials using ifconfig, but learning ip is more accurate for current Linux administration.
What does ping do in Linux?
The ping command tests basic network reachability by sending ICMP echo requests to a host and waiting for replies. For example, ping 8.8.8.8 tests whether the system can reach that IP address, while ping example.com also involves DNS resolution because the hostname must be translated to an IP address. Ping results can show latency and packet loss. However, ping is not a complete test of network health. Some systems block ICMP, and a successful ping does not prove that a specific service such as HTTP, SSH or a database is reachable. It is best used as one step in a layered network diagnosis.
What does ss do in Linux?
The ss command displays socket and network connection information. It is the modern replacement for many uses of netstat. A common administrative command is ss -tulpn, which shows listening TCP and UDP sockets and the associated processes when permissions allow. This helps answer whether a service is listening on the expected port and address. For example, if a web server is running but unreachable, ss can show whether it is actually listening on port 80 or 443. ss is especially useful for troubleshooting web servers, databases, SSH, reverse proxies and application services that depend on network ports.
What does netstat do in Linux?
netstat displays network connections, routing tables, interface statistics and listening ports, but it is considered a legacy tool on many modern Linux systems. It may not be installed by default because newer tools such as ss and ip have replaced much of its functionality. Older tutorials often use commands like netstat -tulpn to show listening services. On current systems, ss -tulpn is usually the preferred equivalent. netstat remains useful when maintaining older systems or reading older documentation, but new Linux learners should understand that it belongs to an older generation of networking tools.
What does ssh do in Linux?
ssh provides secure remote access to another system over the network. For example, ssh pat@192.168.1.10 connects to the host at that IP address as the user pat, if authentication succeeds. SSH encrypts the session, which makes it the standard way to administer Linux servers remotely. It can use password authentication, but key-based authentication is usually more secure and convenient for professional use. SSH can also run remote commands, forward ports and support secure file transfer through tools such as scp and rsync. Because SSH often protects administrative access, it should be configured carefully and secured with strong authentication.
What does scp do in Linux?
scp copies files securely between systems using SSH. For example, scp file.txt pat@server:/home/pat/ copies file.txt to the remote server. It is simple and useful for one-time transfers when SSH access is already available. However, for large transfers, repeated synchronization, deployments or backups, rsync is often more efficient and flexible. scp is still valuable because its syntax is straightforward and it works well for basic file copying. Users should pay close attention to local and remote paths. A missing colon, wrong username or incorrect destination can change the meaning of the command and place files somewhere unexpected.
What does rsync do in Linux?
rsync synchronizes files and directories locally or over SSH. It is more efficient than simple copying because it can transfer only changed files or changed parts of files. A common form is rsync -av source/ destination/, where -a preserves attributes and -v shows verbose output. Over SSH, it can synchronize with a remote server, such as rsync -av site/ user@server:/var/www/site/. rsync is widely used for backups, deployments, migrations and large directory transfers. The trailing slash in the source path is very important because it changes whether the directory itself or only its contents are copied.
Why does the trailing slash matter in rsync?
The trailing slash in an rsync source path changes what is copied. When the source ends with a slash, such as project/, rsync copies the contents of the directory. Without the trailing slash, such as project, it may copy the directory itself into the destination. This difference can create unexpected nested directories or deploy files to the wrong level. For example, a website deployment may accidentally produce /var/www/html/project instead of placing files directly into /var/www/html. Because rsync is powerful and often works with important data, using –dry-run before a real synchronization is a professional safety practice.
What does wget do in Linux?
The wget command downloads files from the web or another network location directly from the terminal. It is useful on servers, minimal systems and SSH sessions where no graphical browser is available. For example, a user can download an archive, installation file, text resource or backup file by giving wget a URL. wget can also save files under a different name, resume interrupted downloads in some cases and work in scripts. It should be used carefully because downloading a file is not the same as trusting it. Before running anything downloaded with wget, users should verify the source, prefer HTTPS, check official documentation and use checksums or signatures when available.
What does curl do in Linux?
The curl command transfers data to or from servers using protocols such as HTTP, HTTPS, FTP and others. It is widely used for testing websites, inspecting HTTP headers, calling APIs, downloading files and debugging network services. For example, curl -I https://example.com retrieves HTTP response headers, while curl https://example.com prints the response body. Compared with wget, curl is often better for APIs and diagnostics because it gives fine control over headers, methods, authentication and request data. Developers, system administrators and DevOps engineers use curl constantly because modern infrastructure often communicates through web services, REST APIs and HTTP-based endpoints.
What does hostname do in Linux?
The hostname command displays the current system hostname and, depending on usage and permissions, may temporarily set it. A hostname identifies a machine in prompts, logs, monitoring tools and network contexts. On modern Linux systems, persistent hostname changes are often managed with hostnamectl, for example with a command that sets a new static hostname. A clear hostname is important in server administration because it helps distinguish production servers, staging machines, development systems and containers. Hostname changes should be planned because they may affect scripts, certificates, service discovery, monitoring, configuration management and documentation. A hostname is not just a label; it is part of operational clarity.
What does date do in Linux?
The date command displays the current system date and time. It can also format date output in specific ways, which makes it useful in scripts, backup filenames, logs and reports. For example, a script may use a formatted date to create a filename for a daily archive. Accurate time is important on Linux because logs, scheduled jobs, certificates, authentication, databases and distributed systems depend on correct timestamps. If the time is wrong, troubleshooting becomes harder because events appear out of order or certificates may fail validation. Changing system time usually requires administrative privileges and is often handled through time synchronization services rather than manual commands.
What does uptime do in Linux?
The uptime command shows how long the system has been running, how many users are logged in and the system load averages. It is a quick health check during troubleshooting. A very short uptime may indicate a recent reboot, crash or maintenance event. A very long uptime may look impressive, but it can also mean kernel updates have not been applied. Load averages help show how much work the system has been handling over the last one, five and fifteen minutes. However, load must be interpreted in context. A load average that is high for a single-core machine may be normal for a server with many CPU cores.
What does history do in Linux?
The history command shows previously executed shell commands. It is useful for repeating commands, reviewing recent work, documenting troubleshooting steps and learning from real command usage. Many shells also support reverse search through history, often with Ctrl+R, which helps find a command typed earlier. Shell history is helpful, but it should not be treated as a secure audit log. It may be incomplete, edited, disabled or cleared. Users should also avoid typing passwords, tokens or secrets directly into commands because they may be stored in history or visible to other tools. History is a productivity feature, not a safe place for sensitive information.
What does crontab do in Linux?
The crontab command manages scheduled tasks for cron. Cron allows commands or scripts to run automatically at specific times, such as every hour, every night or every week. It is commonly used for backups, cleanup tasks, reports, monitoring scripts and maintenance jobs. A cron entry contains time fields followed by the command to run. Cron is powerful, but it can surprise beginners because scheduled jobs often run with a limited environment. They may not have the same PATH, working directory or environment variables as an interactive terminal. Good cron jobs use absolute paths, log their output, handle errors and are tested manually before being scheduled.
What does screen do in Linux?
The screen command starts a persistent terminal session that can continue running even if the user disconnects. This is useful over SSH when running long tasks such as backups, migrations, package builds, large file transfers or maintenance scripts. A user can start a screen session, run a command, detach from the session and later reattach to continue monitoring it. This protects work from network interruptions or accidental terminal closure. Many administrators now also use tmux, which provides similar functionality with modern features. The important concept is persistent terminal work: long-running tasks should not depend entirely on one fragile SSH connection.
What does alias do in Linux?
An alias creates a shortcut or replacement for a command in the shell. For example, an alias can make a short command such as ll run a longer command such as ls -lah. Aliases can improve productivity, standardize frequently used options and reduce typing. They can also create safer defaults, although users should be careful not to hide important behavior behind unexpected shortcuts. If a command behaves differently than documentation suggests, type command-name can reveal whether an alias is involved. Aliases are often stored in shell configuration files such as .bashrc or .zshrc. They are useful, but they should remain clear and predictable.
What is the PATH variable in Linux?
PATH is an environment variable that tells the shell where to look for executable commands. It contains a list of directories separated by colons. When a user types a command such as grep, the shell searches the directories in PATH until it finds a matching executable. If a program is installed but its directory is not in PATH, the shell may return “command not found” even though the file exists. Understanding PATH helps diagnose command resolution problems, conflicting software versions and script behavior. It is especially important for root, cron jobs, deployment scripts and development environments where different command versions may exist.
What are environment variables in Linux?
Environment variables are named values available to processes and shells. They store information such as the current user’s home directory, preferred editor, language settings, command search path and application-specific configuration. Common examples include HOME, USER, SHELL, PATH and LANG. Programs and scripts often read environment variables to decide how they should behave. This is why a command may work in an interactive terminal but fail in cron, systemd or a deployment job where the environment is different. Understanding environment variables helps users troubleshoot inconsistent behavior, configure applications and write scripts that are predictable across different execution contexts.
What is a shell script?
A shell script is a text file containing commands that are executed by a shell. Shell scripts automate repeated tasks such as backups, deployments, log cleanup, system checks, file processing and maintenance workflows. A script can turn a manual sequence of commands into a reusable tool. Good shell scripts are careful with paths, quote variables, check errors and avoid destructive actions without validation. They should be tested on safe data before being used in production. Shell scripting is one of the most practical extensions of Linux command knowledge because it moves a user from typing individual commands to building repeatable administrative workflows.
What is a shebang in a Linux script?
A shebang is the first line of a script that tells Linux which interpreter should run the file. It begins with #!. For example, #!/bin/bash tells the system to run the script with Bash when the file is executed directly. Another common form is #!/usr/bin/env bash, which finds Bash through the environment. The shebang matters because different shells support different syntax. A script written for Bash may fail if it is run by a more minimal shell. A correct shebang makes script behavior more predictable and easier to understand. It is a small line with a major effect on script execution.
Why should variables be quoted in shell scripts?
Variables should usually be quoted in shell scripts to prevent word splitting and unexpected wildcard expansion. If a variable contains a filename with spaces and the script uses it without quotes, the shell may treat it as multiple arguments. Quoting the variable keeps the value together as one argument. This is one of the most important safety habits in shell scripting. It protects scripts from filenames with spaces, empty values, special characters and unexpected input. Many script bugs and data-loss incidents come from unquoted variables in commands such as rm, cp, mv, rsync or loops. A good default is to quote variables unless there is a deliberate reason not to.
What does source do in Linux?
The source command runs a script or configuration file in the current shell instead of launching a separate shell process. This matters because a normal script usually cannot permanently change the parent shell’s environment. When a file is sourced, variable assignments, functions, aliases and directory changes can remain active in the current session. For example, users often source shell configuration files after editing them so the changes apply immediately. source is useful for environment setup, development workflows and shell customization. It should be used only with trusted files because sourced code runs directly in the current shell context with the user’s privileges.
What does ln do in Linux?
The ln command creates links between files. It can create hard links or symbolic links. Symbolic links, created with ln -s, are more common in everyday administration because they act like path-based shortcuts to another file or directory. For example, a symlink may point from an enabled configuration location to an available configuration file. Links are useful for version switching, shared resources, configuration management and directory organization. However, symbolic links can break if the target is moved or deleted. Understanding links helps users understand many Linux systems, because symlinks are widely used in configuration directories, software paths and service setups.
What is a symbolic link in Linux?
A symbolic link, often called a symlink, is a special file that points to another path. It behaves like a shortcut. If the target exists, accessing the symlink usually accesses the target. If the target is removed or moved, the symlink becomes broken. Symlinks are created with ln -s target link-name. They are different from copies because they do not duplicate the target content. They are also different from hard links because they store a path reference rather than another direct name for the same file data. Symlinks are common in Linux because they provide flexible ways to connect files, directories, versions and configurations.
What does file do in Linux?
The file command identifies the type of a file by inspecting its contents and metadata rather than relying only on the filename extension. This is useful because Linux does not depend on extensions in the same way many users expect. A file named .txt may not contain plain text, and an executable file may have no extension at all. For example, file script.sh may identify a shell script, while file image.png may identify image data. The command is helpful before opening unknown files, troubleshooting downloads or checking whether a file is text, binary, compressed, executable or something else.
What does stat do in Linux?
The stat command displays detailed metadata about a file or directory. It can show size, ownership, permissions, inode number and timestamps such as access time, modification time and status change time. It provides more precise information than a basic ls -l listing. stat is useful when troubleshooting permissions, synchronization, backups, deployment issues or unexpected file changes. For example, it can help determine whether a file was modified recently or whether permissions differ from what an application expects. stat does not change the file. It is an inspection command, which makes it safe and useful when gathering evidence before making changes.
What does lsof do in Linux?
The lsof command lists open files. In Linux, many resources are represented as files, including regular files, directories, devices and network sockets. This makes lsof extremely useful for troubleshooting. It can show which process is using a file, which process is keeping a mount point busy or which process still holds a deleted file open. This last case is important when disk space appears full even after a large file was deleted. The file may be gone from the directory listing, but the space may not be released until the process closes it. lsof helps reveal these hidden relationships between processes and files.
What does dmesg do in Linux?
The dmesg command displays messages from the kernel ring buffer. These messages often relate to hardware, drivers, boot events, disk detection, USB devices, network interfaces and kernel-level errors. For example, after connecting a USB drive, dmesg may show how the kernel detected it and which device name was assigned. It is useful when troubleshooting hardware recognition, boot problems, disk errors, driver issues and low-level system events. On some systems, access to dmesg may be restricted for security reasons. It should be understood as a kernel diagnostic tool, not as a replacement for application logs or service-specific logs.
What does watch do in Linux?
The watch command repeatedly runs another command and displays updated output. For example, watch df -h refreshes disk usage every few seconds. This is useful when observing a changing condition, such as disk space during cleanup, process counts during a test, network sockets during service startup or file growth during a backup. watch helps users move from single snapshots to live observation. It is practical during troubleshooting because some problems appear only over time. However, users should avoid running expensive commands too frequently with watch, especially on production systems. Monitoring should provide insight without creating unnecessary load.
How do you troubleshoot “permission denied” in Linux?
To troubleshoot “permission denied,” first identify the exact file, directory or operation that failed. Then check the current user with whoami or id, inspect the file or directory with ls -l or stat, and verify the permissions of parent directories. If a service is involved, identify the user that the service runs as, because the interactive user may not be the relevant one. Avoid immediately using sudo or setting broad permissions such as chmod 777. The correct fix is usually a precise adjustment to ownership, group membership or permission bits. Good permission troubleshooting finds the smallest safe change that allows the required action.
How do you troubleshoot a full disk in Linux?
Start with df -h to identify which filesystem is full. Then use du to find which directories are consuming the most space. A typical investigation checks large top-level directories and drills down step by step. Common causes include logs, backups, caches, database files, container images, temporary files and deleted files still held open by running processes. If df shows high usage but du does not explain it, lsof may reveal deleted open files. Do not delete files randomly just because they are large. First understand what they belong to, whether a service is using them and whether there is a safe cleanup method.
How do you troubleshoot a failed Linux service?
Begin with systemctl status service-name to check whether the service is active, inactive, failed or repeatedly restarting. Then inspect detailed logs with journalctl -u service-name. Look for clear causes such as permission errors, invalid configuration, missing files, port conflicts, dependency failures or resource limits. If the service provides a configuration test command, run it before restarting. After making a change, restart or reload the service and verify status again. A strong troubleshooting workflow follows evidence: check status, read logs, test configuration, make one controlled change and verify the result. Restarting blindly may temporarily hide the real cause.
How do you troubleshoot network connectivity in Linux?
Use a layered approach. First check whether the network interface exists and has an IP address with ip addr. Then check routing with ip route. Test basic reachability with ping to an IP address. Then test name resolution with a hostname. If the issue is with a specific service, check whether it is listening with ss -tulpn. Use curl for HTTP services and verbose SSH output for SSH problems. Also consider firewall rules, DNS configuration, gateway availability and service binding addresses. Network troubleshooting is easier when each layer is tested separately instead of treating every failure as a general internet problem.
How do you know whether a Linux command is safe to run?
A Linux command is safer to run when the user understands what it will affect, whether it reads or modifies data, what privileges it needs and what the possible failure modes are. Inspection commands such as pwd, ls, cat, less, df, du, ps and systemctl status are generally safer because they mainly read information. Destructive or system-changing commands such as rm, mkfs, dd, recursive chmod, recursive chown, package removal and forced service operations require much more caution. Before running an unfamiliar command, read it from left to right, identify options and arguments, check the target path and consult trusted documentation.