Basics of Linux
1. What is Linux?
Linux is an operating system kernel.
A complete Linux operating system usually consists of:
- Linux kernel
- Shell
- System utilities
- Package manager
- Libraries
- Applications
Examples of Linux distributions:
- Debian
- Ubuntu
- Fedora
- Arch Linux
- Alpine Linux
2. Terminal
The terminal lets you interact with Linux using commands.
pwd
Shows your current directory.
ls
Lists files.
cd /path/to/directory
Changes directory.
clear
Clears the terminal.
3. Files and Directories
Create a directory:
mkdir projects
Create a file:
touch hello.txt
Copy:
cp hello.txt backup.txt
Move or rename:
mv hello.txt hello.md
Delete:
rm hello.md
Delete a directory:
rm -r projects
4. Important Directories
Linux has a single filesystem tree starting at /.
/
├── bin → essential commands
├── boot → boot files
├── dev → devices
├── etc → configuration
├── home → user directories
├── lib → libraries
├── media → removable media
├── mnt → temporary mounts
├── opt → optional software
├── proc → process/kernel information
├── root → root user's home
├── run → runtime data
├── sbin → system commands
├── tmp → temporary files
├── usr → user applications/files
└── var → changing data
Your home directory is usually:
/home/username
Shortcut:
~
5. Absolute vs Relative Paths
Absolute path:
/home/marvin/projects
Relative path:
projects
Current directory:
.
Parent directory:
..
Home directory:
~
6. Reading Files
Print a file:
cat file.txt
Read interactively:
less file.txt
First lines:
head file.txt
Last lines:
tail file.txt
Follow a changing file:
tail -f app.log
7. Searching
Find files:
find . -name "*.txt"
Search text:
grep "hello" file.txt
Recursive search:
grep -r "hello" .
8. Permissions
Check permissions:
ls -l
Example:
-rwxr-xr--
Permissions are divided into:
owner group others
rwx r-x r--
Meaning:
r = read
w = write
x = execute
Change permissions:
chmod +x script.sh
9. Users
Show current user:
whoami
Show user information:
id
Run a command as root:
sudo command
Root has unrestricted system access.
10. Processes
List processes:
ps
More detailed:
ps aux
Interactive process viewer:
top
Terminate a process:
kill PID
Force termination:
kill -9 PID
11. Environment Variables
View a variable:
echo $PATH
Set a variable:
export NAME="value"
List environment variables:
env
PATH tells the shell where to look for executable programs.
12. Pipes
Commands can pass output to other commands.
ls | grep ".txt"
Example:
ps aux | grep nginx
The | operator sends the output of one command to another.
13. Redirection
Write output to a file:
echo hello > file.txt
Append:
echo world >> file.txt
Redirect errors:
command 2> error.log
Redirect everything:
command > output.log 2>&1
14. Shell
The shell interprets commands.
Common shells:
- Bash
- Zsh
- Fish
Check your shell:
echo $SHELL
Example:
ls -la
The shell:
- Reads the command
- Parses it
- Finds the program
- Starts the program
- Displays its output
15. Package Managers
Package managers install software.
Debian/Ubuntu:
sudo apt update
sudo apt install curl
Fedora:
sudo dnf install curl
Arch:
sudo pacman -S curl
16. Networking
Show network interfaces:
ip addr
Show routes:
ip route
Test connectivity:
ping example.com
Make HTTP requests:
curl https://example.com
Show listening ports:
ss -tulpn
17. Disk Usage
Show filesystem usage:
df -h
Show directory size:
du -sh directory
Find large files:
du -ah . | sort -h
18. Archives
Create a tar archive:
tar -cf archive.tar directory/
Extract:
tar -xf archive.tar
Create compressed archive:
tar -czf archive.tar.gz directory/
Extract:
tar -xzf archive.tar.gz
19. Services
Modern Linux systems commonly use systemd.
Check a service:
systemctl status nginx
Start:
sudo systemctl start nginx
Stop:
sudo systemctl stop nginx
Enable at boot:
sudo systemctl enable nginx
Restart:
sudo systemctl restart nginx
20. Logs
View system logs:
journalctl
Follow logs:
journalctl -f
Logs for a service:
journalctl -u nginx
21. Useful Commands
| Command | Purpose |
|---|---|
pwd |
Current directory |
ls |
List files |
cd |
Change directory |
mkdir |
Create directory |
touch |
Create file |
cp |
Copy |
mv |
Move/rename |
rm |
Delete |
cat |
Print file |
less |
Read file |
grep |
Search text |
find |
Find files |
chmod |
Change permissions |
sudo |
Run as root |
ps |
List processes |
kill |
Kill process |
ip |
Network configuration |
curl |
HTTP client |
df |
Disk usage |
du |
Directory usage |
systemctl |
Manage services |
journalctl |
Read systemd logs |
22. The Linux Mental Model
Think of Linux as:
Hardware
↓
Linux kernel
↓
System calls
↓
Programs / services
↓
Shell
↓
You
The kernel manages:
- CPU
- Memory
- Processes
- Filesystems
- Devices
- Networking
- Security
The shell is simply a program that lets you interact with the system.
23. Good Commands to Learn First
pwd
ls
cd
mkdir
touch
cp
mv
rm
cat
less
grep
find
chmod
sudo
ps
kill
df
du
curl
systemctl
journalctl
Master these first. Everything else builds on them.