Skip to content

Instantly share code, notes, and snippets.

@gdhaworth
gdhaworth / most_recently_modified_dir.sh
Created September 6, 2012 04:57
An embeddable command that will find the most recently modified directory within the working directory.
#!/bin/sh
ls -ldt * | head -n 1 | tr -s ' ' | cut -d ' ' -f 9-
@gdhaworth
gdhaworth / custom_git_prompt.sh
Last active December 14, 2015 15:18
Uses git-prompt.sh (bundled with git) to show a color-coded branch name and dirty status in the prompt text if executing within a Git repository.
__colorize_arg ()
{
local color=''
case "$1" in
# Other options: AM, AM/REBASE, CHERRY-PICKING, BISECTING
*'|MERGING'* | *'|REBASE'*) # Merging or rebasing
color="0;31" # Red
;;
*'*+'*) # Unstaged + staged
@gdhaworth
gdhaworth / mq-autosync.sh
Last active December 21, 2015 02:48
A quick script to help run the Mercurial AutoSync Extension (bitbucket.org/obensonne/hg-autosync/wiki/Home) in daemon mode. This is useful for situations where someone wants to manage AutoSync's lifecycle, including stopping the service; an example might be within a virtualenv's activate script.
#!/bin/sh
_echo_usage_and_abort() {
echo "Usage: $0 {start|stop} [interval]"
exit 1
}
if [[ ! -d '.hg' ]]; then
cat <<EOM
This directory does not appear to be a mercurial repository. Try running this
@gdhaworth
gdhaworth / auto-open-screenshot.sh
Created September 28, 2013 01:56
Automatically open a screenshot for viewing after it is taken and delete after it is closed. Requires inotify-tools. This is written and tested on Gnome with Shotwell, probably can be adapted to other systems by changing the screenshot filename pattern (line 16) and the photo viewing application (line 18).
#!/bin/bash
if [[ -z "$1" ]]; then
_monitor_dir="$HOME/Pictures"
else
_monitor_dir="$1"
fi
if [[ ! -d "${_monitor_dir}" ]]; then
echo "Can't find directory '${_monitor_dir}', does it exist?"
exit 1
@gdhaworth
gdhaworth / lsof-proc-count.sh
Last active December 24, 2015 16:28
Count processes using files inside a directory, going only 3 subdirectories deep. A lil crazy, I know.
watch -n 1 "lsof -F pan /mnt/gentoo 2> /dev/null | grep -Ev '^p' | sed -r '/a./{N;s/^a(.)\nn/\1/}' | cut -d / -f 1,5-8 | grep -Ev '^\s*$' | cut -c 1,3- --output-delimiter=' ' | sort | uniq -c | sed -r 's/ ([0-9]) /0\1 /g' | sort -r"
@gdhaworth
gdhaworth / largest-zfs-snapshots.sh
Created August 11, 2014 05:38
Find the largest ZFS snapshots
zfs list -t snapshot | egrep --color=none '^\S+\s+[[:digit:]]+(\.[[:digit:]]+)?[GM]' | sort -rnk 2
<!--
Original snippet is from
http://www.communardo.de/home/techblog/2011/05/02/improve-the-confluence-pagetree-macro-functionality/
-->
<script type="text/javascript" >
AJS.toInit(function ($) {
var currentChildClicked = false;
AJS.bind("pagetree-children-loaded", function() {
if(!currentChildClicked) {
@gdhaworth
gdhaworth / vimpager_manpager.sh
Created October 29, 2014 21:49
Safe vimpager as manpager
export MANPAGER="/bin/sh -c \"col -b | vim -c 'set mouse=a ft=man ts=8 nomod nolist nonu noma' -c 'runtime! macros/less.vim' -\""
@gdhaworth
gdhaworth / ccminer-hashrate.sh
Last active August 29, 2015 14:15
A ridiculous way to get an average hashrate straight from the output of ccminer.
function hashrate() { echo $(($(fgrep 'Total: ' ${1}.out | egrep -o '[0-9]+ khash/s' | awk '{ print $1 }' | tail -n +2 | paste -sd+ | bc) / $(fgrep 'Total: ' ${1}.out | egrep '[0-9]+ khash/s' | tail -n +2 | wc -l))); }
@gdhaworth
gdhaworth / cloudwatch-metrics-to-splunk-inputs_conf.sh
Created October 28, 2015 21:26
Grabs all of the CloudWatch metric names and dimensions and prints them in Splunk's inputs.conf format for easy use with the Splunk AWS add-on. Requires awscli and jq.
#!/bin/bash -ex
exec aws cloudwatch list-metrics \
| jq -r '
.Metrics
| reduce .[] as $item ({}; .[$item.Namespace] |= {
names: (.names + [$item.MetricName] | unique),
dimensions: (.dimensions + [$item.Dimensions[].Name?] | unique)
})
| to_entries