Skip to content

Instantly share code, notes, and snippets.

@samba
samba / crypto.sh
Last active December 12, 2015 06:08
OpenSSL for file encryption
## to encrypt
openssl aes-256-cbc -e -in plaintext.txt -out crypted.blob
## to decrypt
openssl aes-256-cbc -d -in crypted.blob -out plaintext.txt
@samba
samba / replace.py
Last active December 13, 2015 17:19
PERL-style RegExp substitution in Python (but within "s/.../.../", uses Python syntax)
#!/usr/bin/env python
# Python support for PERL's shorthand of pattern substitution (s/.../.../)
# Supports flags:
# i => case insensitive
# m => multiline match (caret and dollar represent line terminations, not whole-string)
# s => "dot-all" - dot matches everything, including new-line
# g => "global" - replace every instance (without this, only the first)
import re
@samba
samba / tricks.sh
Last active December 15, 2015 17:49
Shell tricks
#!/bin/sh
# Show duplicate files (compared by MD5 hash)
find ./ -type f -print0 | xargs -0 md5sum | sort | uniq -w 32 -D
# Remove smart-quotes from a buffer/file
sed "s/[”“]/\"/g; s/[’‘]/'/g"
# Rename a series of files (e.g. restore backups) by regular expression in OS X
find ./ -type f -name '*.bak' -print | sed -E 's/(.*)\.bak$/mv "&" "\1"/' > rename.sh
@samba
samba / find-dirty-git.sh
Created April 9, 2013 17:28
Find dirty Git directories (i.e. projects with uncommitted changes)
#!/bin/sh
git_command_context () {
cd $1;
shift;
is_workdir=`git rev-parse --is-inside-work-tree`
is_gitdir=`git rev-parse --is-inside-git-dir`
is_basedir=`git rev-parse --is-bare-repository`
my_gitdir=`git rev-parse --git-dir`
git "--work-tree=`pwd`" "--git-dir=${my_gitdir}" "$@"
@samba
samba / struct.py
Created May 10, 2013 06:01
Python class for masking dictionaries as objects
# Generic data structure for mapping named arguments directly to properties
# More readable code than using dictionaries...
# Usage:
# mystruct = Struct(value1 = "x", value2 = "y", value3 = "z")
# print mystruct.value1
# >> x
class Struct(object):
def __init__(self, **entries):
Struct.update(self, entries)
@samba
samba / find-php-eval.sh
Last active December 17, 2015 08:28
Search for PHP files that call "eval" (that's bad).
#!/bin/sh
BASEDIR="${1:-./}"
listfiles () {
find ${BASEDIR} -type f -print0 | xargs -0 file -i -0 | grep -a "x-php" | cut -d '' -f 1
}
listfiles | xargs grep -l "eval("
@samba
samba / .vimrc
Last active December 17, 2015 09:39
Vim Configuration Redux
" This minimal vimrc is intended to be easily deployed to machines where my
" dotfiles are not already installed, while maintaining a basic feature set
" for usability.
" These directories must exist... @{
set backupdir=$HOME/.vim/backup " backup files location
set directory=$HOME/.vim/swap " swap files location
set tags=./tags,$HOME/.vim/tags " you probably want to add more to these later.
" }@
@samba
samba / backup.sh
Created September 11, 2013 08:29
Backup snapshot script using <fileconvoy.com> as rolling storage
#!/bin/sh
# This script creates encrypted tarballs and then
# 1) Emails them to you, or
# 2) Uploads them to <fileconvoy.com>, and emails you a link and decryption key
# Dependencies: curl, openssl, grep, tar
# Where to find the content to back up
BACKUP_ROOT=/var/backup
@samba
samba / lightamd.js
Last active February 11, 2024 22:20
Simplified AMD-style module framework
/* A lightweight module framework intended to provide (simplified)
* AMD-style module support.
*
* Currently its minified form yields 987 bytes, 577 after gzip compression.
*
* It DOES NOT parse module identifiers as paths (e.g. "/a/b" or "../a").
* It assumes that all module IDs are simple strings, and seeks an exact
* match, without attempting to navigate any hierarchy.
*
* It DOES NOT parse incoming modules as string for require() statements.
@samba
samba / pyprofile.sh
Last active August 29, 2015 14:00
Python profiling shortcut
#!/bin/sh
'''USAGE:
pyprofile.sh run yourscript.py {your arguments} # Runs your program & stores statistics
pyprofile.sh show # Reports your statistics
Within the report mode, use 'help' to learn additional commands.
Initial recommendation:
sort cumtime ncalls # List function calls by total time spent in each function
stats 30 # Show top 30 function calls (by sorted metric, descending)