Bash, Shell & Linux Cheat Sheet

A practical command-line reference for technical interviews, Linux navigation, scripting basics, file permissions, processes, networking, logs, and automation.

Files and Directories

Create / Move / Delete

File Operations

# Create empty file
touch notes.txt

# Create directory
mkdir projects

# Create nested directories
mkdir -p app/src/components

# Copy file
cp file.txt backup.txt

# Copy directory
cp -r folder backup-folder

# Move or rename
mv old.txt new.txt

# Remove file
rm file.txt

# Remove directory and contents
rm -r folder

Read Files

# Print entire file
cat file.txt

# View file page by page
less file.txt

# First 10 lines
head file.txt

# Last 10 lines
tail file.txt

# Follow file updates live
tail -f app.log

Permissions

chmod / chown

Permission Basics

  • r: read
  • w: write
  • x: execute
  • u: user/owner
  • g: group
  • o: others

Common Permission Commands

# Make script executable
chmod +x script.sh

# Owner can read/write/execute, group and others can read/execute
chmod 755 script.sh

# Owner can read/write, group and others can read
chmod 644 file.txt

# Change file owner
sudo chown user:user file.txt

# Show file permissions
ls -l

Permission Numbers

read    = 4
write   = 2
execute = 1

7 = 4 + 2 + 1 = rwx
6 = 4 + 2     = rw-
5 = 4 + 1     = r-x
4 = 4         = r--

Pipes and Redirection

Command Chaining

Pipes

# Pipe output from one command into another
cat app.log | grep "error"

# Count matching lines
grep "error" app.log | wc -l

# Sort unique IPs from log file
awk '{print $1}' access.log | sort | uniq

Redirection

# Write output to file
echo "hello" > file.txt

# Append output to file
echo "new line" >> file.txt

# Redirect errors
command 2> errors.txt

# Redirect output and errors
command > output.txt 2>&1

# Discard output
command > /dev/null 2>&1

Processes

ps / kill

Process Commands

# Show running processes
ps aux

# Search for a process
ps aux | grep nginx

# Interactive process viewer
top

# Better process viewer if installed
htop

# Kill process by PID
kill 1234

# Force kill process
kill -9 1234

# Kill by process name
pkill nginx

Background Jobs

# Run command in background
python3 server.py &

# Show background jobs
jobs

# Bring job to foreground
fg

# Stop current foreground process
Ctrl + Z

# Stop current command
Ctrl + C

Networking

Linux Networking

Network Commands

# Show IP addresses
ip addr

# Show routes
ip route

# Test connectivity
ping example.com

# DNS lookup
nslookup example.com

# Better DNS lookup if installed
dig example.com

# Show listening ports
ss -tulnp

# Test HTTP request
curl -I https://example.com

Logs and System Info

Troubleshooting

System Info

# Kernel and system info
uname -a

# Disk usage
df -h

# Directory size
du -sh folder

# Memory usage
free -h

# CPU info
lscpu

# Current user
whoami

# Logged-in users
who

Logs

# View system logs
sudo journalctl

# Recent logs
sudo journalctl -n 50

# Follow logs live
sudo journalctl -f

# Authentication logs
sudo tail -n 50 /var/log/auth.log

# Syslog
sudo tail -n 50 /var/log/syslog

Bash Scripting

Script Template

Basic Bash Script

#!/bin/bash

name="Xavier"

echo "Hello, $name"

if [ -f "file.txt" ]; then
  echo "File exists"
else
  echo "File does not exist"
fi

Variables and Arguments

#!/bin/bash

echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "All arguments: $@"
echo "Number of arguments: $#"

Loops

#!/bin/bash

for file in *.txt; do
  echo "Found file: $file"
done

count=1

while [ $count -le 5 ]; do
  echo "Count: $count"
  count=$((count + 1))
done

Functions

#!/bin/bash

greet() {
  local name="$1"
  echo "Hello, $name"
}

greet "Xavier"

Interview Notes

Important Concepts

  • Shell: program that interprets commands.
  • Bash: common Unix/Linux shell.
  • Process: running instance of a program.
  • PID: process identifier.
  • PATH: directories where commands are searched.
  • Environment variables: system-wide or session variables.
  • Exit code 0: success.
  • Non-zero exit code: error or failure.

Useful Interview Commands

# Show command location
which python3

# Show environment variable
echo $PATH

# Show previous command exit code
echo $?

# Make script executable
chmod +x script.sh

# Run script
./script.sh

# Run script with bash
bash script.sh

Common Troubleshooting Flow

  • Check if the service is running.
  • Check logs for errors.
  • Check permissions.
  • Check disk and memory usage.
  • Check network connectivity.
  • Check environment variables.
  • Restart only after understanding the issue.