How to query target of all Finder aliases?

1.3k views Asked by At

Mac OSX 10.6

I've got some aliases that are pointing to the wrong volume. I'd like to search my whole hierarchy for such aliases. They're aliases, not symlinks, so I can't just do find / -type l -ls | grep badVolumeName.

It seems that aliases have a com.apple.ResourceFork, but querying this with xattr gives me binary data. Is there a way to dump the target of the alias as text so I can grep for the bad volume name? Or what's another way to do this?

2

There are 2 answers

0
augurar On

To find alias files you can make use of this answer on StackOverflow. First, create a script is_alias.sh:

#! /bin/bash
[ "$(mdls -raw -name kMDItemKind "$1")" = "Alias" ]

and then run

find . -type f -exec ./is_alias.sh {} \; -print

Getting the path of an alias seems quite difficult.

Someone has posted a solution using MacPerl, but as I do not have MacPerl I haven't tested this and don't know whether it works.

There is a similar question on AskDifferent, with several different suggestions, but none of them seem to actually solve the problem. The Applescript answer is okay, but the key operation, getting the "original item" of an alias file, doesn't seem to work for broken aliases.

You can also take a look at this question which might have some Cocoa solutions.

0
user3011542 On

the bash script didn't work for me because I was running it in zsh. so I ran the below perl script with find:

find . -type f -print0|xargs -0 isAlias.pl

and here's the perl script:

#!/usr/bin/perl -w

while ( my $name = shift @ARGV ) {
  #print $name;

  open my $fh, "-|", ( 'mdls', '-n', 'kMDItemKind', '--', $name ) or die "Failed spawning mdls on '$name': $!";
  my @output_lines = <$fh>;
  close $fh;
  #print " ", scalar @output_lines, " ";
  #print "'", @output_lines, "'\n";
  #chomp $output_lines[0];
  
  $output_lines[0] =~ m/kMDItemKind = "([^"]+)"/ or die "Failed getting ItemKind: $!";

  my $kind = $1;

  if ($kind =~ m/Alias/) {
    print "'$name' is an alias\n";
  }
  #else {
  #    print "\n";
  #}
}