💻 Linux
Linux & Terminal
Commands, file system, permissions, processes, scripting, and SSH — your complete Linux reference.
📖 6 sections
⏰ 18 min read
✅ Quizzes included
01Essential Commands
BASHNavigation & files
# Navigation
pwd                   # print working directory
ls -la                # list all files with details
cd /path/to/dir       # change directory
cd ~                  # go to home
cd -                  # go to previous directory

# Files
cp source dest        # copy file
mv source dest        # move/rename
rm file               # delete file
rm -rf dir/           # delete directory (careful!)
mkdir -p a/b/c        # create nested dirs
touch file.txt        # create empty file
cat file.txt          # print file content
head -20 file.txt     # first 20 lines
tail -f log.txt       # follow log in real-time
less file.txt         # paginate file
02File Permissions
BASHchmod & chown
# Permission format: rwxrwxrwx (user/group/other)
# r=4, w=2, x=1
ls -la  # shows: -rw-r--r-- 1 user group 1024 Jan 1 file.txt

# chmod
chmod 755 file    # rwxr-xr-x (owner:all, others:r+x)
chmod 644 file    # rw-r--r--  (common for files)
chmod +x script.sh  # add execute for all
chmod u+x file    # add execute for user only
chmod -R 755 dir/ # recursive

# chown
chown user:group file   # change owner
chown -R user:group dir # recursive
sudo chown root file    # give to root

# sudo
sudo command      # run as root
su - username     # switch user
PermissionOctalMeaning
rwxr-xr-x755Owner: all. Group+Others: read+execute
rw-r--r--644Owner: read+write. Others: read only
rw-rw-r--664Owner+Group: read+write. Others: read
rwx------700Owner: all. No one else
rw-------600Owner: read+write only
03Searching & Filtering
BASHgrep, find, awk, sed
# grep: search text
grep "error" log.txt          # find "error" in file
grep -r "TODO" ./src          # recursive search
grep -i "error" log.txt       # case insensitive
grep -n "error" log.txt       # show line numbers
grep -v "debug" log.txt       # exclude lines with "debug"
grep -E "error|warning" log.txt  # regex: error OR warning

# find: search files
find . -name "*.js"           # all JS files
find . -type f -name "*.log"  # files only
find . -mtime -7              # modified in last 7 days
find . -size +10M             # files over 10MB
find . -name "*.log" -delete  # find and delete

# awk: process columns
awk "{print $1, $3}" file.txt # print col 1 and 3
awk -F"," "{print $2}" data.csv  # CSV, print col 2

# sed: stream editor
sed "s/old/new/g" file.txt    # replace all occurrences
sed -i "s/old/new/g" file.txt # in-place edit
04Processes & System
BASHProcess management
# View processes
ps aux                    # all running processes
ps aux | grep node        # find specific process
top                       # real-time process monitor
htop                      # better top (may need install)

# Kill processes
kill PID                  # graceful kill (SIGTERM)
kill -9 PID               # force kill (SIGKILL)
pkill node                # kill by name
killall node              # kill all matching

# Background jobs
command &                 # run in background
jobs                      # list background jobs
bg %1                     # send job 1 to background
fg %1                     # bring job 1 to foreground
nohup command &           # run after logout

# System info
df -h                     # disk space
du -sh ./folder           # folder size
free -h                   # memory usage
uname -a                  # system info
uptime                    # system uptime
❓ Quiz
What does chmod 755 do?
755 = 7(rwx) for owner, 5(r-x) for group, 5(r-x) for others. Common for executables and public directories.
05Bash Scripting
BASHShell script basics
#!/bin/bash
# First line: shebang specifies interpreter

# Variables
NAME="BitWithBite"
echo "Hello $NAME"
echo "Files: $(ls | wc -l)"  # command substitution

# If statement
if [ -f "file.txt" ]; then
  echo "File exists"
elif [ -d "dir/" ]; then
  echo "Is a directory"
else
  echo "Not found"
fi

# For loop
for i in {1..5}; do
  echo "Count: $i"
done

for file in *.html; do
  echo "Processing: $file"
done

# While loop
counter=0
while [ $counter -lt 5 ]; do
  echo $counter
  ((counter++))
done

# Functions
greet() {
  local name=$1
  echo "Hello, $name!"
}
greet "Ali"

# Run: bash script.sh OR chmod +x script.sh && ./script.sh
06SSH & Remote Servers
BASHSSH commands
# Connect
ssh user@hostname         # basic connection
ssh -p 2222 user@host     # custom port
ssh -i key.pem user@host  # with key file

# Copy files
scp file.txt user@host:/path/  # upload
scp user@host:/path/file .     # download
rsync -av ./local/ user@host:/remote/  # sync folders

# Key-based auth (more secure)
ssh-keygen -t rsa -b 4096      # generate key pair
ssh-copy-id user@host          # copy public key to server
# Now: ssh user@host (no password)

# Config file (~/.ssh/config)
Host myserver
  HostName 192.168.1.100
  User ubuntu
  IdentityFile ~/.ssh/mykey.pem
# Now: ssh myserver

# Port forwarding (tunnel)
ssh -L 3000:localhost:3000 user@host  # local port forward
ssh -R 80:localhost:3000 user@host    # reverse tunnel
💡
Always use SSH keys instead of passwords for servers. Disable password login in /etc/ssh/sshd_config: PasswordAuthentication no