Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Changing the Finder "Open With" contextual menu from the command line

The command line tool duti (http://duti.sourceforge.net) can be used to change default file-application launching associations as shown by the Finder "Open With" contextual menu. Changing the Finder "Open With" contextual menu means changing the default way we "open" documents on the command line as well.

The "lsregister" command can be used to dump or rebuild the Launch Services database.

export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export IFS=$' \t\n'


# download & install duti, MacVim and TextMate

# duti - set default UTI handlers
# cf. http://duti.sourceforge.net/documentation.php and
# http://developer.apple.com/macosx/uniformtypeidentifiers.html

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/duti/duti-1.3.0.pkg.tar.gz
tar -xzf duti-1.3.0.pkg.tar.gz
open -a Installer duti-1.3.0.pkg
stat -x /usr/local/bin/duti
duti -h
man duti


# MacVim, http://code.google.com/p/macvim/

cd ~/Desktop
curl -L -O http://macvim.googlecode.com/files/MacVim-7_2-stable-1_2.tbz
tar -xjf MacVim-7_2-stable-1_2.tbz
#open MacVim-7_2-stable-1_2.tbz    # uses BOMArchiveHelper (10.4) or Archive Utility (10.5) respectively
mv -i MacVim-7_2-stable-1_2 MacVim
mv -i ~/Desktop/MacVim /Applications


# TextMate, http://macromates.com

cd ~/Desktop
curl -L -O http://macromates.com/textmate/files/TextMate_1.5.7.dmg
hdiutil mount ~/Desktop/TextMate_1.5.7.dmg
cp -Ri '/Volumes/TextMate 1.5.7/TextMate.app' /Applications
hdiutil unmount '/Volumes/TextMate 1.5.7'
#/usr/bin/sudo /bin/ln -s /Applications/TextMate.app/Contents/Resources/mate /usr/local/bin/mate


# get the path to the "lsregister" command
locate lsregister | head -n 1
find /System/Library/Frameworks -type f -name lsregister -ls
find /System/Library/Frameworks/CoreServices.framework -type f -name lsregister -ls

alias lsregister="/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"

#/usr/bin/sudo /bin/ln -s `locate lsregister | head -n 1` /bin/lsregister   # alternative

# rebuild Launch Services database
#alias rlsdb='lsregister -kill -r -f -all system,local,user'    # Mac OS X 10.5; cf. http://9stmaryrd.com/2008/05/16/158
alias rlsdb='lsregister -kill -r -f -domain local -domain system -domain user'

rlsdb

lsregister -h

lsregister -dump | grep -i duti
lsregister -dump | grep -i MacVim
lsregister -dump | grep -i TextMate
lsregister -dump | grep -i TextEdit
lsregister -dump | grep -i Safari
lsregister -dump | grep -i helpviewer


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/  \1/p' | sort | uniq | nl
lsregister -dump | grep '[[:space:]]uti:' | awk '{ print $2 }' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*identifier:[[:space:]]+([^[:space:]].*)$/  \1/p' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*path:[[:space:]]+([^[:space:]].*\.app)$/  \1/p' | sort | uniq | nl


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'
lsregister -dump | sed -E -n -e 's/^.*identifier:[[:space:]]+([^[:space:]].*)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'
lsregister -dump | sed -E -n -e 's/^.*path:[[:space:]]+([^[:space:]].*\.app)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(public\..*)$/  \1/p' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*text.*)$/  \1/p' | sort | uniq | nl


alias utis="lsregister -dump | grep '[[:space:]]uti:' | awk '{ print \$2 }' | sort | uniq"
alias utis="lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/\1/p' | sort | uniq"
utis

utis | grep -i 'public\.' | nl

defaults read com.apple.LaunchServices


# ... or just use wsupdate from http://osxutils.sourceforge.net 
# move & copy the same file
unset -f mc
function mc() {
   if [[ $# -gt 1 ]] || [[ ! -e "$@" ]]; then return 1; fi
   tmpfile="${@}.tmp-${RANDOM}"
   printf "%s\n" "$tmpfile"
   /bin/mv "$@" "${tmpfile}"
   /bin/sleep 0.2
   /bin/cp -p "${tmpfile}" "$@"
   /bin/rm "${tmpfile}"
   return 0
}


unset -f mc
function mc() {
   if [[ $# -gt 1 ]] || [[ ! -e "$@" ]]; then return 1; fi
   tmpfile="/tmp/tmpfile.tmp-${RANDOM}"
   printf "%s\n" "$tmpfile"
   /bin/mv "$@" "${tmpfile}"
   /bin/sleep 0.2
   /bin/cp -Rp "${tmpfile}" "$@"
   /bin/rm -Rf "${tmpfile}"
   return 0
}



echo hello world > ~/Desktop/test.txt
echo hello world > ~/Desktop/test.html


# switch between Safari & Help Viewer as the default launching app for html files
open ~/Desktop/test.html


# cf. http://duti.sourceforge.net/documentation.php

printf "%s\n" 'com.apple.helpviewer   public.html   all' | duti     
#mc ~/Desktop/test.html
wsupdate ~/Desktop/test.html
open ~/Desktop/test.html

printf "%s\n" 'com.apple.Safari   public.html   all' | duti
wsupdate ~/Desktop/test.html
open ~/Desktop/test.html

rlsdb


# switch between TextEdit, MacVim & TextMate as the default launching app for plain text files

open ~/Desktop/test.txt

printf "%s\n" 'com.macromates.textmate   public.plain-text   all' | duti
open ~/Desktop/test.txt
wsupdate ~/Desktop/test.txt

printf "%s\n" 'org.vim.MacVim   public.plain-text   all' | duti
open ~/Desktop/test.txt    # type: ":q" or ":x" to quit test.txt
wsupdate ~/Desktop/test.txt

printf "%s\n" 'com.apple.TextEdit   public.plain-text   all' | duti
open ~/Desktop/test.txt
wsupdate ~/Desktop/test.txt



# cf. http://duti.sourceforge.net/documentation.php

/bin/cat > ~/Desktop/test.duti <<-'EOF'
com.apple.helpviewer   public.html   all
com.macromates.textmate   public.plain-text   all
EOF

# remove white space at line beginning
ed -s ~/Desktop/test.duti <<< $',s/^[[:space:]]*//\nw'

open -e ~/Desktop/test.duti

duti -v ~/Desktop/test.duti

wsupdate ~/Desktop/test.html
open ~/Desktop/test.html

wsupdate ~/Desktop/test.txt
open ~/Desktop/test.txt


#--------------------------------------------------


# GUI alternative to switch the default launching app for a single file
[right-click on a file] + [alt]  ->  "Always Open With"

# GUI alternative to change the default launching app for all files of the same kind
[right-click on a file] -> "Get Info" -> "Open with:" -> [select an app] -> "Change All ..."

List manual pages and system commands on Mac OS X

bash --version
man --version

man builtin
help   # list builtin commands


alias mycmds="alias -p | /usr/bin/awk -F= '{print \$1}' | /usr/bin/sort; declare -F | /usr/bin/awk '{print \$NF }' | /usr/bin/sort"
mycmds


# For more information on Bash shortcuts see:
# - Bash Shell Shortcuts, http://linuxhelp.blogspot.com/2005/08/bash-shell-shortcuts.html
# - Bash Magic Tricks, http://gnubie.blogspot.com/2007/07/bash-magic-tricks_18.html

[TAB + TAB]   # hitting the TAB key twice will list all available commands
auto[TAB + TAB]   # list all available commands with name auto*


# cf. Terminal Productivity Tips,
# http://www.afp548.com/article.php?story=20070525141734763
#echo "set show-all-if-ambiguous on" >> ~/.inputrc    # ... then open new Terminal window ...
#[TAB]    # hitting the TAB key once will list all available commands
#auto[TAB]    # list all available commands with name auto*


export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/libexec
export IFS=$' \t\n'

# For information on MANPATH see man man, man 5 man.conf
# and the specified MANPATH directories in: 
# sudo nano +40 /usr/share/misc/man.conf or 
# sudo nano +40 /private/etc/man.conf
# (make sure there is no MANPATH directory that is a symbolic link to another directory)

function mpath() { /usr/bin/man -W | /usr/bin/tr ':' '\n'; return 0; }
function mpath() { /usr/bin/man --path | /usr/bin/tr ':' '\n'; return 0; }   # alternative
function mpath() { /usr/bin/manpath | /usr/bin/tr ':' '\n'; return 0; }   # alternative
mpath

function path() { printf "%s\n" "${PATH}" | /usr/bin/tr ':' '\n'; return 0; }
function path() { printf -- "${PATH//:/\n}\n"; return 0; }   # alternative
path


man makewhatis
man whatis
man apropos
man periodic   # cf. nano /private/etc/periodic/weekly/500.weekly

# rebuild whatis database (index of manual pages)
# cf. http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/manpages.5.html
for dir in $(mpath); do find -x "${dir}" -type f -name whatis -ls; done
nano /usr/share/man/whatis
/usr/bin/sudo /usr/libexec/makewhatis.local $(/usr/bin/man --path)
/usr/bin/sudo /usr/libexec/makewhatis -v $(/usr/bin/man -W)   # alternative


# update locate database
# for a more secure locate see: /opt/local/bin/port info slocate
man locate locate.updatedb
/usr/bin/sudo /usr/libexec/locate.updatedb


# list manual pages
unset -f lsmans
function lsmans() {

   declare sudo find xargs basename sed
   sudo=/usr/bin/sudo
   find=/usr/bin/find
   xargs=/usr/bin/xargs
   basename=/usr/bin/basename
   sed=/usr/bin/sed

   for dir in $(/usr/bin/man -W | /usr/bin/tr ':' '\n'); do 
      #$sudo $find -x "${dir}" ! -type d 2>/dev/null
      $find -x "${dir}" \( -type f -or -type l \) -print0 2>/dev/null | $xargs -0 -n 3000 $basename
      #$find -x "${dir}" \( -type f -or -type l \) -print0 2>/dev/null | $xargs -0 -n 3000 $basename | $sed 's/\.[^\.]*$//'
   done

   return 0

}


mpath

lsmans
man -aW "*"

lsmans | wc -l
man -aW "*" | wc -l

man -aW  chmod
man -aW  "*chmod*"
lsmans | egrep -i chmod
apropos chmod
whatis chmod
locate -0 *chmod* | xargs -0 basename 
 
man -aW perl | nl
man -aW "*perl*" | xargs basename | nl
lsmans | egrep -i perl | nl
apropos perl | nl
whatis perl | nl

man -aW "*roff*" | xargs basename | nl
lsmans | egrep -i roff | nl
apropos roff | awk '{print $1}' | nl
whatis roff | nl

man -aW "*64*" | xargs basename | nl
lsmans | egrep -i 64 | nl
apropos 64 | awk '{print $1}' | nl

man -aW "*ssl*" | xargs basename | nl
lsmans | egrep -i ssl | nl
apropos ssl | awk '{print $1}' | nl

man -aW "*printer*" | xargs basename | nl
lsmans | egrep -i printer | nl
apropos printer | awk '{print $1}' | nl


lsmans | egrep -i '\.gz$'
lsmans | egrep -i util
lsmans | egrep -i sql
lsmans | egrep -i dns
lsmans | egrep "^ds[^A].*"
lsmans | egrep -i ssh
lsmans | egrep -i ssl
lsmans | egrep -i secur
lsmans | egrep -i ipsec
lsmans | egrep -i darwin
lsmans | egrep -i mac
lsmans | egrep -i apple
lsmans | egrep -i osx


#------------------------------


# list commands
unset -f lscmds
function lscmds() {

   declare sudo find xargs basename
   sudo=/usr/bin/sudo
   find=/usr/bin/find
   xargs=/usr/bin/xargs
   basename=/usr/bin/basename

   for dir in $(printf -- "${@:-${PATH}}" | /usr/bin/tr ':' '\n'); do 
      #$sudo $find -x "${dir}" \( -type f -or -type l \) -perm -555 2>/dev/null
      $find -x "${dir}" \( -type f -or -type l \) -perm -555 -print0 2>/dev/null | $xargs -0 -n 1000 $basename
      #$find -x "${dir}" \( -type f -or -type l \) -perm -555 -print0 2>/dev/null | $xargs -0 -n 1000 $basename
   done

   return 0

}


path

lscmds
lscmds | wc -l

lscmds /opt | egrep -i pdf
lscmds /usr | egrep -i pdf
#lscmds /opt | xargs -n 2000 basename | egrep -i pdf
lscmds /opt:/usr | egrep -i pdf

lscmds | egrep -i pdf | nl
apropos pdf | awk '{print $1}' | nl

lscmds | egrep -i roff | nl
apropos roff | awk '{print $1}' | nl

lscmds | egrep -i util
lscmds | egrep -i roff
lscmds | egrep -i html
lscmds | egrep -i "^ds.*"
lscmds | egrep -i x11
lscmds | egrep -i secur
lscmds | egrep -i file
lscmds | egrep -i mount
lscmds | egrep -i dns
lscmds | egrep -i ssh
lscmds | egrep -i ssl
lscmds | egrep -i system
lscmds | egrep -i darwin
lscmds | egrep -i mac
lscmds | egrep -i apple
lscmds | egrep -i osx

mycmds

help set
set -xv

#echo $-
#shopt -u
#shopt -s
#shopt -s | grep expand_aliases
#shopt -q expand_aliases; echo $?
#help shopt
#shopt -p
#shopt -s expand_aliases   

unalias mycmds1 mycmds2 mycmds3  2>/dev/null
alias mycmds1="declare -F | /usr/bin/awk '{print \$NF}' | /usr/bin/sort"
alias mycmds2="declare -F | /usr/bin/awk '{print $NF}' | /usr/bin/sort"
alias mycmds3='declare -F | /usr/bin/awk "{print $NF}" | /usr/bin/sort'

mycmds1
mycmds2
mycmds3


alias mycmds="alias -p | /usr/bin/awk -F= '{print \$1}' | /usr/bin/sort; \
              declare -F | /usr/bin/awk '{print \$NF }' | /usr/bin/sort"

mycmds

Add all new files to Subversion

This solution is shell-agnostic, unlike http://textsnippets.com/posts/show/450, and cleaner than to do a --force. I recommend adding it to a alias in your shell, i.e "sall".

svn st | grep "^?" | awk -F "      " '{print $2}' | xargs svn add


Can be modified to do a svn delete on files you accidentally rm -rf'ed:

svn st | grep "^!" | awk -F "      " '{print $2}' | xargs svn delete

Add all new files to Subversion

Bash script to add all new files to Subversion

for i in `svn st | grep ? | awk -F "      " '{print $2}'`; do svn add $i; done

Web bandwidth determination with awk

cat access_log | awk '{sum += $10} END {print int(sum/1024/1024),"MB"}'

zcat access_log.something.gz | awk '{sum += $10} END {print int(sum/1024/1024),"MB"}'

killing your own dispatch.fcgis

ps aux|grep youruser|grep dispatch|awk '{print $2}'|xargs kill -9

of course you can use this for any process and just change the 'dispatch' part

View processes, grep out by user and then kill all their PIDs

ps axu | grep user | kill -9 `awk{print $2}’`

Clearing out a bunch of spam with spoofed emails that were bounced back to some poor guy with a catchall email

We don't really want to delete them all just in case.

cd /usr/local/scratch/
mkdir junk
find /var/spool/postfix -exec grep "somediscernible-feature.com" '{}' \; | awk '{print($3)}' | xargs -J X mv X ./junk/


The "find" produces

Binary file /var/spool/postfix/active/D/D8832E38 matches
Binary file /var/spool/postfix/active/D/D78EC1C72 matches
Binary file /var/spool/postfix/active/D/D593D279D matches
Binary file /var/spool/postfix/active/D/D0EB32833 matches


The awk

/var/spool/postfix/active/D/D8832E38
/var/spool/postfix/active/D/D78EC1C72
/var/spool/postfix/active/D/D593D279D
/var/spool/postfix/active/D/D0EB32833


And then the mv, moves it.