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!)

Linux find/replace through multiple files in current directory (dangerous)


sed -i.orig 's/hello/goodbye/g' *

MPlayer on Mac OS X

# MPlayer OSX Extended, http://mplayerosx.sttz.ch

cd ~/Desktop

curl -L -O http://mplayerosx.sttz.ch/downloads/MPlayer-OSX-Extended_rev7.dmg

hdiutil mount ~/Desktop/MPlayer-OSX-Extended_rev7.dmg

# test
open '/Volumes/MPlayerOSX Extended/MPlayer OSX Extended.app'
open -a '/Volumes/MPlayerOSX Extended/MPlayer OSX Extended.app' /Applications/iCal.app/Contents/Resources/alarmclock.mov


cd '/Volumes/MPlayerOSX Extended'
/usr/bin/sudo /bin/cp -R 'MPlayer OSX Extended.app' /Applications

hdiutil unmount '/Volumes/MPlayerOSX Extended'


ls -ld '/Applications/MPlayer OSX Extended.app'
ls -Rl '/Applications/MPlayer OSX Extended.app'
find '/Applications/MPlayer OSX Extended.app' -ls | nl


/usr/bin/sudo /bin/ln -is '/Applications/MPlayer OSX Extended.app/Contents/Resources/External_Binaries/mplayer.app/Contents/MacOS/mplayer' /usr/local/bin/mplayer

mplayer --help
mplayer -vo help
mplayer -pphelp


# cf. http://www.selflinux.org/selflinux/html/mplayer.html
mplayer -input keylist
mplayer -input cmdlist


ls -l /usr/local/bin/mplayer
otool -L /usr/local/bin/mplayer
otool -L /usr/local/bin/mplayer | egrep -i 'aa[lib]*'


# cf. Watch Videos in ASCII Art, http://oreilly.com/pub/h/4441
# (requires mplayer to be compiled with AAlib support)
# mplayer -vo aa video.avi

# http://www.apple.com/trailers/
mplayer -fs -monitoraspect 4:3 'http://movies.apple.com...mov'

# cf. http://www.linuxtutorialblog.com/post/tutorial-playing-around-with-mplayer
mplayer -fs -monitoraspect 4:3 -cache 8192 -cache-min 25 -channels 6 'http://movies.apple.com...mov'


# one-file-per-line format for playlist file
# cf. http://www.mplayerhq.hu/DOCS/man/en/mplayer.1.html

mplayer -playlist ~/Desktop/playlist -fs -monitoraspect 4:3 -really-quiet -fixed-vo -colorkey 0x000000



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



# Unoffical MPlayer OS X SVN Builds, http://www.haque.net/software/mplayer/mplayerosx/builds/
# cf. http://www.mplayerhq.hu/design7/dload.html and http://www.mplayerhq.hu/DOCS/HTML/en/macos.html

export IFS=$' \t\n'

export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/lib:/opt/local/include:\
/opt/local/man:/usr/local/bin:/usr/local/sbin:/usr/local/lib:/usr/local/include:/usr/bin:/bin:/usr/sbin:/sbin

alias sudo=/usr/bin/sudo


cd ~/Desktop
curl -L -O http://www.haque.net/software/mplayer/mplayerosx/builds/MPlayer-dev-SVN-latest.dmg
hdiutil mount ~/Desktop/MPlayer-dev-SVN-latest.dmg
cd /Volumes/MPlayer
/usr/bin/sudo /bin/cp -R 'MPlayer OSX.app' /Applications
hdiutil unmount /Volumes/MPlayer
cd


# test
open '/Applications/MPlayer OSX.app'
open '/Applications/MPlayer OSX.app' /Applications/iCal.app/Contents/Resources/alarmclock.mov


# create /usr/X11
# after installing MacPorts (includes X11)
# cf. http://trac.macports.org/wiki/InstallingMacPorts

sudo ln -s /usr/X11R6 /usr/X11


# freetype

sudo port install freetype
sudo mv /usr/X11R6/lib/libfreetype.6.dylib /usr/X11R6/lib/libfreetype.6.dylib.old
sudo ln -s /opt/local/lib/libfreetype.6.dylib /usr/X11R6/lib/libfreetype.6.dylib


# fontconfig

sudo port install fontconfig
sudo mv /usr/X11R6/lib/libfontconfig.1.dylib /usr/X11R6/lib/libfontconfig.1.dylib.old
sudo ln -s /opt/local/lib/libfontconfig.1.dylib /usr/X11R6/lib/libfontconfig.1.dylib


find /usr/X11R6/lib -name "*.old" -ls

# test
open '/Applications/MPlayer OSX.app'
open '/Applications/MPlayer OSX.app' /Applications/iCal.app/Contents/Resources/alarmclock.mov


sudo ln -is /Applications/MPlayer\ OSX.app/Contents/Resources/External_Binaries/mplayer.app/Contents/MacOS/mplayer /usr/local/bin/mplayer

mplayer --help



# undo ln commands above

sudo rm /usr/X11

sudo rm /usr/X11R6/lib/libfreetype.6.dylib
sudo mv /usr/X11R6/lib/libfreetype.6.dylib.old /usr/X11R6/lib/libfreetype.6.dylib

sudo rm /usr/X11R6/lib/libfontconfig.1.dylib
sudo mv /usr/X11R6/lib/libfontconfig.1.dylib.old /usr/X11R6/lib/libfontconfig.1.dylib

sudo rm /usr/local/bin/mplayer


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


function playurl() {

   declare website="" mp4_url=""


#   mp4_url="$(/usr/bin/curl -s "$@" | /usr/bin/sed -E -n -e '/video_id=.*\&fmt_map=\&t=/{s/^.*(video_id=[^\&]+).*\&fmt_map=(\&t=[^\&]+).*$/http:\/\/www.youtube.com\/get_video\?fmt\=18\&\1\2/p;q;}')"

#   mp4_url="$(/usr/bin/curl -L -s --max-time 10 "${lines[${i}]}" | /usr/bin/egrep -o -m 1 'video_id=.+\&fmt_map=\&t=[^\&]+' | \
#            /usr/bin/sed -E -n -e 's/^(video_id=[^\&]+).*\&fmt_map=(\&t=[^\&]+)$/http:\/\/www.youtube.com\/get_video\?fmt\=18\&\1\2/p')"


   # for "printf ... 2>/dev/null | ..." see:  "Why does bash sometimes say 'Broken pipe'?",
   # http://www.unixguide.net/unix/bash/E2.shtml

   # video_id=...&fmt_map=...&t=...
   mp4_url="$(printf "%s\n" "${website}" 2>/dev/null | /usr/bin/egrep -o -m 1 'video_id=.+\&fmt_map=.*\&t=[^\&]+' | \
            /usr/bin/sed -E -n -e 's/^(video_id=[^\&]+).*\&fmt_map=.*(\&t=[^\&]+)$/http:\/\/www.youtube.com\/get_video\?fmt\=18\&\1\2/p')"

   # fmt_map=...&t=...&video_id=...
   if [[ -z "$mp4_url" ]]; then
      mp4_url="$(printf "%s\n" "${website}" 2>/dev/null | /usr/bin/egrep -o -m 1 'fmt_map=.+\&t=.+video_id=[^\&]+' | \
               /usr/bin/sed -E -n -e 's/^fmt_map=.+(\&t=[^\&]+).+(video_id=[^\&]+)$/http:\/\/www.youtube.com\/get_video\?fmt\=18\&\2\1/p')"
   fi


   /usr/local/bin/mplayer -fs -monitoraspect 4:3 -cache 8192 -cache-min 25 -channels 6 "$mp4_url"
   #/usr/local/bin/mplayer -fs -monitoraspect 4:3 -cache 8192 -cache-min 60 -channels 6 -af volnorm -pp 6 "$mp4_url"

   return 0
}


playurl 'http://www.youtube.com/watch?v=xxxxxxxxxxx'


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


# scripting YouTube with Bash & mplayer

declare PLAYLIST="${HOME}/Movies/playlist.txt"
declare PLAYLIST_URLS_WEBSITE="${HOME}/Movies/playlisturls-website.txt"
declare PLAYLIST_URLS_FILE="${HOME}/Movies/playlisturls-file.txt"

declare website="" mp4_url=""


/bin/cat > "$PLAYLIST_URLS_WEBSITE" <<-'EOF'
http://www.youtube.com/watch?v=xxxxxxxxxx1
http://www.youtube.com/watch?v=xxxxxxxxxx2
http://www.youtube.com/watch?v=xxxxxxxxxx3
EOF


# cf. http://www.apple.com/trailers/

/bin/cat > "$PLAYLIST_URLS_FILE" <<-'EOF'
http://movies.apple.com...1_mov
http://movies.apple.com...2_mov
http://movies.apple.com...3_mov
EOF


open -e "$PLAYLIST_URLS_WEBSITE" "$PLAYLIST_URLS_FILE"


# read file into array
declare -a lines
OIFS=$IFS
IFS=$'\n'
lines=( $(< "$PLAYLIST_URLS_WEBSITE" ) )

#echo "${#lines[@]}"
#echo "${lines[@]}"


declare -a playurls

for ((i=0; i < "${#lines[@]}"; i++)); do 

#   mp4_url="$(/usr/bin/curl -L -s --max-time 10 "${lines[${i}]}" | /usr/bin/sed -E -n -e '/video_id=.*\&fmt_map=\&t=/{s/^.*(video_id=[^\&]+).*\&fmt_map=(\&t=[^\&]+).*$/http:\/\/www.youtube.com\/get_video\?fmt\=18\&\1\2/p;q;}')"

#   mp4_url="$(/usr/bin/curl -L -s --max-time 10 "${lines[${i}]}" | /usr/bin/egrep -o -m 1 'video_id=.+\&fmt_map=\&t=[^\&]+' | \
#            /usr/bin/sed -E -n -e 's/^(video_id=[^\&]+).*\&fmt_map=(\&t=[^\&]+)$/http:\/\/www.youtube.com\/get_video\?fmt\=18\&\1\2/p')"


   website="$(/usr/bin/curl -L -s --max-time 10 "${lines[${i}]}")"

   # video_id=...&fmt_map=...&t=...
   mp4_url="$(printf "%s" "${website}" 2>/dev/null | /usr/bin/egrep -o -m 1 'video_id=.+\&fmt_map=.*\&t=[^\&]+' | \
            /usr/bin/sed -E -n -e 's/^(video_id=[^\&]+).*\&fmt_map=.*(\&t=[^\&]+)$/http:\/\/www.youtube.com\/get_video\?fmt\=18\&\1\2/p')"

   # fmt_map=...&t=...&video_id=...
   if [[ -z "$mp4_url" ]]; then
      mp4_url="$(printf "%s" "${website}" 2>/dev/null | /usr/bin/egrep -o -m 1 'fmt_map=.+\&t=.+video_id=[^\&]+' | \
               /usr/bin/sed -E -n -e 's/^fmt_map=.+(\&t=[^\&]+).+(video_id=[^\&]+)$/http:\/\/www.youtube.com\/get_video\?fmt\=18\&\2\1/p')"
   fi


   playurls[${i}]="${mp4_url}"

done

#printf "%s\n" "${playurls[@]}"

# read file into array
declare -a playfiles
playfiles=( $(< "$PLAYLIST_URLS_FILE" ) )


IFS=$OIFS


printf "%s\n" "${playurls[@]}" > "$PLAYLIST"
printf "%s\n" "${playfiles[@]}" >> "$PLAYLIST"

open -e "$PLAYLIST"

/usr/local/bin/mplayer -playlist "$PLAYLIST" -fs -monitoraspect 4:3 -channels 6 -fixed-vo -colorkey 0x000000 -cache 8192 -cache-min 60

/usr/local/bin/mplayer -playlist "$PLAYLIST" -fs -monitoraspect 4:3 -really-quiet -channels 6 -fixed-vo -colorkey 0x000000 -cache 8192 -cache-min 30

/usr/local/bin/mplayer -playlist "$PLAYLIST" -fs -monitoraspect 4:3 -really-quiet -channels 6 -fixed-vo -colorkey 0x000000 -cache 8192 -cache-min 30 -af volnorm -pp 6


# no full-screen mode
/usr/local/bin/mplayer -playlist "$PLAYLIST" -monitoraspect 4:3 -channels 6 -fixed-vo -colorkey 0x000000 -cache 8192 -cache-min 60

# you may blacken your desktop background with:
# - doodim, http://www.lachoseinteractive.net/en/products/doodim/
# - MenuShade, http://www.nullriver.com/products
# - Cursorcerer, http://doomlaser.com/cursorcerer-hide-your-cursor-at-will/

ASCII art demo using AAlib

# See:
# - http://www.sveinbjorn.org/aalib
# - http://aa-project.sourceforge.net/aalib/
# - http://aa-project.sourceforge.net/aalib/aalib_toc.html
# - http://aa-project.sourceforge.net/bb/bb.html
# - Watch Videos in ASCII Art, http://oreilly.com/pub/h/4441
#   (requires mplayer to be compiled with AAlib support)


cd ~/Desktop

curl -L -O http://www.sveinbjorn.org/files/stuff/aalib-1.4.0.pkg.zip

unzip -qq aalib-1.4.0.pkg.zip

/usr/sbin/installer -pkg aalib-1.4.0.pkg -volinfo

/usr/bin/sudo /usr/sbin/installer -pkg aalib-1.4.0.pkg -target "/"
#/usr/bin/sudo /usr/sbin/installer -pkg aalib-1.4.0.pkg -target "/Volumes/Macintosh HD_2"


ls -l /usr/local/bin/bb /usr/local/include/aalib.h
ls -1 -l /usr/local/bin/aa*
ls -1 -l /usr/local/lib/*libaa*
ls -1 -l /usr/local/info/aalib*
ls -1 -l /usr/local/share/aalib*

/usr/bin/sudo /usr/sbin/chown root:wheel /usr/local/bin/bb /usr/local/include/aalib.h
/usr/bin/sudo /usr/sbin/chown root:wheel /usr/local/bin/aa*
/usr/bin/sudo /usr/sbin/chown root:wheel /usr/local/lib/*libaa*
/usr/bin/sudo /usr/sbin/chown root:wheel /usr/local/info/aalib* /usr/local/info/dir
/usr/bin/sudo /usr/sbin/chown root:wheel /usr/local/share/aalib*


/usr/bin/info aalib

/usr/local/bin/aatest
/usr/local/bin/aafire

# ASCII art demo
/usr/local/bin/bb --help
/usr/local/bin/bb

search password string within file

// description of your code here

strings <file> | grep ^<search string>

convert "man" help to pdf

man foo | enscript -o foo.ps | ps2pdf foo.ps [filename.pdf]

Clear linux buffer cache

// more info here
// http://www.linuxinsight.com/proc_sys_vm_drop_caches.html

echo 3 > /proc/sys/vm/drop_caches

Find largest files in Unix/Linux

find . -type f -printf "%k %p\n" | sort -rn | head

Compile & install git on Mac OS X

Requires Xcode.

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


# see what you already have
which asciidoc gpg gettext git
find /usr/lib /usr/local/lib -iname "*expat*"
python -V   # should be Python 2.4 or newer


# rudix
# http://rudix.org

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/rudix-1.5.2-0.dmg
hdiutil mount ~/Desktop/rudix-1.5.2-0.dmg
ls -1 /Volumes/* | egrep -i rudix
open -a Installer /Volumes/rudix/rudix.pkg
hdiutil unmount /Volumes/rudix

which rudix
man rudix


# expat
# also available via http://rudix.org

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/expat/expat-2.0.1.tar.gz
tar -xvzf expat-2.0.1.tar.gz 
cd expat-2.0.1
./configure --prefix=/usr/local
make
make check
sudo make install

find /usr/local -iname "*expat*"


# Python 2.5.2

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/python-2.5.2-1.dmg
hdiutil mount ~/Desktop/python-2.5.2-1.dmg
sudo rudix -i /Volumes/python/python.pkg
hdiutil unmount /Volumes/python

which python
python -V     # Python 2.5.2


# python-docs

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/python-docs-2.5.2-1.dmg
hdiutil mount ~/Desktop/python-docs-2.5.2-1.dmg
sudo rudix -i /Volumes/python-docs/python-docs.pkg
hdiutil unmount /Volumes/python-docs

man /usr/local/share/man/man1/python.1
ls -1 /usr/local/share/doc/python/*
find /usr/local/share/doc/python -mindepth 1 -maxdepth 1
find /usr/local/share/doc/python/doc -mindepth 1 -maxdepth 1
find /usr/local/share/doc/python/mac -mindepth 1 -maxdepth 1


# asciidoc
# asciidoc 8.3.0 requires Python 2.4 or newer

cd ~/Desktop
curl -L -O http://www.methods.co.nz/asciidoc/asciidoc-8.3.0.tar.gz
tar -xvzf asciidoc-8.3.0.tar.gz
cd asciidoc-8.3.0
open -e ~/Desktop/asciidoc-8.3.0/INSTALL
sudo ./install.sh

which asciidoc
man asciidoc


# GPG

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/gnupg-1.4.9-2.dmg
hdiutil mount ~/Desktop/gnupg-1.4.9-2.dmg
sudo rudix -i /Volumes/gnupg/gnupg.pkg
hdiutil unmount /Volumes/gnupg

which gpg
gpg --version
man gpg



# GetText

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/gettext-0.17-1.dmg
hdiutil mount ~/Desktop/gettext-0.17-1.dmg
sudo rudix -i /Volumes/gettext/gettext.pkg
hdiutil unmount /Volumes/gettext

which gettext
gettext --version
man gettext
open /usr/local/share/locale




# git 
# http://rudix.org

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/rudix/git-1.5.6.2-1.dmg
hdiutil mount ~/Desktop/git-1.5.6.2-1.dmg
sudo rudix -i /Volumes/git/git.pkg
hdiutil unmount /Volumes/git

which git
git --version
man git

sudo rudix -r git.pkg



# git
# http://git.or.cz

cd ~/Desktop
curl -L -O http://kernel.org/pub/software/scm/git/git-1.6.0.4.tar.gz
tar -xzf git-1.6.0.4.tar.gz
cd git-1.6.0.4
./configure
make
sudo make install

which git
git --version


# git man pages

cd ~/Desktop
curl -L -O http://kernel.org/pub/software/scm/git/git-manpages-1.6.0.4.tar.gz
sudo tar -C /usr/local/share/man -xzf git-manpages-1.6.0.4.tar.gz


git --help

git help add
git help bisect
git help shell
git help git-svn
git help git-daemon

git help gittutorial
git help gitcore-tutorial
git help gitglossary


cd ~/Desktop
mkdir -p ~/Desktop/git-test
cd ~/Desktop/git-test
openport 9418   # cf. http://codesnippets.joyent.com/posts/show/1747
git clone git://git.kernel.org/pub/scm/git/git.git
closeport 9418
open ~/Desktop/git-test
find ~/Desktop/git-test/git -mindepth 1 -maxdepth 1 -ls


# To configure git see:
# - http://dysinger.net/2007/12/30/installing-git-on-mac-os-x-105-leopard/
# - http://arthurkoziel.com/2008/05/02/git-configuration/
# - http://www.bergek.com/wp-content/uploads/2008/09/git-install.sh
# - http://git.or.cz/gitwiki/GitTips


References:

- Git - Fast Version Control System
- Wikipedia: Git
- Rudix
- Install git on Mac OS X 10.4 (expat, asciidoc)
- Installing GIT on Mac OS X 10.5 Leopard (GPG, GetText)
- Compiling Git and git-svn on OSX Tiger (git-svn, Perl)
- Getting git-svn working on the Mac
- Git vs. Mac HFS+ filesystem (UFS & git)
- git-osx-installer
- Git Configuration (on Mac OS X)
- GitTips
- Git User's Manual
- Tag: git
- Git Quick Reference
- Git Guide
- Git Internals: Source code control and beyond
- PeepCode Git screencast
- git hosting with Leopard
- Hosting Git repositories, The Easy (and Secure) Way
- The Git Community Book
- Setting up Git on OSX
- Git and Binaries
- Git cheat sheet
- Git SVN Workflow
- Setting up a new remote git repository
- Sharing git repositories via OS X's built-in web sharing
- GitHub Tips: Removing a Remote Branch
- Using git in the Finder
- GitX - a git GUI for Mac OS X

prevent script execution upload directories php shtml

// description of your code here

<Directory "/Library/MediaWiki/web/images">
   # Ignore .htaccess files
   AllowOverride None
 
   # Serve HTML as plaintext, don't execute SHTML
   AddType text/plain .html .htm .shtml
 
   # Don't run arbitrary PHP code.
   php_admin_flag engine off
 
   # If you've other scripting languages, disable them too.
</Directory>

md5 compute compare files

// description of your code here

md5sum <filename>