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

open or cd to the directory of a file path

# open directory of a file path
# can be used together with the [esc-.] key sequence
function odf() { /usr/bin/open "$(/usr/bin/dirname "$@")"; return 0; }

# cd to the directory of a file path
function cdf() { cd "$(/usr/bin/dirname "$@")"; return 0; }


ls -l /Library/Desktop\ Pictures/Nature/Tranquil\ Surface.jpg
ls -l /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ExecutableBinaryIcon.icns 

odf [esc-.]
cdf [esc-.]


odf /System/Library/SystemProfiler/SPFirewallReporter.spreporter/Contents/Resources/
cdf /System/Library/SystemProfiler/SPFirewallReporter.spreporter/Contents/Resources/English.lproj/Localizable.strings

newfolder contextual menu item with Automator

Right-click on a folder in a Finder window and select Automator -> newfolder to create a "NewFolder".


open -a Automator

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

Drag or add actions here to build your workflow:
Library: Finder -> Action: Get Selected Finder Items
Library: Automator -> Action: Run Shell Script
                                 - Shell: /bin/sh
                                 - Pass input: as arguments

current_dir="$@"
name_of_new_dir="NewFolder"

if [[ -d "${current_dir}" ]] && [[ -w "${current_dir}" ]]; then
   /bin/mkdir -p "${current_dir}${name_of_new_dir}"
fi

exit 0


# now save the newfolder Automator workflow as a contextual menu item
Automator -> File -> Save As Plug-in ... -> Save Plug-in As: newfolder -> Plug-in for: Finder -> Save

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

open ~/Library/Workflows/Applications/Finder/newfolder.workflow

# now open your Home directory, right-click on a folder and select Automator -> newfolder to create a "NewFolder" 
open ~

dirmodes

Usage: dirmodes /path/to/directory


function dirmodes() {

   declare dir user group mods
   declare -a ar ret
   declare -i size i n 

   dir="$@"

   if [[ ! -e "$dir" ]]; then printf "%s\n" "Directory (or file) does not exist: $dir"; return 1; fi

   dir=${dir%/}      # remove a trailing slash character "/" if necessary

   OIFS="$IFS"
   export IFS=$'\n'


   i=-1
   while [[ -n "$dir" ]]; do
      i=$[i+1]
      user="$(/usr/bin/stat -f "%Su" "$dir")"
      group="$(/usr/bin/stat -f "%Sg" "$dir")"

      mods="$(/usr/bin/stat -f "%p" "$dir")"
      mods="${mods: -4}"
      #mods="$(/usr/bin/stat -f "%p" "$dir" | /usr/bin/grep -Eo "[[:digit:]]{4}$")"
  
      ar[$i]="$(printf "%-35s        %-50s\n" $user:$group:$mods $dir)"

      dir="$(/usr/bin/dirname "$dir")"

      if [[ "$dir" == '/' ]]; then 
         i=$[i+1] 
         ar[$i]="$(printf "%-35s        %-50s\n" $user:$group:$mods $dir)"
         dir=""
      fi

   done


   # get number of array elements 
   size=$(/bin/expr ${#ar[@]} - 1 )
   n=-1
   
   for (( i=$size; i>=0; i-- )); do    # reverse the array
      n=$[n+1]
      ret[$n]=${ar[$i]}
      printf "%s\n" "$(printf -- "${ar[$i]}" | tr -d '\r\n')"
   done

   export IFS="$OIFS"

   return 0

}

Compressing a directory with rar on Linux

I've been struggling to get this to work for so long that when I finally got it going I had to throw it up here so I wouldn't lose it.
rar a -m5 -R output.rar /etc/

This will create a max compression (not taking into account dictionary sizes and the like) archive of the entire etc directory.

Latest file to download

Perl script using shell command to get(/grep) the lastest file on directory for download.

Just Create .htaccess (DirectoryIndex .latest.cgi) for more automations, so to get the latest files, just point your download link to: "yoursite.com/files/"

#!/usr/bin/perl
#This is .latest.cgi
$|++; my @file = `ls -1 -t -p | grep -v -P '/'`; 
print "Location: $file[0]\n\n";
exit;