Simple cli shell scripts for daily use
Network tools like dig, ping, myip.
Sync files between folders, remote to local and vice versa.
Usage samples for find and grep
Usage samples for tail with colorized output
# check for dns entries | |
dig -t ANY domain.tld @ns.domain.tld |
#!/bin/bash | |
# Facebook clear cache via Command Line | |
# create logs directory if its missing | |
/bin/mkdir -p logs | |
# via curl | |
/usr/local/bin/curl -X POST -F "id={object-url OR object-id}" -F "scrape=true" -o "logs/$(date +%Y-%m-%d_%H-%M).json" "https://graph.facebook.com" >/dev/null 2>&1 | |
# via wget | |
/usr/local/bin/wget --post-data="id={object-url OR object-id}&scrape=true" -O logs/$(date +%Y-%m-%d_%H-%M).json https://graph.facebook.com >/dev/null 2>&1 |
# list all linux processes with some extra information about its origin | |
ps -ef -A | |
# stop all processes matching node | |
# when -f is set, the full command line is used. | |
pkill -f node | |
# list all running processes | |
ps aux | |
# kill all processes named php81 | |
kill -9 $(pgrep -d' ' -f php81) |
# copy files from local to remote via ssh on a non-standard port | |
rsync -avzh -e "ssh -p2121" /path/to/local/ [email protected]:/path/to/remote/ | |
# rsync dirs | |
# show progress (--progress) and update only newer (--update) | |
# cd into the target directory and run | |
rsync -avzh --progress --update ~/source-dir/ . | |
# to delete files on the target directory | |
rsync -avzh --progress --update --delete ~/source-dir/ . | |
# to exclude multiple patterns | |
rsync -avzh --progress --update --delete ---exclude '*.min.*' --exclude 'cache' --exclude 'uploads' ~/source-dir/ . | |
# to include only specific filetypes | |
rsync -avzh --progress --update --delete --include='**/' --include='*.jpg' --exclude='*' ~/source-dir/ . | |
# for timestamp diffs between source and destination | |
# use --modify-window=[amount-of-time-in-seconds] | |
# see https://wiki.glitchdata.com/index.php?title=Rsync:_Modify_Window | |
rsync -avzh --progress --update --modify-window=1 ~/source-dir/ . | |
# for utf-8 filename conversion issues (macos to linux) | |
# try the option --iconv=utf-8,utf-8 or --iconv=utf-8-mac,utf-8 | |
# see https://serverfault.com/a/639367 and https://elliotli.blogspot.ch/2011/10/using-rsync-to-migrate-data-between.html | |
rsync -avzh --progress --update --iconv=utf-8-mac,utf-8 --modify-window=1 ~/source-dir/ remote.tld:~/dest-dir/ | |
# man page https://linux.die.net/man/1/rsync | |
# -a = archive | |
# -z = compress | |
# -u = skip files that are still new in the destination directory | |
# -h = human-readable | |
# -v = verbose | |
# -n = dry-run | |
# --ignore-existing = only sync files on the source that do not exist on the target | |
# --progress = show progress | |
# --stats = show stats | |
# --del = delete missing files on the target while transferring | |
# --exclude=.DS_Store = exclude files | |
rsync -azuhv --progress --stats --del --exclude='wp-config.php' --exclude='.htaccess' --exclude='.DS_Store' --exclude='wp-snapshots' --exclude='cache' --exclude='uploads' ~/source-dir/ remote.tld:~/dest-dir/ | |
# incremental backup | |
rsync -Cavz --no-o --no-g --omit-dir-times --delete --exclude=".DS_Store" ./ /destination/ |
# list files owned by current user | |
ll /tmp | grep $(whoami) | |
# find owned by current user | |
find /tmp -user $(whoami) -ls | |
# find in current and subdirs by name and ls -l the searchresults | |
find . -name "*Searchstring*" -exec ls -l {} \; | |
# find string in file and output filename | |
grep -lrs "wc_checkout_add_ons_position" ./ | |
# find a string current and subdirs (-R) and list matching filenames (-l) | |
grep -Rl "Searchstring" ./ | |
# find a string current and subdirs (-R) and list matching filenames with context (-i) | |
grep -Ri "Searchstring" ./ | |
# find a string current and subdirs (-R) and list matching filenames with context and line number (-n) | |
grep -Rn "Searchstring" ./ | |
# find a string current and subdirs (-R) matching filepattern (--include=*.scss) | |
# and list matching filenames with context and line number (-n) | |
grep --include=*.scss -Rn "Searchstring" ./ | |
# find string with variable-names prefixed with a dollar-sign like $variable-name and single-quotes to compare | |
# NOTE: you have to escape the dollar sign with three backslashes to prevent bash from using ENV variable --> "\\\$variable-name" | |
grep --include=*.scss -PRn "\\\$variable-name: '';" | |
# NOTE: for zsh you have to escape * in --include= otherwise it would throw "zsh: no matches found: --include=*.scss" | |
grep --include=\*.scss -PRn "\t[^\/]*\\\$variable-name: '';" | |
# looking for aria-hidden in themes/**/*.php | |
grep -Rion ".*aria-hidden.*" ./**/themes/**/*.php | |
# looking for [aria-hidden='true'] in .scss files | |
grep -Rion ".*\[aria-hidden='true'\].*" --include=\*.scss ./ | |
# recursive chmod for directories | |
find ./ -type d -print0 | xargs -0 chmod 755 | |
# recursive chmod for files | |
find ./ -type f -print0 | xargs -0 chmod 644 |
# https://makandracards.com/makandra/476071-bash-how-to-use-colors-in-your-tail-output | |
# https://blog.crythias.com/2008/04/awk-colorizer-for-tail.html | |
# https://sed.js.org/ | |
# colorize info, warning, error | |
tailc() { | |
tail -f -n 400 $1 | sed \ | |
-e 's/\(.*INFO.*\)/\x1B[34m\1\x1B[39m/i' \ | |
-e 's/\(.*WARNING.*\)/\x1B[33m\1\x1B[39m/i' \ | |
-e 's/\(.*ERROR.*\)/\x1B[31m\1\x1B[39m/i'; | |
} | |
# colorize date [yyyy-mm-dd*] | |
taild() { | |
tail -f -n 400 $1 | sed \ | |
-r -e 's/(\[([0-9]{4})-([0-9]{2})-([0-9]{2}).[^\ ]*)/\x1B[36m\1\x1B[39m/i'; | |
} | |
tail -f -n 400 file.log | sed \ | |
-r -e 's/(\[([0-9]{4})-([0-9]{2})-([0-9]{2}).[^\ ]*)/\x1B[36m\1\x1B[39m/i' | |
tail -f -n 400 debug.log | sed \ | |
-e 's/\(.*INFO.*\)/\x1B[34m\1\x1B[39m/i' \ | |
-e 's/\(.*WARNING.*\)/\x1B[33m\1\x1B[39m/i' \ | |
-e 's/\(.*ERROR.*\)/\x1B[31m\1\x1B[39m/i' | |
# filter output | |
tail -f -n 400 debug.log | awk ' | |
/INFO|info|Info/ {print "\033[34m" $0 "\033[39m"} | |
/WARNING|warning|Warning/ {print "\033[34m" $0 "\033[39m"} | |
/ERROR|error|Error/ {print "\033[31m" $0 "\033[39m"} | |
' |