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

Find directories with trailing spaces

// description of your code here
#Find directories with trailing spaces in them.
find . -type d -iregex '.* '

Concatenate two arrays in Bash

array1=(1 2 3 4 5)
array2=(6 7 8 9 10)

#declare -i n=${#array1[@]}     # "last index + 1" is equal to the number of array items

declare -a indices=( ${!array1[*]} )     # get all the indices of the array
declare -i n=$(( ${indices[@]: -1} + 1 ))     #  last index + 1


for ((i=0; i < ${#array2[@]}; i++)); do
   array1[${n}]="${array2[${i}]}"
   let n+=1
done

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


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


array1=(1 2 3 4 5)
array2=(6 7 8 9 10)

array1=(1 2 3 4 5)
array2=(6 7 "a string" 9 10)

array1=(1 2 3 4 5)
array2=(6 7 $'a string\nwith a newline' 9 10)


array1=(1 2 3 4 5)
array1[10]="added at index 10 of array1"
echo ${!array1[*]}   # indices of array1
array2=(6 7 8 9 10)


array1=( "${array1[@]}" "${array2[@]}" )

for ((i=0; i < ${#array1[@]}; i++)); do 
   echo "${i}: ${array1[${i}]}"
done

Simple command line rss reader using XMLStarlet

See: The best in command line xml: XMLStarlet

# cf. http://trac.macports.org/wiki/InstallingMacPorts
sudo port install xmlstarlet

man xmlstarlet
xmlstarlet --help
xmlstarlet sel --help
open http://xmlstar.sourceforge.net


function readrss() {

   declare -a rss
   OIFS="$IFS"
   IFS=$'\n'
   set -f   # cf. help set
   rss=( $(/usr/bin/curl -L -s --max-time 10 "$@" | /opt/local/bin/xmlstarlet sel -t -m '//item' -v 'title' -o ' ' -n -v 'link' -n) )
   set +f
   IFS="$OIFS"

   declare n=0

   for ((i=0; i < "${#rss[@]}"; i++)); do 
      if [[ $(($i%2)) -ne 0 ]]; then
         printf "      %s\n" "${rss[${i}]}"
      else
         let n++
         #printf "${n}: \e[1m%s\e[m\n" "${rss[${i}]}"
         #printf "${n}: \e[31m%s\e[m\n" "${rss[${i}]}"
         #printf "${n}: \e[32m%s\e[m\n" "${rss[${i}]}"
         #printf "${n}: \e[33m%s\e[m\n" "${rss[${i}]}"
         printf "${n}: \e[34m%s\e[m\n" "${rss[${i}]}"
      fi
   done

   return 0
}


readrss 'http://codesnippets.joyent.com/rss'
readrss 'http://bashcurescancer.com/rss/'
readrss 'http://feeds.feedburner.com/Shell-fu'
readrss 'http://images.apple.com/main/rss/hotnews/hotnews.rss'
readrss 'http://images.apple.com/downloads/macosx/home/recent.rss'
readrss 'http://rss.cnn.com/rss/cnn_topstories.rss'
readrss 'http://rss.cnn.com/rss/money_topstories.rss'
readrss 'http://feeds.wsjonline.com/wsj/xml/rss/3_7011.xml'
readrss 'http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml'


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


# batch read rss

function breadrss() {

   set -- \
'http://codesnippets.joyent.com/rss' \
'http://bashcurescancer.com/rss/' \
'http://feeds.feedburner.com/Shell-fu' \
'http://images.apple.com/main/rss/hotnews/hotnews.rss' \
'http://images.apple.com/downloads/macosx/home/recent.rss' \
'http://rss.cnn.com/rss/cnn_topstories.rss' \
'http://rss.cnn.com/rss/money_topstories.rss' \
'http://feeds.wsjonline.com/wsj/xml/rss/3_7011.xml' \
'http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml'


   declare -a rss
   OIFS="$IFS"
   IFS=$'\n'
   set -f   # cf. help set

   while [[ $# -gt 0 ]]; do

      echo

      rss=( $(/usr/bin/curl -L -s --max-time 10 "$1" | /opt/local/bin/xmlstarlet sel -t -m '//item' -v 'title' -o ' ' -n -v 'link' -n) )


      printf "\e[1m%s\e[m\n\n" "$1"

      declare n=0

      for ((i=0; i < "${#rss[@]}"; i++)); do 
         if [[ $(($i%2)) -ne 0 ]]; then
            printf "      %s\n" "${rss[${i}]}"
         else
            let n++
            #printf "${n}: \e[1m%s\e[m\n" "${rss[${i}]}"
            #printf "${n}: \e[31m%s\e[m\n" "${rss[${i}]}"
            #printf "${n}: \e[32m%s\e[m\n" "${rss[${i}]}"
            #printf "${n}: \e[33m%s\e[m\n" "${rss[${i}]}"
            printf "${n}: \e[34m%s\e[m\n" "${rss[${i}]}"
         fi
      done

      echo

      shift

   done   # while

   set +f
   IFS="$OIFS"

   return 0
}


alias breadrss='breadrss | less -r'
breadrss


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


# batch read rss full-screen

# full screen
# cf. http://codesnippets.joyent.com/posts/show/1516

function fscreen() { printf "\e[3;0;0;t\e[8;0;0t"; /usr/bin/clear; return 0; }


# increase font size
# cf. http://codesnippets.joyent.com/posts/show/1516

function ifont() { 

   /usr/bin/open -a Terminal

/usr/bin/osascript <<__END__
    repeat ${1:-1} times
       tell application "System Events" to tell process "Terminal" to keystroke "+" using command down
    end repeat
__END__

   return 0 
}


alias breadrss='fscreen; ifont 10; sleep 3; breadrss | less -r'

[cmd-n]   # new Terminal window
breadrss

Enable & disable the root user account on Mac OS X

See: Another way to enable and disable root

# enable root user account
/usr/sbin/dsenableroot

# disable root user account
/usr/sbin/dsenableroot -d

Read file into array in Bash

# cf. "How can I read a file (data stream, variable) line-by-line?", 
# http://wooledge.org:8000/BashFAQ/001

man bash 2>/dev/null | less -p '\$\(< file\)'

declare -a lines
OIFS="$IFS"
IFS=$'\n'
set -f   # cf. help set
lines=( $(< "/private/etc/passwd" ) )
set +f
IFS="$OIFS"

echo "${#lines[@]}"
printf "%s\n" "${lines[@]}" | nl

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

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

convert "man" help to pdf

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

Find largest files on Mac OS X

# find files greater than 50 MB, 100 MB, ...
mdfind 'kMDItemFSSize > 52428800'
mdfind "kMDItemFSSize > $[50*1024*1024]"
mdfind "kMDItemFSSize > $[100*1024*1024]"


# find the 10 largest files on your system (> 50 MB)
/usr/bin/sudo -H -i
/usr/bin/find -x / -type f -size +$[50*1024*1024]c -ls 2>/dev/null | /usr/bin/sort -rn -k 7,7 | /usr/bin/head | /usr/bin/nl

exit


mkfile 10m ${HOME}/Desktop/'te:st\file:'

# convert file sizes to megabyte with awk (> 1 MB)
/usr/bin/find -x ~/Desktop -type f -size +$[1*1024*1024]c -print0 2>/dev/null | xargs -0 stat -f "%z:  %N" | \
      sort -rn | awk -F: '{printf "%-20s", $1/(1024*1024.0); for (i=2;i<NF+1;i++) {printf "%s%s",$i,(i==NF) ? "\n" : ":"}}'


# find the 10 largest directories in the current directory
/usr/bin/find -x . -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 du -hs
/usr/bin/find -x . -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 du -ks | sort -rn | head

# awk: print the first field and from the second field to the end (just in case there are file paths with spaces)
/usr/bin/find -x . -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 du -ks | sort -rn | head | \
    awk -F' ' '{printf "%-20s", $1/1024.0; for (i=2;i<NF+1;i++) {printf "%s ",$i}; print ""}'


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


# sort file sizes & file paths containing newlines \n
# cf. Sorting arrays in Bash, http://codesnippets.joyent.com/posts/show/1592

mkfile 10m ${HOME}/Desktop/'te:st\file'.txt
mkfile 10m ${HOME}/Desktop/'te:st\file:'
mkfile 10m ${HOME}/Desktop/$'te:s\nt\\file:'


declare -a file_path_array

man ruby | less -p '777'

IFS=$'\777'
file_path_array=($(/usr/bin/find -x ~/Desktop -type f -size +$[1*1024*1024]c -print0 2>/dev/null | xargs -0 -n 500 printf "%s\777"))
IFS=$' \t\n'


echo "${#file_path_array[@]}"
echo "${file_path_array[@]}"
printf "%s\n" "${file_path_array[@]}"  | ruby -n -e 'p $_.to_s'


declare -a file_path_array2
for ((i=0; i < "${#file_path_array[@]}"; i++)); do 
   mbyte=$(/usr/bin/stat -f "%z" "${file_path_array[$i]}" | awk '{print $1/(1024*1024.0);}')
   file_path_array2[${i}]="${mbyte}: ${file_path_array[$i]}"
done

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



IFS=$'\n'

declare -a file_path_array2_sorted
file_path_array2_sorted=( $(printf "%s\000\n" "${file_path_array2[@]}" | sed -e :a -e '$!N; s/\n/NEWLINE/g; ta' | tr '\000' '\n' | \
      sed -e 's/^NEWLINE//' | sort -rn -t . -k 1,1 -k 2,2) )

IFS=$' \t\n'

for ((i=0; i < "${#file_path_array2_sorted[@]}"; i++)); do printf "%s\n" "${file_path_array2_sorted[$i]//NEWLINE/$'\n'}"; done | nl
for ((i=0; i < "${#file_path_array2_sorted[@]}"; i++)); do printf "%s\n" "${file_path_array2_sorted[$i]//NEWLINE/\n}"; done | nl
for ((i=0; i < "${#file_path_array2_sorted[@]}"; i++)); do 
   printf "%s" "${file_path_array2_sorted[$i]//NEWLINE/\n}" | ruby -0777 -n -e 'p $_.to_s'
done | nl


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


# alternative

declare -i i=0
declare -a file_path_array


while read -d $'\000' filepath; do 
   mbyte=$(/usr/bin/stat -f "%z" "${filepath}" | awk '{print $1/(1024*1024.0);}') 
   file_path_array[${i}]="${mbyte}:   ${filepath}"
   let i++
done < <(/usr/bin/find -x ~/Desktop -type f -size +$[1*1024*1024]c -print0 2>/dev/null | sed -E '/\\/s/\\/\\\\/g')


IFS=$'\n'

declare -a file_path_array_sorted
file_path_array_sorted=( $(printf "%s\000\n" "${file_path_array[@]}" | sed -e :a -e '$!N; s/\n/NEWLINE/g; ta' | tr '\000' '\n' | \
      sed -e 's/^NEWLINE//' | sort -rn -t . -k 1,1 -k 2,2) )

IFS=$' \t\n'


for ((i=0; i < "${#file_path_array_sorted[@]}"; i++)); do printf "%s\n" "${file_path_array_sorted[$i]//NEWLINE/$'\n'}"; done | nl
for ((i=0; i < "${#file_path_array_sorted[@]}"; i++)); do printf "%s\n" "${file_path_array_sorted[$i]//NEWLINE/\n}"; done | nl
for ((i=0; i < "${#file_path_array_sorted[@]}"; i++)); do 
   printf "%s" "${file_path_array_sorted[$i]//NEWLINE/\n}" | ruby -0777 -n -e 'p $_.to_s' 
done | nl


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


# same for directory paths containing possible newlines \n

declare -i i=0
declare -a dir_path_array

while read -d $'\000' dirpath; do 
   dir_size=$(/usr/bin/du -ks "${dirpath}" | /usr/bin/awk '/^[[:digit:]]+/{print $1/1024.0}') 
   dir_path_array[${i}]="${dir_size}     ${dirpath}"
   let i++
done < <(/usr/bin/find -x . -mindepth 1 -maxdepth 1 -type d -print0 2>/dev/null | sed -E '/\\/s/\\/\\\\/g')

echo "${#dir_path_array[@]}"
echo "${dir_path_array[@]}"
printf "%s\n" "${dir_path_array[@]}"  | ruby -n -e 'p $_.to_s'


IFS=$'\n'

declare -a dir_path_array_sorted
dir_path_array_sorted=( $(printf "%s\000\n" "${dir_path_array[@]}" | sed -e :a -e '$!N; s/\n/NEWLINE/g; ta' | tr '\000' '\n' | \
      sed -e 's/^NEWLINE//' | sort -rn -t . -k 1,1 -k 2,2) )

IFS=$' \t\n'


for ((i=0; i < "${#dir_path_array_sorted[@]}"; i++)); do printf "%s\n" "${dir_path_array_sorted[$i]//NEWLINE/$'\n'}"; done | nl
for ((i=0; i < "${#dir_path_array_sorted[@]}"; i++)); do printf "%s\n" "${dir_path_array_sorted[$i]//NEWLINE/\n}"; done | nl
for ((i=0; i < "${#dir_path_array_sorted[@]}"; i++)); do 
   printf "%s" "${dir_path_array_sorted[$i]//NEWLINE/\n}" | ruby -0777 -n -e 'p $_.to_s' 
done | nl


#-------


# get file sizes via ls command
ls -alS
ls -alSr

ls -ahlS
ls -ahlSr

ls -alSrR

Compile & install ncurses on Mac OS X

Requires Xcode.

export IFS=$' \t\n'
export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/lib:/usr/local/include:/usr/bin:/bin:/usr/sbin:/sbin
export LD_LIBRARY_PATH=/usr/local/lib
export TERM=xterm-color

# 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


# gawk

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

which gawk
gawk --version


# GNU libtool

open http://www.gnu.org/software/libtool/
open http://libtool.darwinports.com/
/opt/local/bin/port info libtool

cd ~/Desktop
curl -L -O http://ftp.gnu.org/gnu/libtool/libtool-2.2.6a.tar.gz
tar -xzf libtool-2.2.6a.tar.gz

open -e ~/Desktop/libtool-2.2.6/INSTALL

cd ~/Desktop/libtool-2.2.6
./configure --help

./configure \
   CC="gcc -arch $(/usr/bin/arch)" \
   CXX="g++ -arch $(/usr/bin/arch)" \
   CPP="gcc -E" CXXCPP="g++ -E" \
   --prefix=/usr/local --mandir=/usr/local/share/man

make
sudo make install


man ncurses
man 5 terminfo
man tic
man infocmp
man tack
man toe


locate *libncurses.*
locate *ncurses.*
locate *terminfo

# cf. http://trac.macports.org/wiki/InstallingMacPorts and
# http://ncurses.darwinports.com
/opt/local/bin/port info ncurses  


# back up old terminfo versions
sudo mkdir -p /usr/share/terminfo-2008-12
sudo cp -R -p /usr/share/terminfo/* /usr/share/terminfo-2008-12
sudo mv -i /usr/local/share/terminfo /usr/local/share/terminfo-2008-12
sudo mv -i /usr/local/lib/terminfo /usr/local/lib/terminfo-2008-12


# ncurses

cd ~/Desktop
curl -L -O http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.7.tar.gz
tar -xzf ~/Desktop/ncurses-5.7.tar.gz

open -e ~/Desktop/ncurses-5.7/INSTALL

cd ~/Desktop/ncurses-5.7


# shared
./configure --prefix=/usr/local --mandir=/usr/local/share/man --with-shared \
            --enable-sigwinch --enable-widec --without-ada --disable-rpath 

# libtool
#./configure --prefix=/usr/local --mandir=/usr/local/share/man --with-libtool \
#            --enable-sigwinch --enable-widec --without-ada --disable-rpath


make

sudo make install


# tests
~/Desktop/ncurses-5.7/test/background
~/Desktop/ncurses-5.7/test/blue
~/Desktop/ncurses-5.7/test/bs
~/Desktop/ncurses-5.7/test/cardfile
~/Desktop/ncurses-5.7/test/chgat
~/Desktop/ncurses-5.7/test/color_set
~/Desktop/ncurses-5.7/test/demo_altkeys
~/Desktop/ncurses-5.7/test/demo_forms
~/Desktop/ncurses-5.7/test/demo_termcap
~/Desktop/ncurses-5.7/test/dots
~/Desktop/ncurses-5.7/test/dots_mvcur
~/Desktop/ncurses-5.7/test/echochar
~/Desktop/ncurses-5.7/test/firework
~/Desktop/ncurses-5.7/test/gdc
~/Desktop/ncurses-5.7/test/hanoi
~/Desktop/ncurses-5.7/test/knight
~/Desktop/ncurses-5.7/test/lrtest
~/Desktop/ncurses-5.7/test/ncurses
~/Desktop/ncurses-5.7/test/newdemo
~/Desktop/ncurses-5.7/test/railroad   # Hello World
~/Desktop/ncurses-5.7/test/rain
~/Desktop/ncurses-5.7/test/savescreen
~/Desktop/ncurses-5.7/test/tclock
~/Desktop/ncurses-5.7/test/testaddch
~/Desktop/ncurses-5.7/test/testcurs
~/Desktop/ncurses-5.7/test/worm
~/Desktop/ncurses-5.7/test/xmas


# requires the wide-ncurses library
~/Desktop/ncurses-5.7/test/inch_wide ~/Desktop/ncurses-5.7/test/widechars-utf8.txt
~/Desktop/ncurses-5.7/test/inch_wide ~/Desktop/ncurses-5.7/test/bulgarian-utf8.txt