Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## One command to update them all
- function update ()
- {
- brew update
- brew upgrade
- gisty pull_all
- gisty sync_delete
- sudo gem update
- sudo pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs pip install -U -q
- }
- ## Get colors in manual pages
- function man()
- {
- env \
- LESS_TERMCAP_mb=$(printf "\e[1;31m") \
- LESS_TERMCAP_md=$(printf "\e[1;31m") \
- LESS_TERMCAP_me=$(printf "\e[0m") \
- LESS_TERMCAP_se=$(printf "\e[0m") \
- LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
- LESS_TERMCAP_ue=$(printf "\e[0m") \
- LESS_TERMCAP_us=$(printf "\e[1;32m") \
- man "$@"
- }
- ## Change directory to current Finger directory
- function cdf()
- {
- target=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'`
- if [ "$target" != "" ]; then
- cd "$target"; pwd
- else
- echo 'No Finder window found' >&2
- fi
- }
- ## Faster downloading with Aria
- function get()
- {
- aria2c -x 16 -s 16 "${1}"
- }
- ## Use a RSA private key in git
- function kit ()
- {
- trap 'rm -f /tmp/.git_ssh.$$' 0
- SSH_KEY="~/.ssh/id_rsa"
- echo "ssh -i $SSH_KEY \$@" > /tmp/.git_ssh.$$
- chmod +x /tmp/.git_ssh.$$
- export GIT_SSH=/tmp/.git_ssh.$$
- [ "$1" = "git" ] && shift
- git "$@"
- }
- ## Google Auth Token Generator
- function token ()
- {
- google-authenticator --force --time-based --disallow-reuse --rate-limit=3 \
- --rate-time=30 --window-size=10
- }
- ## Accept security dialogs until they no-longer exist
- function Accepts ()
- {
- osascript <<EOF
- tell application "System Events"
- repeat while exists (processes where name is "SecurityAgent")
- tell process "SecurityAgent" to click button "Allow" of window 1
- delay 0.2
- end repeat
- end tell
- EOF
- }
- ## Accept security dialogs until they no-longer exist, adds credentials
- function AcceptWithCreds ()
- {
- username="$1"
- password="$2"
- [ -z "${password}" ] && return 1
- osascript 2>/dev/null <<EOF
- set appName to "${username}"
- set appPass to "${password}"
- tell application "System Events"
- repeat while exists (processes where name is "SecurityAgent")
- tell process "SecurityAgent"
- if exists (text field 1 of window 1) then
- set value of text field 1 of window 1 to appName
- set value of text field 2 of window 1 to appPass
- end if
- end tell
- tell process "SecurityAgent" to click button "Allow" of window 1
- delay 0.2
- end repeat
- end tell
- EOF
- echo 'Finished...'
- }
- ## Sublime cannot open null files, so create them first
- function subl()
- {
- [ ! -f "$1" ] && {
- [ -z "$1" ] && {
- /bin/subl
- } || {
- touch "$1"
- }
- }
- /bin/subl "$1"
- }
- ## Completely forgot why i wrote this one....
- function iterate()
- {
- [ ! -z "$1" ] || [ ! -z "$2" ] && {
- while read line; do
- eval "$2"
- done < <(eval "$1")
- }
- }
- ## Explode a deb file
- function undeb()
- {
- ar p "$1" data.tar.xz |tar -Jxvf
- }
- ## Wrap common git commands
- function gush()
- {
- [ ! -z "$1" ] && {
- git add -A . ; git commit -m"$1" ; git push
- } || {
- echo 'Must use a commit message'
- }
- }
- ##
- # display a default hash, of md5 of input
- # Usage: qsh -> 22d84b304278ebbc5d5b86e9942d7414
- # Usage: qsh foo -> d3b07384d113edec49eaa6238ad5ff00
- # Usage: qsh $(date +"%T") -> ecfb1f72e032fef204eb2c3ecda70321
- ##
- function qsh ()
- {
- # Set default for variable
- val="$1" ; : ${val:="foobarbazbleh"} ; md5sum <<<"$val" |awk '{print $1}'
- }
- ##
- # high compression tarball of a directory or file
- # Usage: compress '/some/folder' -> folder.tar.xz
- # Usage: compress '/thefile.nfo' -> thefile.tar.xz
- ##
- function compress ()
- {
- shortname=$(basename "$1" | awk -NF '.' '{print $1}')
- XZ_OPT=-9 tar -Jcvf "${shortname}.tar.xz" "$1"
- }
- ##
- # send yourself something
- # Usage: push '/some/file.tar.gz' you@domain.com
- ##
- function push ()
- {
- # Raise sendmails size limit to 50mb
- # sudo echo -e '\n# unlimited size\nmessage_size_limit = 419430400' >> /etc/postfix/main.cf
- # sudo /etc/init.d/postfix restart
- echo ' ' | mutt -s "Received ${1}" -a "${1}" -- $2
- }
- ## Open a OSX Terminal tab
- function tab ()
- {
- osascript 2>/dev/null <<EOF
- tell application "System Events"
- tell process "Terminal" to keystroke "t" using command down
- end
- tell application "Terminal"
- activate
- do script with command "exec bash; cd \"$PWD\"; $*" in window 1
- do script with command "clear" in window 1
- end tell
- EOF
- }
- ## Quite terminal
- function close ()
- {
- osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "w" using command down'
- }
- #-------------------------------------------------------------
- # File & strings related functions:
- #-------------------------------------------------------------
- # Find a file with a pattern in name:
- function ff() { find . -type f -iname '*'"$*"'*' -ls ; }
- # Find a file with pattern $1 in name and Execute $2 on it:
- function fe() { find . -type f -iname '*'"${1:-}"'*' \
- -exec ${2:-file} {} \; ; }
- # Find a pattern in a set of files and highlight them:
- #+ (needs a recent version of egrep).
- function fstr()
- {
- OPTIND=1
- local mycase=""
- local usage="fstr: find string in files.
- Usage: fstr [-i] \"pattern\" [\"filename pattern\"] "
- while getopts :it opt
- do
- case "$opt" in
- i) mycase="-i " ;;
- *) echo "$usage"; return ;;
- esac
- done
- shift $(( $OPTIND - 1 ))
- if [ "$#" -lt 1 ]; then
- echo "$usage"
- return;
- fi
- find . -type f -name "${2:-*}" -print0 | \
- xargs -0 egrep --color=always -sn ${case} "$1" 2>&- | more
- }
- function swap()
- { # Swap 2 filenames around, if they exist (from Uzi's bashrc).
- local TMPFILE=tmp.$$
- [ $# -ne 2 ] && echo "swap: 2 arguments needed" && return 1
- [ ! -e $1 ] && echo "swap: $1 does not exist" && return 1
- [ ! -e $2 ] && echo "swap: $2 does not exist" && return 1
- mv "$1" $TMPFILE
- mv "$2" "$1"
- mv $TMPFILE "$2"
- }
- function extract() # Handy Extract Program
- {
- if [ -f $1 ] ; then
- case $1 in
- *.tar.bz2) tar xvjf $1 ;;
- *.tar.gz) tar xvzf $1 ;;
- *.bz2) bunzip2 $1 ;;
- *.rar) unrar x $1 ;;
- *.gz) gunzip $1 ;;
- *.tar) tar xvf $1 ;;
- *.tbz2) tar xvjf $1 ;;
- *.tgz) tar xvzf $1 ;;
- *.zip) unzip $1 ;;
- *.Z) uncompress $1 ;;
- *.7z) 7z x $1 ;;
- *) echo "'$1' cannot be extracted via >extract<" ;;
- esac
- else
- echo "'$1' is not a valid file!"
- fi
- }
- # Creates an archive (*.tar.gz) from given directory.
- function maketar() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
- # Create a ZIP archive of a file or folder.
- function makezip() { zip -r "${1%%/}.zip" "$1" ; }
- # Make your directories and files access rights sane.
- function sanitize() { chmod -R u=rwX,g=rX,o= "$@" ;}
- #-------------------------------------------------------------
- # Process/system related functions:
- #-------------------------------------------------------------
- function my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; }
- function pp() { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; }
- function killps() # kill by process name
- {
- local pid pname sig="-TERM" # default signal
- if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
- echo "Usage: killps [-SIGNAL] pattern"
- return;
- fi
- if [ $# = 2 ]; then sig=$1 ; fi
- for pid in $(my_ps| awk '!/awk/ && $0~pat { print $1 }' pat=${!#} )
- do
- pname=$(my_ps | awk '$1~var { print $5 }' var=$pid )
- if ask "Kill process $pid <$pname> with signal $sig?"
- then kill $sig $pid
- fi
- done
- }
- function mydf() # Pretty-print of 'df' output.
- { # Inspired by 'dfc' utility.
- for fs ; do
- if [ ! -d $fs ]
- then
- echo -e $fs" :No such file or directory" ; continue
- fi
- local info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') )
- local free=( $(command df -Pkh $fs | awk 'END{ print $4 }') )
- local nbstars=$(( 20 * ${info[1]} / ${info[0]} ))
- local out="["
- for ((j=0;j<20;j++)); do
- if [ ${j} -lt ${nbstars} ]; then
- out=$out"*"
- else
- out=$out"-"
- fi
- done
- out=${info[2]}" "$out"] ("$free" free on "$fs")"
- echo -e $out
- done
- }
- function my_ip() # Get IP adress on ethernet.
- {
- MY_IP=$(/sbin/ifconfig eth0 | awk '/inet/ { print $2 } ' |
- sed -e s/addr://)
- echo ${MY_IP:-"Not connected"}
- }
- function ii() # Get current host related info.
- {
- echo -e "\nYou are logged on ${BRed}$HOST"
- echo -e "\n${BRed}Additionnal information:$NC " ; uname -a
- echo -e "\n${BRed}Users logged on:$NC " ; w -hs |
- cut -d " " -f1 | sort | uniq
- echo -e "\n${BRed}Current date :$NC " ; date
- echo -e "\n${BRed}Machine stats :$NC " ; uptime
- echo -e "\n${BRed}Memory stats :$NC " ; free
- echo -e "\n${BRed}Diskspace :$NC " ; mydf / $HOME
- echo -e "\n${BRed}Local IP Address :$NC" ; my_ip
- echo -e "\n${BRed}Open connections :$NC "; netstat -pan --inet;
- echo
- }
- #-------------------------------------------------------------
- # Misc utilities:
- #-------------------------------------------------------------
- function repeat() # Repeat n times command.
- {
- local i max
- max=$1; shift;
- for ((i=1; i <= max ; i++)); do # --> C-like syntax
- eval "$@";
- done
- }
- function ask() # See 'killps' for example of use.
- {
- echo -n "$@" '[y/n] ' ; read ans
- case "$ans" in
- y*|Y*) return 0 ;;
- *) return 1 ;;
- esac
- }
- function corename() # Get name of app that created a corefile.
- {
- for file ; do
- echo -n $file : ; gdb --core=$file --batch | head -1
- done
- }
- function now()
- {
- date +"%Y-%m-%d @ %H:%M:%S"
- }
- function sizeof ()
- {
- du -h "$1" |tail -n1
- }
- # rewrite_commit_date(commit, date_timestamp)
- #
- # !! Commit has to be on the current branch, and only on the current branch !!
- #
- # Usage example:
- #
- # 1. Set commit 0c935403 date to now:
- #
- # rewriteCommitDate 1a888939
- #
- # 2. Set commit 0c935403 date to 1402221655:
- #
- # rewriteCommitDate 1a888939 1439311554
- #
- # date +"%s" -d "Tue Aug 11 09:45:54 PDT 2015 ## 1439311554
- rewriteCommitDate () {
- local commit="$1" date_timestamp="$2"
- local date temp_branch="temp-rebasing-branch"
- local current_branch="$(git rev-parse --abbrev-ref HEAD)"
- if [[ -z "$commit" ]]; then
- date="$(date -R)"
- else
- date="$(date -R --date "@$date_timestamp")"
- fi
- git checkout -b "$temp_branch" "$commit"
- GIT_COMMITTER_DATE="$date" git commit --amend --date "$date"
- git checkout "$current_branch"
- git rebase "$commit" --onto "$temp_branch"
- git branch -d "$temp_branch"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement