It’s been a while since my last update, but as with any good IT guy I’ve been both too busy and too lazy to write anything. Anyway, I thought I’d make a note of some commands more for my own use that anyone else’s, but if someone else does find one useful then that’s a bonus.
Split a file at a word or pattern into multiple files:
awk '/Pattern to split at/{n++}{print > "split" n ".txt"}' FILE
Notes: Replace FILE with the file name you wish to run the command against.
Take a screenshot via SSH:
DISPLAY=:0.0 import -window root /path/to/directory/screenshot.png
Notes: None.
Search for something that looks like an e-mail address:
egrep -io '([[:alnum:]_.-]+@[[:alnum:]_.-]+?\.[[:alpha:].]{2,6})'
Run a query on multiple tables in a database matching a pattern
mysql -uUSER -pPASSWORD -D DATABASE_NAME -e "show tables" -s | egrep "SEARCH_PATTERN" | xargs -I "@@" mysql -uUSER -pPASSWORD -D DATABASE_NAME -e "DROP TABLE @@;"
Notes: Replace the markers where indicated (USER, PASSWORD etc) and obviously change the example query.
Command line screencast
mkfifo /tmp/fifo;(nc -q0 -k -l -p 5000 < /tmp/fifo > /dev/null &);script -f /tmp/fifo
Notes: Run nc ADDRESS 5000 to connect and watch.
Empty all log files
for file in $(find /var/log -type f); do > $file; done;
Notes: None.
Watch MySQL queries
watch -n 1 mysqladmin --user=USER --password=PASSWORD processlist
Notes: Replace the markers where indicated (USER, PASSWORD etc).
Find potential duplicate files
find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
Notes: None.