title: Shell
category: Linux & MacOS
time: 1483916848107
---
# Shell Scripts / Commands

## `sed`
Delete line with specific string:
```
sed -i '/sha/d' ./*.log
```

Delete line that not contain specific string:
```
sed -i '/sha\|cas/!d' ./*.log
```

## Check existing connections
*The most `netstat` command can be used on `ss` as well*
```
# Listening(-l) TCP(-t)/UDP(-u) Ports
netstat -tlnp | less
netstat -ulnp | less

# All Ports
netstat -nap | less
ss -nap | less
```

## IO Check:
```
iostat -m /dev/xvdb 1 | while read a; do echo "$a" | awk '/xvdb/ {print strftime("%Y-%m-%d %T")",", $2 }' >> $(date +'%Y-%m-%d')-iops.csv; done

# Or save it to a .csv files
iostat -m /dev/xvdb 1 | while read a; do echo "$a" | awk '/xvdb/ {sum += $2; n++; print strftime("%Y-%m-%d %T")",", sum / n",", $2 }' >> $(date +%Y-%m-%d)-iops-with-average.csv; done
```

## Some more useful commands
Kernal panic: `dmesg`
tail: `tail -n 5`
ls: `ls -lhS` (-S: ordering by size; -t: ordering by time)
grep: `grep -c` (-c: count only)


# Terminal Shotcut and Software's Commands
## tmux
Attach and deattach the remote session (-t to attach specific session)
```
tmux a -d -t Wordspace
```

# System Backup

## Rsync
```
rsync --archive --one-file-system --hard-links \
  --acls --xattrs --sparse \
  --human-readable --numeric-ids --delete --delete-excluded \
  --itemize-changes --verbose --progress \
  --exclude='*~' \
  --exclude-from=root.exclude \
  src dest
```

*Reference:*
- [System Backup - PhoenixWiki](https://wiki.phoenixlzx.com/page/system-backup/)

