TL;DR

Your everyday Linux reference: files, processes, users, text processing, and quick system checks. Use this page when you need to recall a flag, pattern, or safe one-liner during an incident or maintenance window.

Files & Directories

The most frequently used navigation and file inspection commands on any Linux host or container.

bashfiles.sh
# List with details; -a includes hidden, -h human sizes, -t sort by time
ls -lahtr /etc/

# Print working directory and change into it
pwd
cd /var/log/pods

# Recursive listing with file sizes
du -sh /var/log/*       # size of each item in /var/log
du -sh --max-depth=1 /  # top-level disk usage breakdown

# Find files by name, type, size, or modification time
find /etc -name "*.conf" -type f
find /var/log -name "*.log" -mtime -1   # modified in last 24 h
find / -size +100M -type f 2>/dev/null  # files over 100 MB

# Read files
cat /etc/os-release          # distro info
head -n 20 /var/log/syslog   # first 20 lines
tail -f /var/log/syslog      # follow in real time
tail -n 50 /var/log/kern.log
less +G /var/log/auth.log    # open at end; q to quit

# Copy, move, delete (confirm before rm -r in production)
cp -av src/ dst/             # copy recursively, verbose
mv oldname newname
rm -i file.txt               # -i prompts before each delete
rm -rf dir/                  # remove directory; be certain of path

Text Search & Processing

Use grep to find patterns in logs and configs; combine with awk, sed, sort, and uniq to transform output on the fly.

bashtext.sh
# grep: search for pattern in files
grep -rn "OOMKilled" /var/log/          # recursive, show line numbers
grep -i "error\|warn" /var/log/syslog   # case-insensitive, multiple patterns
grep -v "debug" app.log                 # exclude lines matching pattern
grep -A 3 -B 2 "FATAL" app.log          # 3 lines after, 2 lines before match

# awk: column extraction and arithmetic
awk '{print $1, $4}' access.log         # print columns 1 and 4
awk -F: '{print $1}' /etc/passwd        # use : as delimiter, print usernames
awk '$3 > 100 {print $0}' data.txt      # conditional: print rows where col3 > 100
awk '{sum+=$1} END{print sum}' nums.txt # sum a column

# sed: in-place substitution
sed 's/old/new/g' file.txt              # replace all occurrences, print to stdout
sed -i 's/old/new/g' file.txt           # edit file in place (careful in prod)
sed -n '10,20p' file.txt                # print lines 10–20

# sort, uniq, wc
sort -k2 -n data.txt                    # sort by column 2 numerically
sort -u file.txt                        # sort and deduplicate
uniq -c sorted.txt | sort -rn           # count occurrences, rank by frequency
wc -l file.txt                          # count lines
wc -c file.txt                          # count bytes

# cut: extract fields from delimited text
cut -d',' -f1,3 data.csv               # extract columns 1 and 3 from CSV
cut -d':' -f1 /etc/passwd               # extract usernames

Process Management

Find, inspect, and signal processes; useful when tracking down rogue containers, runaway scripts, or node-level resource consumers.

bashprocesses.sh
# Snapshot of all processes
ps aux                          # all users, user-oriented format
ps aux | grep kubelet           # filter for specific process
ps -eo pid,ppid,cmd,%cpu,%mem --sort=-%cpu | head -15  # top CPU users

# Interactive process viewers
top                             # press M to sort by memory, P by CPU
htop                            # friendlier; F5 for tree view (if installed)

# Find by name
pgrep -la kubelet               # list PIDs and names matching kubelet
pidof dockerd                   # get PID of a named binary

# Send signals
kill -15 <pid>                  # SIGTERM: graceful shutdown (try first)
kill -9 <pid>                   # SIGKILL: force kill (last resort)
pkill -f "python app.py"        # kill by full command match

# Background / foreground
nohup long-script.sh > out.log 2>&1 &  # run immune to hangup
jobs                            # list background jobs
fg %1                           # bring job 1 to foreground
bg %2                           # resume job 2 in background

# Process tree
pstree -p <pid>                 # show children of a process

Users & Permissions

Check who you are, switch users, and manage file permissions — important when debugging pod service-account mounts or host-level file access errors.

bashusers-perms.sh
# Identity checks
whoami                          # current username
id                              # uid, gid, and all groups
id <username>                   # check another user's IDs
groups                          # groups for current user

# Switch user / privilege escalation
sudo -i                         # root shell (if allowed)
sudo -u <user> <command>       # run command as another user
su - <user>                    # switch user (requires password)

# File permissions (symbolic and octal)
ls -la file.txt                 # shows permissions, owner, group
chmod 644 file.txt              # rw-r--r--: owner rw, others r
chmod 755 script.sh             # rwxr-xr-x: owner rwx, others rx
chmod -R 750 /opt/app/          # recursive
chown user:group file.txt       # change owner and group
chown -R app:app /data/         # recursive

# setuid / setgid / sticky bit
chmod u+s binary                # setuid: run as file owner
chmod +t /tmp                   # sticky: only owner can delete

# ACL (where supported)
getfacl file.txt                # show extended ACLs
setfacl -m u:appuser:r file.txt # grant appuser read access

Archives & Transfer

Compress and copy files between hosts; rsync is preferred over scp for directories because it is resumable and shows progress.

basharchives.sh
# tar: create and extract archives
tar -czf archive.tar.gz /path/to/dir/   # create compressed tar
tar -xzf archive.tar.gz                 # extract to current directory
tar -xzf archive.tar.gz -C /target/    # extract to specific directory
tar -tzf archive.tar.gz | head          # list contents without extracting

# rsync: efficient directory sync
rsync -avz /local/path/ user@host:/remote/path/   # sync to remote
rsync -avz --progress --delete src/ dst/           # delete extraneous destination files
rsync -avz --exclude='*.log' src/ dst/             # exclude pattern
rsync -n -avz src/ dst/                            # dry-run: show what would change

# scp: simple file copy over SSH
scp file.txt user@host:/tmp/
scp -r dir/ user@host:/tmp/

# curl: download files
curl -Lo /tmp/binary https://example.com/binary   # save with specified name
curl -fsSL https://get.helm.sh/helm-install.sh | bash

Environment & Shell

Manage environment variables, shell configuration, and aliases — essential for keeping kubectl contexts, cloud credentials, and tool paths straight across sessions.

bashenv.sh
# View and set environment variables
env                             # all current env vars
echo $HOME $PATH $USER
export MY_VAR="value"           # set for this session and child processes
unset MY_VAR                    # remove variable

# Persistent variables (add to ~/.bashrc or ~/.zshrc)
export KUBECONFIG=~/.kube/config
export AWS_PROFILE=dev
export PATH="$PATH:/usr/local/bin/custom"

# Source changes without restart
source ~/.bashrc
. ~/.zshrc

# Aliases for kubectl and cloud CLIs (add to profile)
alias k='kubectl'
alias kgp='kubectl get pods'
alias kns='kubectl config set-context --current --namespace'
alias kctx='kubectl config use-context'

# History: find previously used commands
history | grep kubectl
!1234           # re-run command 1234
!!              # re-run last command (use with caution as root)
Ctrl+R          # interactive reverse history search

# Shell options
set -e          # exit script on first error
set -o pipefail # catch errors in pipes
set -u          # error on undefined variables

Quick System Info

One-liners to orient yourself on an unfamiliar host before touching anything — kernel version, OS release, uptime, hostname, mounts.

bashsysinfo.sh
uname -a                        # kernel version and architecture
cat /etc/os-release             # distro name and version
hostnamectl                     # hostname, machine ID, OS, kernel, virt type
uptime                          # load averages and uptime duration
who                             # who is logged in
last | head -10                 # recent login history
date                            # current time (check timezone!)
timedatectl                     # NTP sync status

# CPU and memory summary
lscpu | grep -E "Model|Socket|Core|Thread|MHz"
free -h                         # memory: total, used, available
cat /proc/cpuinfo | grep "model name" | head -1

# Mounted filesystems
df -hT                          # disk space with filesystem type
findmnt                         # tree view of all mounts
mount | grep -i nfs             # find NFS mounts

# Open ports (see networking page for details)
ss -tlnp                        # TCP listening ports and owning processes

Common Gotchas

  • !rm -rf with wrong path: always echo the variable first; never use rm -rf $VAR/ if $VAR can be empty.
  • !sed -i on macOS: requires sed -i '' (empty string) instead of sed -i as on Linux.
  • !find -delete: test with find … -print before switching to -delete.
  • !kill -9: skips graceful shutdown; use SIGTERM (15) first and give the process time to clean up.
  • !tail -f across log rotation: use tail -F (capital F) to follow across log rotations.