Skip to content

Instantly share code, notes, and snippets.

View allthingscode's full-sized avatar

Matthew Hayes allthingscode

View GitHub Profile
@allthingscode
allthingscode / Bash Colors
Created January 28, 2011 21:32
Dumps a nicely formatted table with all bash colors
#!/bin/bash
#
# This file echoes a bunch of color codes to the
# terminal to demonstrate what is available. Each
# line is the color code of one foreground color,
# out of 17 (default + 16 escapes), followed by a
# test use of that color on all nine background
# colors (default + 8 escapes).
#
@allthingscode
allthingscode / Common Tar Commands
Created January 28, 2011 21:39
This is a nice reference for common tar commands - including working with tar.gz files.
#!/bin/bash
#
# Create/Upack Tar File
tar -cvf dir.tar dir/
tar -xvf dir.tar
# Create/Uncompress Tar.gz File
tar -cvzf dirs.tar.gz ./dir1 ./dir2
tar -xzf dirs.tar.gz
@allthingscode
allthingscode / Compare Mysql Databases
Created January 28, 2011 21:42
This script compares the record counts for 2 different mysql servers and creates a CSV report with the differences. This is useful for verifying that a master and slave server are the same. This does not verify the actual data at the 2 servers, it just co
#!/usr/bin/perl
#
# This script compares table counts in all databases
# from 2 servers.
# This is useful for finding replication issues.
#
use strict;
use warnings;
use DBI;
@allthingscode
allthingscode / Detect Samba Locks
Created January 28, 2011 21:44
Sometimes it can be frustrating when you need to delete/rename/move a file via a samba file share, but you can't because it's locked by someone else. This script attempts to look up the IP address of all samba users that are currently using the file. S
#!/bin/bash
#
SCRIPT=${0##*/}
if [[ $# -ne 1 ]] ; then
echo "usage: $SCRIPT filename"
exit 192
fi
@allthingscode
allthingscode / Disk Usage
Created January 28, 2011 21:45
This simply prints out the number of files and amount of space they are taking up. This can be useful for determining what is eating up all your hard-drive space.
#!/bin/bash
#
#
printf "%s %s %s\n" "There are `find "$1" -type f | wc -l` regular files" \
"taking up `du -sh "$1"|awk '{print $1}'` space" \
"in folder "$1""
exit 0
@allthingscode
allthingscode / Egrep to Vim
Created January 28, 2011 21:46
This script is an exact alias to egrep except that instead of printing out the files, it opens them in VIM sequentially.
#!/bin/bash
#
shopt -s -o nounset # Option Explicit
# =============================================================================
for ITEM in `egrep -l $* 2> /dev/null | sed -e 's/ /[SPACE]/g'` ; do
# unescape spaces
declare FILEPATH
FILEPATH=`echo $ITEM | sed -e 's/\[SPACE\]/ /g' -e 's/\n//g'`
@allthingscode
allthingscode / Find Duplicate Files
Created January 28, 2011 21:47
This script identifies duplicate files using their md5sum hash value.
#!/bin/bash
#
# lsdup.sh
#
# create a hash table and look up items
#
# This script identifies duplicate files using their md5sum hash value.
# This can be done much easier in a language that supports hash lists such as PERL. I have done it in a shell script as both a proof-of-concept
# and to demonstrate the basic concept of storing/retrieving hash values and dealing with collisions.
#
/**
* @param unsortedRecords The array that needs to be sorted. This array must be in the typical
* format that our arrays are in after retrieving database data (i.e. array of records).
* @param sortKeys The record keys that we'll be sorting on.
* If this is an array, multi-column sorting will be done.
* @param sortDirections The direction to sort the columns.
* If this is an array, it should match up with the sort_keys array.
* @param sortTypes The type of sorting that should be done (SORT_NUMERIC, SORT_STRING, etc. )
* If this is an array, it should match up with the sort_keys array.
* @example To sort records by 'last_name' in descending order:
#!/bin/bash
#
# Extract File Extension
file_ext=${filename_or_path##*.}
# Extract File Name From Full Path
# This returns the file name given a full path:
file_name=${full_path_var##*/}
#!/bin/bash
#
for item in `find ./ -type f | sed -e 's/ /[SPACE]/g'` ; do
# unescape spaces
file_path=`echo $item | sed -e 's/\[SPACE\]/ /g'`
fi