Linux & Bash Cheatsheet
SysAdmin quick reference: file permissions, process monitoring, systemd service management, network triage, and text manipulation.
File System & Navigation
13 commandspwdbeginnerPrint the full path of your current working directory.
ls -lahbeginnerList files with permissions, owners, sizes, and hidden entries in a readable format.
cd /var/logbeginnerChange into another directory by absolute or relative path.
find . -type f -name "*.log"intermediateSearch recursively for files matching a name pattern.
find . -type f -mtime -1intermediateLocate files modified within the last day.
locate sshd_configbeginnerQuery the locate database for files by name almost instantly.
tree -L 2beginnerDisplay directory contents as a tree up to a chosen depth.
du -sh .beginnerShow the total size of the current directory in human-readable form.
du -sh * | sort -hintermediateRank files and folders by size to find what is consuming space.
df -hbeginnerShow mounted filesystem capacity, usage, and free space.
realpath ./script.shbeginnerResolve a relative path into its canonical absolute path.
stat /etc/passwdintermediateInspect detailed file metadata like size, inode, mode, and timestamps.
basename /var/log/nginx/access.logbeginnerExtract the filename portion from a full path.
File Operations
13 commandscp -av src/ backup/beginnerCopy files or directories while preserving attributes and showing progress.
cp -r templates/ releases/templates/beginnerRecursively copy a directory and everything inside it.
mv old-name.txt new-name.txtbeginnerRename a file or move it to another directory.
rm -rf build/intermediateForce-remove files or directories recursively.
mkdir -p logs/archive/2024/07beginnerCreate nested directories without failing if parents already exist.
touch deploy.logbeginnerCreate an empty file or update its modification timestamp.
touch -d "2024-01-01 00:00" audit.logintermediateSet a file timestamp manually for testing or restore workflows.
ln -s /opt/app/current /srv/www/appintermediateCreate a symbolic link that points to another file or directory.
ln existing.txt hardlink.txtadvancedCreate a hard link that references the same inode as the original file.
chmod 644 app.confbeginnerSet read-write for owner and read-only for group and others.
chmod u+x deploy.shbeginnerAdd execute permission for the file owner using symbolic mode.
chmod -R 750 scripts/intermediateRecursively apply restrictive directory and file permissions.
chown -R deploy:devops /srv/appintermediateChange file ownership recursively to a user and group.
Text Processing
14 commandscat /etc/os-releasebeginnerPrint a file to standard output exactly as stored.
grep -n "ERROR" app.logbeginnerSearch for matching text and show the line numbers for each hit.
grep -Rin "DATABASE_URL" .intermediateSearch recursively through a project for a string or pattern.
awk -F: '{print $1, $7}' /etc/passwdadvancedSplit input into fields and print selected columns or computed values.
sed -n '1,20p' file.txtintermediatePrint only a selected range of lines from a file.
sed 's/http:/https:/g' urls.txtintermediateReplace matching text in a stream using sed substitution syntax.
cut -d: -f1 /etc/passwdbeginnerExtract specific delimiter-separated fields from each input line.
sort -u hosts.txtbeginnerSort lines alphabetically and remove duplicates in one step.
uniq -c access.logintermediateCollapse adjacent duplicate lines and count their occurrences.
wc -l app.logbeginnerCount lines, words, or bytes in a file or stream.
head -n 20 server.logbeginnerShow the first lines of a file for quick inspection.
tail -f /var/log/nginx/access.logbeginnerFollow the end of a growing log file in real time.
diff -u old.conf new.confintermediateCompare two files and show unified diff output.
less +G /var/log/syslogbeginnerOpen a file in a pager for efficient scrolling, searching, and navigation.
Process Management
15 commandsps auxbeginnerList every running process with CPU, memory, user, and command details.
pgrep -af nodeintermediateFind process IDs and commands by pattern without scanning raw ps output manually.
topbeginnerOpen the built-in live process monitor for CPU and memory analysis.
htopbeginnerUse an interactive, colorized process viewer with easier sorting and filtering.
kill -15 1234intermediateSend SIGTERM so a process can shut down gracefully.
kill -9 1234intermediateForce-kill a stuck process using SIGKILL when graceful shutdown fails.
killall nginxintermediateSignal all processes with the same name at once.
nice -n 10 tar -czf backup.tar.gz /srv/dataadvancedStart a process with lower CPU scheduling priority.
nohup npm run start > app.log 2>&1 &intermediateRun a command immune to hangups so it keeps running after logout.
jobsbeginnerList background and stopped jobs in the current shell session.
bg %1intermediateResume a stopped shell job and continue it in the background.
fg %1intermediateBring a background or stopped job back into the foreground.
systemctl status nginxbeginnerCheck the current state, logs, and unit details for a systemd service.
systemctl restart nginxbeginnerRestart a service managed by systemd.
journalctl -u nginx -fintermediateFollow logs from a specific systemd service in real time.
Networking
13 commandsip addr showbeginnerDisplay IP addresses and status for all network interfaces.
ip route showintermediateInspect the kernel routing table and default gateway configuration.
ifconfig -abeginnerShow interface configuration using the legacy net-tools command set.
ping -c 4 8.8.8.8beginnerTest reachability and round-trip latency to a host.
curl -I https://example.combeginnerFetch HTTP headers to verify status codes, redirects, and server metadata.
wget -O backup.html https://example.combeginnerDownload a remote resource and save it to a chosen filename.
ss -tulpnintermediateList listening TCP and UDP sockets with associated processes.
netstat -tulpnintermediateDisplay active listening ports and bound services with the classic networking tool.
nmap -sV 192.168.1.10advancedScan a target host for open ports and detected service versions.
ssh devops@server.example.combeginnerOpen an encrypted remote shell session over SSH.
scp app.tar.gz devops@server:/srv/releases/intermediateSecurely copy files between local and remote systems over SSH.
rsync -avz ./site/ devops@server:/var/www/site/intermediateSynchronize files efficiently while transferring only deltas.
nc -vz localhost 5432intermediateTest whether a TCP port is reachable using netcat.
User & Permissions
13 commandswhoamibeginnerPrint the effective username of the current shell session.
idbeginnerShow the current user ID, group ID, and supplementary groups.
sudo -lintermediateList the commands the current user may run with sudo.
su - deployintermediateSwitch to another user account with a full login shell.
passwdbeginnerChange the password for the current account.
useradd -m -s /bin/bash aliceintermediateCreate a new user with a home directory and default Bash shell.
usermod -aG docker aliceintermediateAppend a user to an additional supplementary group.
userdel -r aliceintermediateDelete a user account and remove its home directory.
groupadd devopsintermediateCreate a new system group for shared access control.
groups alicebeginnerList all groups that a user belongs to.
chmod 755 deploy.shbeginnerSet owner read-write-execute and read-execute for group and others using octal mode.
chmod 640 /etc/myapp.envbeginnerRestrict a sensitive file to owner read-write and group read-only using octal permissions.
chown -R alice:devops /srv/appintermediateRecursively set both owner and group for an application directory.
Disk & Storage
14 commandsdf -hTbeginnerShow disk usage with human-readable sizes and filesystem types.
du -sh /var/logbeginnerMeasure how much space a directory consumes.
lsblk -fbeginnerList block devices, partitions, filesystems, and mount points.
mount | column -tintermediateDisplay all currently mounted filesystems in an easy-to-scan format.
mount /dev/sdb1 /mnt/dataintermediateMount a filesystem at a chosen directory so it becomes accessible.
umount /mnt/dataintermediateUnmount a filesystem cleanly before removing or reformatting it.
fdisk -ladvancedList disk partitions and geometry using the classic partitioning utility.
parted -ladvancedInspect partition tables with GNU Parted, including GPT layouts.
mkfs.ext4 /dev/sdb1advancedCreate an ext4 filesystem on a partition or block device.
tar -czf backup.tar.gz /etc/nginxbeginnerCreate a compressed tar archive from files or directories.
tar -xzf backup.tar.gzbeginnerExtract a gzip-compressed tar archive into the current directory.
gzip -k app.logbeginnerCompress a file with gzip while keeping the original copy.
zip -r artifacts.zip dist/beginnerCreate a ZIP archive recursively from a folder or file set.
unzip artifacts.zip -d restore/beginnerExtract a ZIP archive into a target directory.
Bash Scripting
15 commands#!/usr/bin/env bashbeginnerUse a portable shebang so the script runs with Bash from the current environment.
set -euo pipefailintermediateExit on errors, treat unset variables as failures, and catch pipe errors early.
ENVIRONMENT="production"beginnerAssign a shell variable without spaces around the equals sign.
echo "${ENVIRONMENT^^}"intermediateExpand a variable and transform it to uppercase using Bash string operations.
if [[ -f .env ]]; then echo "found"; fibeginnerRun commands conditionally when a file, string, or status test succeeds.
case "$1" in start|stop|restart) echo "ok" ;; *) exit 1 ;; esacintermediateDispatch different logic branches based on an input argument.
for file in *.log; do gzip "$file"; donebeginnerLoop over matching files and run the same command for each one.
while read -r host; do ping -c 1 "$host"; done < hosts.txtintermediateRead input line by line from standard input or a file safely.
sum() { echo "$(( $1 + $2 ))"; }intermediateDefine a reusable shell function that accepts positional parameters.
servers=("web-1" "web-2" "web-3")beginnerCreate a Bash array for managing multiple related values.
echo "${servers[@]}"intermediateExpand every array element as separate words.
command > out.log 2> err.logbeginnerRedirect standard output and standard error to different files.
command >> app.log 2>&1beginnerAppend both stdout and stderr to the same log file.
journalctl -u nginx | grep ERROR | tee errors.logintermediatePipe output through multiple commands and save a copy with tee.
(crontab -l; echo "0 2 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1") | crontab -advancedInstall a recurring cron job that runs a command on a fixed schedule.