Cheatsheets
Bash / shell
Shell commands & operators for files, search, pipes, and processes.
37 entries
Navigation & files8
ls -lahList all files, long format, human sizes
cd -Switch to the previous directory
cp -r src dstCopy a directory recursively
mv a bMove or rename
rm -rf dirRemove a directory and contents (careful!)
mkdir -p a/b/cCreate nested directories
ln -s target linkCreate a symbolic link
du -sh * | sort -hSize of each item, sorted
Search & filter8
grep -rn "text" .Recursive search with line numbers
grep -i / -v / -ECase-insensitive / invert / regex
find . -name '*.ts'Find files by name
find . -type f -mtime -1Files modified in the last day
sort | uniq -c | sort -rnCount and rank unique lines
awk '{print $1}'Print the first column
sed -i 's/old/new/g' fileIn-place replace
cut -d, -f2Take the 2nd CSV field
Pipes & redirects7
cmd > file / >> fileRedirect stdout (overwrite / append)
cmd 2>&1Redirect stderr to stdout
cmd1 | cmd2Pipe stdout into the next command
cmd1 && cmd2 / ||Run next on success / on failure
find . -name '*.log' | xargs rmBuild commands from input
cmd | tee fileWrite to a file and stdout
diff <(cmd1) <(cmd2)Compare two command outputs
Variables & scripting6
#!/usr/bin/env bash
set -euo pipefailSafe script header (exit on error)
name="world"; echo "hi $name"Assign and expand a variable
${VAR:-default}Use a default if VAR is unset
$(command)Capture command output
for f in *.txt; do
echo "$f"
doneLoop over files
if [[ -f "$f" ]]; then
echo exists
fiTest and branch
Permissions & processes8
chmod +x fileMake a file executable
chmod 644 fileSet rw-r--r--
chown user:group fileChange owner and group
ps aux | grep nodeFind running processes
kill -9 <pid>Force-kill a process
lsof -i :3000Find what's using a port
nohup cmd &Run immune to hangups, in background
df -h / du -shDisk free / directory size