views:

819

answers:

3

I'm writing a shell script which will rsync files from remote machines, some linux, some macs, to a central backup server. The macs have folders on the root level containing aliases of all files/folders which need to be backed up. What is a terminal command I can use to resolve the path to the files/folders the aliases point to? (I'll need to pass these paths to rsync)

A: 

Aliases are symlinks, right? (never know if apple invented their own wheel)

If rsync doesn't have a switch to follow the links itself, you can use this function. It will print final tagret of a symlink regardless of how many levels of redirection are there.

function resolve_symlink()
{
    local dir="$1"
    local final_path=""

    while true
    do
        if [[ -h "${dir}" ]]
        then 
            dir="$(ls -l "${dir}" | sed "s#^.*-> ##")"
        else
            echo "${dir}"
            break
        fi
    done
}

resolve_symlink "/path/to/symlink"
Eugene
Assuming Josh is really talking about aliases, and not using "alias" as a synonym for "symlink" (as some people do), then no, aliases _aren't_ symlinks. HFS+ does support symlinks, and the Finder displays symlinks in the same way as aliases, but they're not the same thing.
mipadi
@mipadi is correct -- aliases are specific to Mac OS X and are sadly not the same as symlinks.
Josh
The `file` command can tell you where a symlink goes. Maybe it also works on aliases.
jleedev
Groxx
+1  A: 

I found the following script which does what I needed:

#!/bin/sh
if [ $# -eq 0 ]; then
  echo ""
  echo "Usage: $0 alias"
  echo "  where alias is an alias file."
  echo "  Returns the file path to the original file referenced by a"
  echo "  Mac OS X GUI alias.  Use it to execute commands on the"
  echo "  referenced file.  For example, if aliasd is an alias of"
  echo "  a directory, entering"
  echo '   % cd `apath aliasd`'
  echo "  at the command line prompt would change the working directory"
  echo "  to the original directory."
  echo ""
fi
if [ -f "$1" -a ! -L "$1" ]; then
    # Redirect stderr to dev null to suppress OSA environment errors
    exec 6>&2 # Link file descriptor 6 with stderr so we can restore stderr later
    exec 2>/dev/null # stderr replaced by /dev/null
    path=$(osascript << EOF
tell application "Finder"
set theItem to (POSIX file "${1}") as alias
if the kind of theItem is "alias" then
get the posix path of ((original item of theItem) as text)
end if
end tell
EOF
)
    exec 2>&6 6>&-      # Restore stderr and close file descriptor #6.

    echo "$path"
fi
Josh
+2  A: 

Though I realize how late this is, for future reference I've found this:

http://www.warrenmoore.net/blog/2010/01/09/make-terminal-follow-aliases-like-symlinks/

A tiny bit of compiled code, a function in your .bash_profile, and viola. Transparent handling of aliases, just use "cd". Several times faster than using Applescript, too.

Groxx