294 lines
7.2 KiB
Markdown
294 lines
7.2 KiB
Markdown
# Linux Essential Commands – Cheat Sheet
|
||
|
||
Each block groups related commands and includes inline comments so it can be copied and pasted as a whole.
|
||
|
||
---
|
||
|
||
## 1. Navigation & Help
|
||
|
||
```bash
|
||
# Show current directory
|
||
pwd
|
||
|
||
# List files (basic / long / including hidden)
|
||
ls
|
||
ls -l # long listing (permissions, owner, size, date)
|
||
ls -a # include hidden files (starting with .)
|
||
|
||
# Change directory
|
||
cd /path/to/dir
|
||
cd ~ # go to home directory
|
||
cd .. # go up one level
|
||
|
||
# Command help
|
||
man ls # full manual page
|
||
ls --help # quick help for a command
|
||
```
|
||
|
||
---
|
||
|
||
## 2. Files & Directories
|
||
|
||
```bash
|
||
# Create or update an empty file
|
||
touch file.txt
|
||
|
||
# Copy files and directories
|
||
cp source.txt dest.txt
|
||
cp -r src_dir/ dest_dir/ # copy directory recursively
|
||
|
||
# Move or rename
|
||
mv oldname.txt newname.txt
|
||
mv file.txt /target/dir/
|
||
|
||
# Remove files and directories
|
||
rm file.txt # remove file
|
||
rm -r dir_to_remove/ # remove directory recursively
|
||
rm -rf dir_to_force/ # force remove (use with care)
|
||
|
||
# Create directories
|
||
mkdir new_dir
|
||
mkdir -p a/b/c # create nested directories
|
||
```
|
||
|
||
---
|
||
|
||
## 3. Viewing & Searching Content
|
||
|
||
```bash
|
||
# View file content
|
||
cat file.txt # print whole file
|
||
less file.txt # scrollable view (quit with 'q')
|
||
|
||
# View start / end of a file
|
||
head file.txt # first 10 lines
|
||
head -n 20 file.txt # first 20 lines
|
||
tail file.txt # last 10 lines
|
||
tail -f logfile.log # follow log in real time
|
||
|
||
# Search text
|
||
grep "pattern" file.txt # search in single file
|
||
grep -r "pattern" /path/dir # recursive in directory
|
||
grep -ri "pattern" /path/dir # recursive and case-insensitive
|
||
```
|
||
|
||
---
|
||
|
||
## 4. Permissions & Ownership
|
||
|
||
```bash
|
||
# View permissions and ownership
|
||
ls -l /path/to/file_or_dir
|
||
|
||
# Change permissions
|
||
chmod 644 file.txt # owner read/write, group/others read
|
||
chmod 600 secret.txt # owner read/write only
|
||
chmod +x script.sh # add execute bit
|
||
|
||
# Change owner and group
|
||
chown user:group file.txt
|
||
chown -R user:group /path/to/dir # recursive
|
||
```
|
||
|
||
---
|
||
|
||
## 5. Users, Groups & sudo (Debian/Ubuntu style)
|
||
|
||
```bash
|
||
# Identify current user and groups
|
||
whoami
|
||
id # show UID, GID, and groups
|
||
|
||
# Run a command as root (if allowed)
|
||
sudo command # run single command with privileges
|
||
|
||
# User management (Debian/Ubuntu)
|
||
sudo adduser newuser # interactive user creation
|
||
sudo passwd newuser # set or change password
|
||
|
||
# Add user to an additional group
|
||
sudo usermod -aG sudo newuser # grant sudo rights (example)
|
||
sudo usermod -aG docker newuser # allow use of docker group
|
||
```
|
||
|
||
---
|
||
|
||
## 6. Processes & System Monitoring
|
||
|
||
```bash
|
||
# List processes
|
||
ps aux # all processes with details
|
||
|
||
# Real-time monitoring
|
||
top # built-in monitor
|
||
htop # improved top (if installed)
|
||
|
||
# Kill a process by PID
|
||
kill 1234 # ask process to terminate
|
||
kill -9 1234 # force kill (SIGKILL)
|
||
|
||
# Jobs in current shell
|
||
jobs # list background jobs
|
||
fg %1 # bring job 1 to foreground
|
||
bg %1 # send job 1 to background
|
||
```
|
||
|
||
---
|
||
|
||
## 7. Services & Logs (systemd)
|
||
|
||
```bash
|
||
# Service management (systemd)
|
||
sudo systemctl status nginx.service # check status
|
||
sudo systemctl start nginx.service # start service
|
||
sudo systemctl stop nginx.service # stop service
|
||
sudo systemctl restart nginx.service # restart service
|
||
sudo systemctl enable nginx.service # start at boot
|
||
sudo systemctl disable nginx.service # do not start at boot
|
||
|
||
# View logs with journalctl
|
||
sudo journalctl -u nginx.service # logs for one unit
|
||
sudo journalctl -u nginx.service -f # follow logs
|
||
sudo journalctl -b # logs from current boot
|
||
```
|
||
|
||
---
|
||
|
||
## 8. System Information & Resources
|
||
|
||
```bash
|
||
# Basic system info
|
||
uname -a # kernel and system info
|
||
hostname # system hostname
|
||
|
||
# Disk and filesystem usage
|
||
df -h # disk usage by filesystem
|
||
du -sh /path/to/dir # summary size of a directory
|
||
|
||
# Memory and swap usage
|
||
free -h
|
||
|
||
# Uptime and logged-in users
|
||
uptime
|
||
w # who is logged in and what they do
|
||
```
|
||
|
||
---
|
||
|
||
## 9. Networking Basics
|
||
|
||
```bash
|
||
# IP addresses and links
|
||
ip a # show interfaces and addresses
|
||
ip r # routing table
|
||
|
||
# Connectivity tests
|
||
ping 8.8.8.8 # test raw connectivity
|
||
ping example.com # test DNS + connectivity
|
||
|
||
# Open/listening ports
|
||
sudo ss -tulpn # TCP/UDP sockets with PIDs
|
||
|
||
# HTTP requests and downloads
|
||
curl https://example.com # simple HTTP GET
|
||
wget https://example.com/file.iso # download file
|
||
```
|
||
|
||
---
|
||
|
||
## 10. Storage & Mounts (Conceptual Commands)
|
||
|
||
```bash
|
||
# List block devices (disks/partitions)
|
||
lsblk # tree view of devices and mount points
|
||
|
||
# Show filesystems and usage
|
||
df -h # mounted filesystems
|
||
|
||
# Show current mounts
|
||
mount | grep "/"
|
||
|
||
# View /etc/fstab (persistent mounts)
|
||
sudo nano /etc/fstab # edit with care
|
||
```
|
||
|
||
---
|
||
|
||
## 11. Archives & Compression
|
||
|
||
```bash
|
||
# Create and extract tar archives
|
||
tar -cf archive.tar file1 dir2 # create tar
|
||
tar -xf archive.tar # extract tar
|
||
|
||
# Create and extract compressed tar.gz
|
||
tar -czf archive.tar.gz file1 dir2 # create tar.gz
|
||
tar -xzf archive.tar.gz # extract tar.gz
|
||
|
||
# Zip archives
|
||
zip archive.zip file1 file2 # create zip
|
||
unzip archive.zip # extract zip
|
||
```
|
||
|
||
---
|
||
|
||
## 12. Package Management (Debian / Ubuntu)
|
||
|
||
```bash
|
||
# Update package lists and upgrade
|
||
sudo apt update # refresh package index
|
||
sudo apt upgrade # upgrade installed packages
|
||
|
||
# Install, remove, purge packages
|
||
sudo apt install htop # install package
|
||
sudo apt remove htop # remove package (keep config)
|
||
sudo apt purge htop # remove package + config
|
||
|
||
# Search and inspect packages
|
||
apt search nginx # search in repositories
|
||
apt show nginx # show details
|
||
```
|
||
|
||
---
|
||
|
||
## 13. SSH & Remote Access
|
||
|
||
```bash
|
||
# Connect to a remote host
|
||
ssh user@server.example.com
|
||
|
||
# Use a specific key and port
|
||
ssh -i ~/.ssh/id_ed25519 -p 2222 user@server.example.com
|
||
|
||
# Copy files over SSH
|
||
scp file.txt user@server.example.com:/remote/path/
|
||
scp -r dir/ user@server.example.com:/remote/path/
|
||
```
|
||
|
||
---
|
||
|
||
## 14. Shutdown & Reboot
|
||
|
||
```bash
|
||
# Shutdown and reboot (systemd)
|
||
sudo shutdown now # immediate shutdown
|
||
sudo shutdown -r now # immediate reboot
|
||
sudo shutdown +5 # shutdown in 5 minutes
|
||
|
||
sudo reboot # reboot system
|
||
sudo poweroff # power off system
|
||
|
||
sudo systemctl reboot # reboot via systemd
|
||
sudo systemctl poweroff # power off via systemd
|
||
```
|
||
|
||
---
|
||
|
||
## 15. Shell Shortcuts (Bash)
|
||
|
||
- `Ctrl + C` – Cancel current command
|
||
- `Ctrl + D` – Logout / send EOF
|
||
- `Ctrl + R` – Search in command history
|
||
- `↑` / `↓` – Navigate command history
|
||
- `Tab` – Auto-completion
|