Skip to content

Instantly share code, notes, and snippets.

@kematzy
Created March 29, 2012 08:58
Show Gist options
  • Save kematzy/2235186 to your computer and use it in GitHub Desktop.
Save kematzy/2235186 to your computer and use it in GitHub Desktop.
Default .bash_profile for use on web servers
# ~/.bash_profile: executed by bash(1) for login shells.
umask 002
# PS1='[\h:$PWD]$ '
# sets [ USER @ HOSTNAME RELATIVE PATH]$
export PS1="[\u@\[\e[32;1m\]\H \[\e[0m\]\w]\$ "
export LSCOLORS="exfxcxdxbxegedabagacad"
### ALIASES
## LISTING FILES AND DIRECTORIES
alias l='ls -1'
alias lsf="ls -lah"
alias lsd="ls -l"
alias la='ls -lhAF'
alias ll='ls -lhF'
alias lt="ls -lhtrF"
alias l.="ls -lhtrdF .*"
## DATE RELATED ALIASES
alias now=date
# alias now="date +%Y-%m-%d-%H%M%S"
alias today="date +%Y-%m-%d"
alias todaytime="date +%Y-%m-%d-%H%M"
## GENERAL STUFF
# convenience for recursive directories
alias md="mkdir -p"
# convenience alias for removing dirs
alias rmdir="rm -rf"
# make moving / renaming interactive
alias mv="mv -i"
alias symlink="ln -s"
alias sed='sed -E'
alias dir="lt"
## TRAVERSING DIRECTORIES
alias cd..="cd .."
alias cd...="cd ../.."
alias cd....="cd ../../.."
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
# go back to previous dir (Taken from SinatraRB IRC ht cypher23)
alias -- -='cd -'
# set the default shell editor
EDITOR="/usr/bin/pico"
# make it more Mac/TextMate friendly
alias mate="pico"
### FUNCTIONS
## BASH PROFILE EDITING AND RELOADING
# update .bash_profile after change
function u-bash {
source ~/.bash_profile; echo " ~/.bash_profile reloaded"
}
# update .bash_profile after change
function e-bash {
nano ~/.bash_profile;
}
## COMPRESSION AND EXTRACTION RELATED
# convenience method for tar.gz a folder
# => [DATE]-[FOLDERNAME].tar.gz [FOLDERNAME]
function kompress {
if [ -z $1 ] || [ "$1" = "." ]; then
echo "usage:
$FUNCNAME [name of directory to compress]
NB! You must be on the same level as the directory to compress
"
else
tar zcvf `date "+%Y-%m-%d-%H%M"`-$1.tar.gz $1;
# ensure the file has the correct permissions
chmod 644 `date "+%Y-%m-%d-%H%M"`-$1.tar.gz
fi
}
# convenience method to extract the contents of a tar.gz a file
function extract {
if [ -z $1 ] || [ "$1" = "." ]; then
echo "usage:
$FUNCNAME [name of .tar.gz file to extract]
NB! use unzip instead if you have a .zip file
"
else
tar -xzvf $1;
# ensure the output directory has the correct permissions
# chmod 755 $1
echo "
NB!! NOW ENSURE THE NEWLY CREATED DIRECTORY
HAVE THE CORRECT PERMISSIONS SET
chmod 755 directory-name
"
fi
}
# convenience method to show the calculated size of a directory
function dirsize {
du -ch $1 | grep total
}
### ADD / INCLUDE FURTHER FEATURES BELOW
# Load any exports & PATHs defined
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
#/EOF
#### GIT RELATED STUFF
### GIT ALIASES
alias gst='git status'
alias gl='git pull'
alias gp='git push'
alias gd='git diff | mate'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gb='git branch'
alias gba='git branch -a'
# function that creates a new git repos
function newgit() {
if [ -z $1 ]; then
echo "usage: $FUNCNAME project-name.git"
else
gitdir="$HOME/GITREPO/$1"
mkdir $gitdir
pushd $gitdir
git --bare init
git --bare update-server-info
# chmod a+x hooks/post-update
touch git-daemon-export-ok
popd
fi
}
#/GIT
#### ODD BITS STUFF
# convenience for finding out what process is active
# on a specified port
#
# whats-on-port 3456
function whats-on-port {
lsof -i TCP:$1
}
alias who-is-on-port="whats-on-port"
alias port-busy?="whats-on-port"
#/ ODD BITS
#### RUBY RELATED STUFF
### ALIASES
# use readline, completion and require rubygems by default for irb
alias irb='irb --readline -r irb/completion -rubygems'
### RUBYGEMS
# convenience for rebuilding the gems.YOURDOMAIN index
alias gems-rebuild-index="gem generate_index -d $HOME/gems.YOURDOMAIN/current"
# convenience for gem installs without docs & ri
function gem-install {
gem install --no-ri --no-rdoc $1
}
alias gems-install="gem-install"
# convenience method that lists outdated gems
function gems-outdated {
gem outdated > ~/.tmp/OutdatedGems.txt; less ~/.tmp/OutdatedGems.txt
}
alias gem-outdated="gems-outdated"
# convenience method that lists currently installed gems
function gems-installed {
gem list -l > ~/.tmp/InstalledGems.txt; less ~/.tmp/InstalledGems.txt
}
alias gem-installed="gems-installed"
#/RUBY
#### SECURITY RELATED STUFF
### ALIASES
## SET DEFAULT PERMISSIONS
# sets the default dir / file permissions
alias set-dir-perms="find . -type d -exec chmod 755 {} \; -print;"
alias set-file-perms="find . -type f -exec chmod 755 {} \; -print;"
alias set-perms="find . -type d -exec chmod 755 {} \; -print; find . -type f -exec chmod 755 {} \; -print;"
## CHECK LOGIN HISTORY
# NB!! the usernames printed by "last" truncate after 8 characters,
# so if you have a longer username you'll want to truncate yours
# in the grep string as well.
# returns the current USER's login history for the current month
alias check-logins="last -i | grep $USER"
# returns the current USER's login history for the prior month
alias check-logins2="last -if /var/log/wtmp.1 | grep $USER"
# returns the list of IP ADDRESSES only
alias check-logins-ip="last -if /var/log/wtmp.1 | grep $USER | awk '{print $3}' | sort | uniq -c"
#/NB!!
## INSECURE (WORLD-WRITABLE) DIRECTORIES
# World-writable directories will allow file writing by any user on the machine.
# These directories can be mass-scanned so this attack has been surfacing quickly.
# Even after you've checked all the above options this step must be performed.
# Even if you're sure you didn't make any permission mistakes, some less security-aware
# software vendors or plugin developers often use system commands or language-native
# permission-management functions to make some directories (usually ones used for caching
# and temporary files, session files etc) to ease installation and management.
# To scan for directories with world-writeable permissions use the UNIX find tool.
# If no results are displayed, then no folders are world-writeable.
alias dir777="find . -type d -perm -o=w;"
# FIXING WORLD-WRITABLE DIRECTORIES
#
# You can mass-change all your world-writable directories permission with the UINX find tool
alias dir777fix="find . -type d -perm -o=w -print -exec chmod 755 {} \;"
# find . -type d -perm -o=w -print -exec chmod 770 {} \;
# It's always better to enumerate all the world-writable directories and then
# deciding the proper permissions. Some folders require special attention.
### FUNCTIONS
# Dreamhost recommended CGI hacks check
function dh-cgi-hacks-test() {
if [ -z $1 ] || [ "$1" = "." ]; then
echo "usage:
$FUNCNAME [DomainName]
NB! use the full domain-name.ext
"
else
gunzip -c $HOME/logs/$1/http/access.log.* | gawk '{a[$7]++}END{for (i in a) {print a[i]"\t"i}}' | sort -n | more
fi
}
#/SECURITY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment