Linux Fundamentals, Explained Step by Step
If you are reading this, you want to be comfortable in a Linux terminal, not just copy commands you do not understand. That is the goal here.
By the end you will be able to find your way around the Linux file system, read and change file permissions, understand who the users and groups on a system are, see and stop running processes, manage services and software, and write small bash commands that chain together. Every command is explained the first time it appears, and each section ends with something you can try.
How to use this guide: open a Linux terminal (a virtual machine with Ubuntu, Debian or Kali is ideal) and type the examples as you read. Reading alone will not make this stick; typing will.
Do not have Linux yet? Choose the row that matches your computer:
| Your computer | A way to get a Linux terminal |
|---|---|
| Windows 10 or 11 | Windows Subsystem for Linux (WSL). Open PowerShell as administrator, run wsl --install and restart. Microsoft says this command enables the features WSL needs and installs Ubuntu by default. |
| Windows, macOS or Linux | A virtual machine. Ubuntu's tutorial describes VirtualBox as "available across Linux, Mac OS and Windows", and VirtualBox publishes packages for Windows, Intel Mac and Apple silicon Mac hosts. Kali's documentation has guides for VirtualBox, VMware, UTM, Hyper-V and Parallels. |
| Mac with Apple silicon | UTM is a virtual machine app for Mac that uses Apple's Hypervisor framework to run ARM64 operating systems on Apple silicon. |
| Already on Linux | Open your terminal. |
The Terminal on a Mac looks similar but is not Linux: the macOS Terminal has no apt, systemctl or useradd, so use a virtual machine for the package, user and service sections. The Windows Command Prompt and PowerShell are also different from Linux shells.
1. What Linux is, and where you type
Linux is strictly the kernel: the core of the operating system that talks to the hardware and manages memory, processes and files. What people install is a distribution (a "distro"), which bundles the kernel with tools, a package manager and a default setup. Ubuntu, Debian, Fedora and Kali are distributions. The commands in this guide work on all of them unless noted.
You control Linux mostly through a terminal, a window where you type commands. Inside it runs a shell, the program that reads your commands and runs them. The most common shell is bash.
The prompt
When the terminal is ready, you see a prompt, something like:
alice@ubuntu:~$
It tells you who you are (alice), which machine (ubuntu), where you are (~, your home directory), and ends with $. By convention, $ means you are a normal user and # means you are root, the all-powerful administrator.
Anatomy of a command
ls -l /etc
lsis the command.-lis an option (also called a flag) that changes how it behaves./etcis an argument, the thing it works on.
Options can usually be combined: ls -la means ls -l -a. Many also have a long form with two dashes, such as --help.
Three rules that save you hours
- Linux is case-sensitive.
File.txtandfile.txtare different files. - There is no undo. Delete a file and it is gone; there is no recycle bin.
- Read the help first. Every standard command has a manual:
man ls # the full manual; press q to quit, / to search
ls --help # a shorter summary
Shortcuts worth learning on day one
| Keys | What they do |
|---|---|
Tab |
Auto-complete a name. Press twice to see the choices |
↑ / ↓ |
Move through your command history |
Ctrl+R |
Search your history |
Ctrl+C |
Stop the running command |
Ctrl+L (or clear) |
Clear the screen |
Where am I?
whoami # your username
hostname # this machine's name
uname -a # kernel and system details
cat /etc/os-release # which distribution and version
2. The Linux file system
On Windows you have drives like C: and D:. On Linux there is one single tree, and everything hangs from its top, the root directory, written /. Other disks and USB sticks are attached ("mounted") somewhere in the same tree.
Paths
- An absolute path starts with
/and always means the same place:/etc/passwd. - A relative path does not start with
/and starts from where you are now:Documents/notes.txt. .means the current directory, and..means the parent directory. You cannot go above the root:/..is just/.~is shorthand for your home directory.- A file or directory whose name starts with a dot, like
.bashrc, is hidden. It is not secret;lsjust skips it unless you ask.
The main directories
You do not need to memorize every folder, but you should know these:
| Directory | What lives there |
|---|---|
/ |
The root of the whole tree |
/bin |
Essential programs needed to boot and repair the system (on many modern systems it points into /usr/bin) |
/sbin |
Essential system administration programs, normally run by root |
/boot |
Files for the boot loader and the kernel |
/dev |
Device files that represent hardware (disks, terminals) |
/etc |
Configuration files for the machine |
/home |
Home directories of normal users, such as /home/alice |
/root |
The home directory of the root user |
/lib |
Shared libraries needed by programs in /bin and /sbin |
/media |
Mount points for removable media such as USB sticks |
/mnt |
A place to temporarily mount a file system |
/opt |
Optional add-on software packages |
/proc |
A virtual file system showing information about running processes and the kernel |
/sys |
A virtual file system showing information about the kernel and devices |
/run |
Information about the system since it booted |
/srv |
Data served by this system (for example, by a web server) |
/tmp |
Temporary files, which may be deleted without notice |
/usr |
Shareable, read-only programs and data (most installed software lives here) |
/var |
Files that change in size, such as logs (/var/log) and mail |
The three you will visit most: /etc (settings), /var/log (what happened), and /home (people's files).
Moving around: pwd, cd, ls
pwd # print working directory: where am I?
cd /etc # go to /etc (absolute path)
cd .. # go up one level
cd ~ # go home (plain `cd` does the same)
cd - # go back to the previous directory
ls # list the current directory
ls -l # long listing (details)
ls -a # include hidden files
ls -lah # long, all, human-readable sizes
The most useful ls options:
| Option | Meaning |
|---|---|
-l |
Long format: permissions, owner, group, size, date, name |
-a |
Show hidden files too |
-h |
Human-readable sizes (K, M, G), used with -l |
-R |
Recurse into subdirectories |
-t |
Sort by time, newest first |
-S |
Sort by size, largest first |
-d |
List directories themselves, not their contents |
Working with files
cat notes.txt # print a file's contents
less /var/log/syslog # read a long file page by page (q to quit)
head -n 5 notes.txt # first 5 lines
tail -n 20 notes.txt # last 20 lines
tail -f /var/log/syslog # keep showing new lines as they are added
touch newfile.txt # create an empty file (or update its timestamp)
mkdir projects # create a directory
mkdir -p a/b/c # create nested directories at once
cp a.txt b.txt # copy a file
cp -r folder1 folder2 # copy a directory and its contents
mv old.txt new.txt # move or rename
rm file.txt # delete a file (permanent)
rm -r folder # delete a directory and everything in it
rmdir emptyfolder # delete an empty directory
cat is short for "concatenate": it prints files, one after another. For long files use less.
A note on logs: some systems keep their logs only in the systemd journal, so /var/log/syslog may not exist. Debian's release notes say that from Debian 12 ("bookworm") the rsyslog package, which writes that file, is no longer installed by default. If the file is missing, read the logs with journalctl (section 6).
Be careful with rm. rm -r deletes a whole tree with no confirmation and no undo. Check your location with pwd and your target with ls before running it, and never run rm -r on a path you typed from memory as root. When unsure, use rm -i, which asks before each deletion.
Try it: create a folder, create three files in it, copy one, rename another, list the folder with ls -l, then delete the folder and confirm it is gone.
3. Searching: grep and find
Real systems hold thousands of files and logs. Two commands find what you need.
grep: search inside text
grep prints the lines that match a pattern.
grep "root" /etc/passwd # lines containing "root"
grep -i "error" app.log # ignore case
grep -n "error" app.log # show line numbers
grep -c "error" app.log # count matching lines
grep -v "debug" app.log # lines that do NOT match
grep -w "ssh" services.txt # whole word only
grep -r "password" /etc/ 2>/dev/null # search a whole folder tree
grep -l "root" /etc/* # only the names of files that match
grep -C 2 "failed" auth.log # 2 lines of context around each match
| Option | Meaning |
|---|---|
-i |
Ignore case |
-v |
Invert: show non-matching lines |
-n |
Prefix each line with its line number |
-c |
Count matching lines |
-l |
List only the names of files with a match |
-w |
Match whole words only |
-r |
Search directories recursively |
-E |
Use extended regular expressions |
-F |
Treat the pattern as plain text, not a regex |
-A n, -B n, -C n |
Show n lines After, Before, or around each match |
A few pattern characters (regular expressions): . matches any one character, ^ anchors to the start of a line, $ to the end, * repeats the previous item zero or more times, and [abc] matches any one of the listed characters. For example, grep "^root" /etc/passwd finds lines that begin with root.
grep also reports success through its exit status: 0 if it found a match, 1 if it found none, 2 if there was an error. Scripts rely on this.
The 2>/dev/null in the recursive example hides "permission denied" noise. You will see exactly how in section 8.
find: search for files
While grep looks inside files, find looks for the files themselves, using their name, type, size, age, owner or permissions.
find /home -name "notes.txt" # exact name (case-sensitive)
find /home -iname "*.pdf" # name pattern, ignore case
find /var/log -type f # regular files only
find /etc -type d # directories only
find . -size +100M # larger than 100 MiB
find . -mtime 0 # modified within the last 24 hours
find /home -user alice # owned by alice
find . -maxdepth 2 -name "*.conf" # do not go deeper than 2 levels
find . -name "*.log" -exec ls -l {} \; # run a command on each result
Key points:
- Quote patterns like
"*.pdf". Otherwise your shell may expand the*beforefindsees it. -type fmeans regular file,-type dmeans directory,-type lmeans symbolic link.- Size suffixes:
cbytes,kKiB,MMiB,GGiB. A+means "more than". -exec command {} \;runs the command once for every result, with{}replaced by the file name. Ending with{} +instead groups many files into one run, which is faster.- Searching
/as a normal user prints many "Permission denied" lines. Add2>/dev/nullto hide them.
You can also search by permission, which is useful for security checks. For -perm: -perm 664 means exactly those permissions, -perm -664 means at least those bits are set, and -perm /222 means any of those bits is set. For example, this finds files with the setuid bit (explained in section 5):
find / -type f -perm -4000 2>/dev/null
Try it: find every .conf file under /etc, then use grep to find which of them contain the word port.
4. Users and groups
Linux is a multi-user system. Every process and file belongs to a user, and that is the foundation of its security.
Users
Every user has a name and a number, the UID. The special user is root, with UID 0. Root can generally read, change and delete anything. Everything else is limited by permissions.
whoami # your username
id # your UID, primary group and all groups
who # who is logged in
User accounts are stored in /etc/passwd. Despite its name, it holds no passwords on modern systems. Each line has seven fields separated by colons:
alice:x:1001:1001:Alice Smith:/home/alice:/bin/bash
| # | Field | Example |
|---|---|---|
| 1 | Login name | alice |
| 2 | Password marker (x means the real hash is in /etc/shadow) |
x |
| 3 | UID | 1001 |
| 4 | Primary group ID (GID) | 1001 |
| 5 | Comment (often the full name) | Alice Smith |
| 6 | Home directory | /home/alice |
| 7 | Login shell | /bin/bash |
Password hashes live in /etc/shadow, which ordinary users must not be able to read. Each line has nine colon-separated fields: the login name, the password hash, then dates and limits for password age and expiry. In the hash field, a value starting with ! means the password is locked, and * means the account cannot log in with a password. Normally only root can read this file (on some systems, members of a special shadow group can too).
Many entries in /etc/passwd are not people but system accounts for services, such as www-data or daemon. They typically have a shell like /usr/sbin/nologin so nobody can log in as them.
Groups
A group is a collection of users that can share access to files. Every user has one primary group and may belong to others (supplementary groups). Groups live in /etc/group, four fields per line:
devs:x:1005:alice,bob
That is: group name, password field (rarely used), GID, and a comma-separated list of members.
groups # groups you belong to
groups alice # groups alice belongs to
cat /etc/group # all groups
Managing users and groups (needs root)
sudo useradd -m bob # create user bob with a home directory
sudo passwd bob # set bob's password
sudo usermod -aG devs bob # ADD bob to the group "devs"
sudo userdel -r bob # delete bob and his home directory
sudo groupadd devs # create a group
Note the -a in usermod -aG. It means append. Without it, -G replaces all of bob's supplementary groups with only the one you listed. On Debian and Ubuntu, adduser bob is a friendlier alternative to useradd.
Becoming root safely: sudo and su
Working as root all the time is dangerous, because one typo can destroy the system. Instead, use sudo to run a single command with higher privileges:
sudo apt update # run one command as root
sudo -l # list what you are allowed to do with sudo
sudo checks a policy, normally the /etc/sudoers file, to decide whether you may run a command, and it logs attempts. Use it only for what needs it. The related su command switches to another user completely (su - alice, or su - for root).
Try it: run id, then grep "^$(whoami):" /etc/passwd to see your own passwd line and match each field to the table above.
5. Permissions
This is the heart of Linux security. Every file and directory has an owner, a group, and permissions for three classes of people: the owner (user), the group, and others.
Reading ls -l
ls -l script.sh
-rwxr-xr-- 1 alice devs 220 Oct 2 10:15 script.sh
| Part | Meaning |
|---|---|
- |
File type: - regular file, d directory, l symbolic link |
rwx |
Permissions for the owner |
r-x |
Permissions for the group |
r-- |
Permissions for others |
1 |
Number of hard links |
alice |
Owner |
devs |
Group |
220 |
Size in bytes |
Oct 2 10:15 |
Last modified |
script.sh |
Name |
Each permission is a letter, or - if it is missing: r read, w write, x execute.
What r, w, x mean
| On a file | On a directory | |
|---|---|---|
| r | Read the contents | List the names inside |
| w | Change the contents | Create, delete or rename entries inside |
| x | Run it as a program | Enter it and reach things inside (it is also called "search" permission) |
Two consequences surprise beginners:
- To delete a file you need write permission on the directory that holds it, not on the file itself.
- To reach
/home/alice/notes.txt, you need execute permission on every directory along the path.
Which class applies to you?
Linux checks in order: if you are the owner, the owner's permissions apply. Otherwise, if you are in the file's group, the group's permissions apply. Otherwise, the others permissions apply. Only one class is used. So an owner with --- cannot read their own file, even if "others" have r--. Root is the exception and generally bypasses these checks.
Changing permissions: chmod
There are two ways to write it.
Symbolic mode: who, an operator, and the permissions.
chmod u+x script.sh # give the owner execute
chmod go-w file.txt # remove write from group and others
chmod a=r file.txt # everyone gets exactly read, nothing else
chmod g+rw,o-rwx file # several changes at once
Who: u owner, g group, o others, a all. Operators: + add, - remove, = set exactly.
Numeric (octal) mode: each permission has a value: r = 4, w = 2, x = 1. Add them for each class, and write three digits in the order owner, group, others.
| Digit | Sum | Permissions |
|---|---|---|
| 7 | 4+2+1 | rwx |
| 6 | 4+2 | rw- |
| 5 | 4+1 | r-x |
| 4 | 4 | r-- |
| 0 | none | --- |
chmod 755 script.sh # rwxr-xr-x
chmod 644 notes.txt # rw-r--r--
chmod 600 secret.key # rw-------
chmod -R 750 project/ # apply to a whole tree
Common choices:
| Mode | Typical use |
|---|---|
644 |
Normal files: owner edits, everyone reads |
755 |
Programs and directories: owner full, everyone can run or enter |
600 |
Private files such as SSH private keys |
700 |
Private directories |
777 |
Everyone can do everything. Almost always a mistake |
Changing ownership: chown and chgrp
sudo chown alice file.txt # change the owner
sudo chown alice:devs file.txt # change owner and group
sudo chown :devs file.txt # change only the group
sudo chown -R alice:devs project/ # the whole tree
chgrp devs file.txt # change only the group
The syntax is OWNER:GROUP. Normally only root can change a file's owner. A regular user may change a file's group only to one they belong to.
Special permission bits
A fourth, leading octal digit holds three special bits:
| Bit | Value | On an executable file | On a directory |
|---|---|---|---|
| setuid | 4 | Runs with the permissions of the file's owner | (not used) |
| setgid | 2 | Runs with the permissions of the file's group | New files inherit the directory's group |
| sticky | 1 | (not used) | Only a file's owner (or root) may delete it, even if others can write to the directory |
In ls -l the setuid bit shows as an s where the owner's x would be (-rwsr-xr-x), and the sticky bit as a t at the end (drwxrwxrwt). The /tmp directory is the classic example of sticky: everyone can create files there, but you cannot delete other people's.
chmod 4755 program # setuid + rwxr-xr-x
chmod +t shared_dir # sticky bit
Setuid is how a normal user can run passwd to change their own password, even though that changes protected system files. It is also dangerous: a flaw in a setuid-root program can give an attacker root. Setuid is ignored on shell scripts on many systems for that reason. This is why security checks list every setuid file, using the find command from section 3.
Try it: create a file, remove all your permissions with chmod 000 file, try to cat it and read the error, then restore with chmod 644 file.
6. Processes and services
Processes
Every running program is a process. Each has a unique PID (process ID) and a PPID (the PID of the process that started it). On most modern distributions the first process, PID 1, is systemd, which starts everything else.
ps aux # every process on the system
ps -ef # the same idea, different layout
ps aux | grep ssh # find a specific process
top # a live, updating view
pgrep ssh # just the PIDs of matching processes
Useful columns in ps aux:
| Column | Meaning |
|---|---|
USER |
Who owns the process |
PID |
Process ID |
%CPU, %MEM |
Share of CPU and memory used |
STAT |
State (see below) |
TIME |
Total CPU time used |
COMMAND |
What is running |
Process states in STAT: R running or ready to run, S sleeping (waiting for something), D waiting on input/output and cannot be interrupted, T stopped, Z zombie (finished but its parent has not yet collected its exit status).
In top, press q to quit, P to sort by CPU use, M to sort by memory, k to kill a process, and h for help.
Foreground and background
sleep 300 # runs in the foreground, ties up your terminal
sleep 300 & # the & starts it in the background
jobs # list background jobs
fg # bring one back to the foreground
Ctrl+C stops a foreground command. Ctrl+Z pauses it so you can resume it with fg or bg.
Stopping processes: kill and signals
kill does not always kill. It sends a signal to a process. The process (or the kernel) decides what to do.
kill 1234 # polite request to terminate (signal 15, TERM)
kill -9 1234 # force kill (signal 9, KILL)
kill -l # list all signal names
pkill firefox # signal processes by name
| Signal | Number | Meaning |
|---|---|---|
SIGHUP |
1 | "Hang up"; often used to make a service reload |
SIGINT |
2 | Interrupt (what Ctrl+C sends) |
SIGTERM |
15 | Ask to terminate, allowing clean-up. The default |
SIGKILL |
9 | Terminate immediately, no clean-up. Cannot be caught or ignored |
SIGSTOP |
19 | Pause the process. Cannot be caught or ignored |
SIGCONT |
18 | Resume a stopped process |
(Signal numbers shown are the common ones on x86 and ARM Linux.)
Try kill (TERM) first, because it lets the program save its work and tidy up. Use kill -9 only when the process ignores the polite request. You can only signal your own processes unless you are root.
Services
A service (or daemon) is a program that runs in the background, usually started at boot, such as a web server or SSH. Most modern distributions use systemd to manage services, with the systemctl command:
systemctl status ssh # is it running? show recent log lines
sudo systemctl start ssh # start it now
sudo systemctl stop ssh # stop it now
sudo systemctl restart ssh # stop then start
sudo systemctl reload ssh # re-read its config without a full restart
sudo systemctl enable ssh # start automatically at boot
sudo systemctl disable ssh # do not start at boot
sudo systemctl enable --now ssh # enable AND start right now
systemctl is-active ssh # prints "active" or not
systemctl list-units --type=service # services in memory
The most important thing to remember: start and enable are independent. start runs the service now, but it will not come back after a reboot. enable makes it start at boot, but does not start it now. --now does both at once.
The service name varies by distribution: on Debian and Ubuntu the SSH service is ssh, while on Red Hat family systems it is sshd.
To read a service's logs, use journalctl:
journalctl -u ssh # logs for one service
journalctl -u ssh -f # follow new log lines live
If you change a service's unit file, run sudo systemctl daemon-reload so systemd re-reads it.
Try it: start a sleep 600 & job, find its PID with ps aux | grep sleep, then stop it with kill. Run systemctl status ssh (or sshd) and read each line. If systemctl says the unit could not be found, that service is not installed on your machine; choose another from systemctl list-units --type=service, or install the SSH server with sudo apt install openssh-server.
Using WSL? Microsoft says systemd is now the default init system for the current Ubuntu installed by wsl --install, but other distributions may still use WSL's own init. To switch one to systemd, add [boot] and systemd=true to /etc/wsl.conf inside the distribution, then run wsl.exe --shutdown in PowerShell and start it again.
7. Package management
On Linux you usually install software from a package manager, which downloads programs from trusted repositories, installs their dependencies, and lets you update or remove them cleanly. This is safer than downloading random installers.
The tool depends on the distribution family:
| Family | Examples | Package manager |
|---|---|---|
| Debian | Debian, Ubuntu, Kali | apt (with dpkg underneath) |
| Red Hat | Fedora, RHEL, Rocky | dnf |
| Arch | Arch, Manjaro | pacman |
Because Kali, Ubuntu and Debian are the most common starting points, here is apt in detail. Commands that change the system need sudo; read-only ones such as search, show and list do not.
sudo apt update # refresh the list of available packages
sudo apt upgrade # install newer versions of installed packages
sudo apt full-upgrade # same, but may remove packages if needed
sudo apt install nmap # install a package
sudo apt remove nmap # remove it, keep its config files
sudo apt purge nmap # remove it AND its config files
sudo apt autoremove # remove dependencies no longer needed
apt search wireshark # find packages
apt show nmap # details about a package
apt list --installed # what is installed
The most common beginner confusion is update versus upgrade:
apt updateonly downloads the latest list of what is available. It installs nothing.apt upgradeactually installs the newer versions. It never removes an installed package; if an upgrade would need that, it skips it.full-upgradeis allowed to remove packages.
So the usual routine is:
sudo apt update && sudo apt upgrade
Equivalents on other systems are sudo dnf install nmap / sudo dnf remove nmap for Fedora and RHEL, and sudo pacman -S nmap / sudo pacman -R nmap for Arch.
Good habits: install from your distribution's repositories when you can, keep the system updated, and be very cautious about adding third-party repositories or running install scripts you have not read.
8. Bash basics
Everything so far was single commands. Bash lets you combine them, and that is where the real power is.
Variables
name="Alice" # NO spaces around =
echo "Hello, $name" # use $ to read a variable -> Hello, Alice
echo "${name}_file" # braces mark where the name ends
export EDITOR=nano # export: also visible to programs you start
echo $HOME # built-in variables: HOME, USER, PATH, PWD
PATH is a colon-separated list of directories where the shell looks for programs when you type a command name. echo $PATH shows it. That is why ls works from anywhere, and why your own script in the current folder needs ./ in front: ./script.sh.
Quoting
x=5
echo '$x' # single quotes: nothing expands -> $x
echo "$x" # double quotes: variables still expand -> 5
echo "Today is $(date)" # $(...) runs a command and inserts its output
echo \$x # backslash escapes one character -> $x
Single quotes are literal. Double quotes still let $variables and $(commands) through. Always quote variables that might contain spaces: "$file", not $file.
Wildcards
The shell expands these before the command runs:
| Pattern | Matches |
|---|---|
* |
Any number of characters: *.txt |
? |
Exactly one character: file?.txt |
[abc] |
One of the listed characters: file[123].txt |
Pipes and redirection
Every command has three standard streams: input (0), output (1), and errors (2). You can connect and redirect them.
Pipe | sends one command's output into the next command's input:
cat /etc/passwd | grep bash | wc -l # how many accounts use bash
ps aux | grep ssh
ls -l /etc | sort -k5 -n | tail -5 # the 5 entries with the largest size column
(wc -l counts lines; sort sorts lines.)
Redirection sends streams to files:
| Syntax | Meaning |
|---|---|
cmd > file |
Write output to a file (overwrites it) |
cmd >> file |
Append output to a file |
cmd < file |
Read input from a file |
cmd 2> file |
Send error messages to a file |
cmd 2> /dev/null |
Throw error messages away |
cmd > file 2>&1 |
Send output and errors to the same file |
cmd &> file |
Same, as a shorthand in bash |
Order matters: redirections are processed left to right. ls > out.txt 2>&1 sends both to the file, but ls 2>&1 > out.txt sends the errors to the screen and only the output to the file.
/dev/null is a special file that discards anything written to it.
Chaining commands
cmd1 ; cmd2 # run cmd2 after cmd1, whatever happens
cmd1 && cmd2 # run cmd2 ONLY if cmd1 succeeded
cmd1 || cmd2 # run cmd2 ONLY if cmd1 failed
Success is an exit status of 0; anything else means failure. The special variable $? holds the exit status of the last command:
grep -q root /etc/passwd
echo $? # 0: it found a match
Your first script
A script is a text file of commands. Create demo.sh:
#!/bin/bash
name="Learner"
echo "Hello, $name"
for f in /etc/passwd /etc/hosts /nonexistent; do
if [ -e "$f" ]; then
echo "$f exists"
else
echo "$f is missing"
fi
done
The first line, #!/bin/bash, tells the system which program should run the file. Then make it executable and run it:
chmod +x demo.sh
./demo.sh
You should see:
Hello, Learner
/etc/passwd exists
/etc/hosts exists
/nonexistent is missing
Notice how it all connects: the script needs chmod +x (permissions, section 5), it is found with ./ (PATH, above), and [ -e "$f" ] tests whether a file exists. Arguments passed to a script are available as $1, $2 and so on, and $# is how many there are.
Try it: write a script that takes a directory name as $1, checks that it exists with [ -d "$1" ], and if it does, prints how many files ls finds in it.
9. Quick reference
| I want to... | Command |
|---|---|
| See where I am | pwd |
| List files with details | ls -lah |
| Change directory | cd /path |
| Read a file | cat file, less file |
| Find text inside files | grep -rn "text" /path |
| Find files by name | find /path -name "*.conf" |
| See my identity and groups | id |
| Make a file executable | chmod +x file |
| Set exact permissions | chmod 640 file |
| Change owner and group | sudo chown user:group file |
| List processes | ps aux |
| Live process view | top |
| Stop a process politely | kill PID |
| Force stop a process | kill -9 PID |
| Check a service | systemctl status name |
| Start at boot and now | sudo systemctl enable --now name |
| Update package lists | sudo apt update |
| Install software | sudo apt install name |
| Hide error messages | command 2>/dev/null |
| Get help | man command |
Closing
You started with a blinking prompt and now you can move through the file system, search it, understand who owns what, read and set permissions, control processes and services, install software, and chain commands into scripts. These are the same skills professionals use daily, and everything more advanced, including security work, builds on them.
Three habits will help most from here:
- Read before you run. Know what a command does before pressing Enter, especially with
sudoandrm. - Think in permissions. Whenever something fails or looks risky, ask: who owns this, which class am I in, and what bits are set?
- Practice in a lab, not on a machine you care about. Repeat each "Try it" until it feels ordinary.
Next: Operating Systems and Windows Basics, Explained shows how the ideas you just used (processes, permissions, services, logs) work underneath, and how Windows does them. After that, Git and GitHub, Explained uses your terminal skills to track and share work.
Further practice
- Hack The Box Academy, module 18 (section 94). An account is required.
- Linux Fundamentals (HackTheBox), an article on Medium.
- Linux Basics Course Introduction, part 1 by HACKPHILES, a video in Malayalam.