UNIX Commands Cheat Sheet
Command Purpose Common Options
ls List directory contents -l (long), -a (all), -h (human), -R (recursive), -t (time)
ps Display running processes -e (all), -f (full), -u user, aux (detailed)
pwd Show current directory None
mv Move/Rename files mv file1 file2, mv files dir
cp Copy files -r (recursive), -i (interactive), -v (verbose)
touch Create file / update timestamp touch file1 file2
cat Display file contents -n (number lines), -E (show $)
time Measure execution time time command
cal Display calendar cal, cal year, cal month year
bc Calculator bc, bc -l (math lib)
sort Sort text -n (numeric), -r (reverse), -u (unique), -k (field)
diff Compare files diff f1 f2, -u (unified)
wc Word/line count -l (lines), -w (words), -c (bytes), -m (chars)
comm Compare sorted files comm file1 file2
ln Create links ln file link (hard), ln -s file link (soft)
du Disk usage -h (human), -s (summary), -a (all files)
kill Send signals kill -9 pid (force), kill -15 pid (terminate)
sleep Pause execution sleep 10
chmod Change permissions chmod 755 file, chmod u+x file
chown Change owner chown user file, chown user:group file
chgrp Change group chgrp group file
top Show processes interactive (k=kill, q=quit)
nice Set priority nice -n 10 cmd
renice Change priority renice +5 -p pid
cut Extract fields -d (delimiter), -f (field)
paste Merge lines paste f1 f2
grep Search text -i (ignore case), -r (recursive), -n (line no.)
file Identify type file filename
whereis Locate binary/docs whereis cmd
which Show command path which python
echo Print text/vars echo $PATH, echo 'Hello'
env Show env vars env
PATH Executable path echo $PATH, export PATH=$PATH:/new
CLASSPATH Java class path export CLASSPATH=.:/usr/local/lib
find Search files find / -name 'file', find . -size +10M
vi Editor
- Modes: Command, Insert, Last line (:)
- Commands: :w (save), :q (quit), :wq (save+quit), :q! (quit without save),
dd (delete line), yy (copy), p (paste), /word (search), u (undo)
Shell
- Interface between user & kernel
- Types: sh, bash, ksh, csh
- Executes commands & scripts
Wildcards
- * → zero or more chars
- ? → single char
- [abc] → a, b, or c
- [a-z] → range
Shell Script
- File of UNIX commands
- Start with shebang: #!/bin/bash
- Example:
#!/bin/bash
echo "Hello, World!"
- Run: chmod +x script.sh; ./script.sh