Cheatsheet

Bash Commands Cheat Sheet – Linux & Terminal Reference

Essential bash commands for navigation, file management, process control, networking, and scripting. Copy any command with one click.

Navigation

pwdPrint current working directory
ls -laList all files with details (including hidden)
cd ~Go to home directory
cd -Switch back to previous directory
pushd <dir>Push directory onto stack and navigate to it
popdPop top directory from stack and navigate to it

File Operations

cp -r <src> <dst>Copy directory recursively
mv <src> <dst>Move or rename file/directory
rm -rf <path>Delete file or directory (no confirmation)
mkdir -p <path>Create directory (and parents) if not exists
touch <file>Create empty file or update timestamp
ln -s <target> <link>Create a symbolic link
find . -name "*.js" -type fFind files by name pattern
find . -mtime -7Find files modified in the last 7 days

Viewing Files

cat <file>Print entire file to stdout
less <file>Page through a file (q to quit)
head -n 20 <file>Show first 20 lines
tail -f <file>Follow file as it grows (great for logs)
grep -r "pattern" .Search for pattern recursively in current dir
wc -l <file>Count lines in file

Permissions

chmod 755 <file>rwxr-xr-x — owner full, others read+execute
chmod +x <file>Make file executable
chmod -R 644 <dir>Set 644 recursively on directory
chown user:group <file>Change file owner and group
umask 022Set default file creation permissions mask

Process Control

ps auxList all running processes
topInteractive process viewer
kill <PID>Send SIGTERM to process
kill -9 <PID>Force kill (SIGKILL) a process
pkill -f <name>Kill process by name pattern
<command> &Run command in background
nohup <command> &Run immune to hangup, keep running after logout
jobsList background jobs in current shell
fg %1Bring job 1 to foreground

Networking

curl -fsSL <url>Fetch URL silently, fail on error
wget -O <file> <url>Download URL to file
ping -c 4 <host>Ping host 4 times
netstat -tulpnShow listening ports and processes
ssh user@hostConnect to remote host via SSH
scp <file> user@host:/pathSecurely copy file to remote
rsync -avz <src> user@host:/dstSync files to remote efficiently

Archives

tar czf archive.tar.gz <dir>Create gzipped tar archive
tar xzf archive.tar.gzExtract gzipped tar archive
tar tzf archive.tar.gzList contents without extracting
zip -r archive.zip <dir>Create zip archive
unzip archive.zip -d <dir>Extract zip to directory

Bash Scripting

#!/bin/bashShebang — use bash to run the script
$1 $2 $@Positional args: first, second, all args
if [ "$x" = "y" ]; then ... fiConditional block
for i in *.txt; do echo "$i"; doneLoop over files
while read line; do ...; done < fileRead file line by line
$?Exit code of last command (0 = success)
set -euo pipefailExit on error, undefined variable, or pipe failure

Bash Scripting Basics

A bash script is just a text file with a #!/bin/bash shebang on the first line. Make it executable with chmod +x script.sh and run it with ./script.sh.

Variables and Quoting

Assign with name="value" (no spaces around =) and reference with $name or ${name}. Always double-quote variable expansions to prevent word splitting: "$var".

Defensive Scripting

  • Add set -euo pipefail at the top to catch errors early
  • Quote all variables to handle filenames with spaces
  • Test scripts with bash -n script.sh to syntax check without running
  • Use shellcheck for static analysis of bash scripts