| Intention | Commands |
|---|---|
| Make file non-edit/deletable even by root | chattr +i virgin.txt (change attribute, file immutable) |
| Monitor network statistics | apt-get install mrtg |
| Delete top secret file. -n overwrite 15 times, -z write 0s hide | shred -n 15 -z secret.txt |
| Suspend running process and move to background | ctrl+z |
| Copy files & directories hierarchically to another location | cpio |
| Who scheduled running jobs. Schedule jobs in future ‘at’ | at -l |
| View contents of tar ball without extracting. T display; verbose files | tar -tvf |
| Current run level | Who -r or runlevel |
| Find in directory files size > 10M | Find dirpath -size +10M |
| Find files not modified within 90, more than +12, exact 10 days 10 | Find dirpath -atime -90 |
| Case sensitive find . -iname “str” | find . -iname "str" |
| Count how many times ‘word’ in file | Grep -c ‘word’ file.txt |
| List all directories only | echo */ |
| print escapes --> ls -b | Do not show backup files --> ls -B |
| cat |
-help: 1 line; man: medium; info: full
| Create read-only file | Touch a.txt && chmod 400 a.txt |
|---|---|
| Get pid and kill (process status) | ps I grep java && kill $pid; fg pid→ foreground |
| ? space left in current drive | df (-h) |
| Replace all ‘a’ to ‘A’ | sed s/a/A/g file.txt |
| Ip address and hostname | nslookup |
| Delete tmp files using find | Find . -name “*.tmp”-print I xargs rm -rf |
| Find files contain exception | Find . -iname “*.txt” -print I xargs grep “Exception” |
| Search current directory only no sub | Find . -type f -cmin 15 -prune (-maxDepth 1) |
| Monitor system activities. ?CPU, port#, mem, pid, state | top |
|---|---|
| network connection details. Protocol, send/recv, local/foreign addr, state | netstat |
| Network bandwidth statistics | vnstat |
| Server status, which process ←→ file | lsof |
Word frequency
| Lower case char and white space only. Sorted by decreasing frequency. word.txt:the day is sunny the the the sunny is is |
|---|
| the 4 is 3 sunny 2 day 1 |
| cat words.txt I tr -s ' ' '\n' I sort I uniq -c I sort -r I awk '{ print $2, $1 }' |
| Tr -s: truncate str w only 1 remain instance; sort by strings for unique to count; Uniq -c: filter out repetitive successive lines. c → countSort -r: sort descending; awk: format |
Valid phone number
| Input: 987-123-4567 123 456 7890 (123) 456-7890 |
|---|
| Output:987-123-4567 (123) 456-7890 |
| -- ^...$ → start...end; \d{3}-\d{4} → 456-7890;(\d{3}- → 123-; I → OR; \(\d{3}\) → (123))/p print; -n suppress pattern space ;-r regularexpression |
| sed -n -r '/^([0-9]{3}-I\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/p' file.txt |
| grep -P '^(\d{3}-I\(\d{3}\) )\d{3}-\d{4}$' file.txt |
delete 7th line: sed -i ‘7d’ a.txt
| Print 10th line of file.txt |
|---|
| sed -n ‘10p’ file.txt //10th, print; without n, print all lines + 10th line |
| awk 'NR==10' file.txt //NR: current row#; default is {print $0}awk'FNR==10 {print }'file.txt |
First 3 lines: head -3 a.txt;
| Transpose file.txt. Each row has same # of columns, each field is separated by ' ' character. |
|---|
| name age alice 21 ryan 30 |
| name alice ryan age 21 30 |
| NF: # of fields; NR: # of records variable; FILENAMEFNR: # of records relative to current record END part will run after it dealt with every line. |
| awk '{ for (i = 1; i <= NF; i++) { if(NR == 1) { s[i] = $i;} else { s[i] = s[i] "" $i;} } } END { for (i = 1; s[i] != ""; i++) { print s[i];} }' file.txt |
Unix: multi-user multitasking-optimized OS run on various hardware platforms. Core components from same vendor, more stable and consistent for enterprise.