Skip to content

Instantly share code, notes, and snippets.

@haze83
Last active February 27, 2024 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haze83/58d1da97177cf2ba7858431bdd0f5e38 to your computer and use it in GitHub Desktop.
Save haze83/58d1da97177cf2ba7858431bdd0f5e38 to your computer and use it in GitHub Desktop.
CLI

CLI

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
# 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/
# find in current and subdirs by name and ls -l the searchresults
find . -name "*Searchstring*" -exec ls -l {} \;
# 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 ./
# 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"}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment