Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Bash / Shell Cheat Sheet

Navigation and files

// move around
pwd; ls -la; cd dir; cd ..; cd ~; cd -
mkdir -p a/b; touch f.txt; cp -r src dst; mv old new; rm -rf dir
cat f; less f; head -n 10 f; tail -f log
find . -name "*.js"; du -sh *; df -h

Variables and strings

name="Asha"; echo "Hi $name"; echo "${name:0:2} ${#name} ${name^^}"
now=$(date +%F); export PATH="$HOME/bin:$PATH"
arr=(a b c); echo "${arr[0]} ${#arr[@]} ${arr[@]}"
file="a.tar.gz"; echo "${file%%.*} ${file##*.}"

Conditions and loops

if [[ -f file ]]; then echo yes; elif [[ -d dir ]]; then echo dir; else echo no; fi
(( n > 5 )) && echo big
case "$x" in a|b) echo ab ;; *) echo other ;; esac
for i in {1..5}; do echo $i; done
for f in *.txt; do echo "$f"; done
while read -r line; do echo "$line"; done < file

Functions and scripts

greet() { local n="${1:-you}"; echo "Hi $n"; }
set -euo pipefail
echo "$# args: $@"; exit 1
chmod +x script.sh && ./script.sh

Text processing

grep -rn "TODO" src; grep -i -c "error" log
sed -i 's/old/new/g' file
awk -F, '{ print $1, $3 }' data.csv
sort -nr | uniq -c | head
cut -d: -f1 /etc/passwd; tr 'a-z' 'A-Z'; wc -l file

Processes and system

ps aux | grep node; kill -9 PID; lsof -i :3000
cmd > out.log 2>&1 &; nohup cmd &; jobs; fg
chmod 755 f; chmod 600 .env; sudo chown user:group f
ssh user@host; scp f user@host:/path; curl -s URL

Full Bash / Shell documentation