Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Mac Finder to list all aliases in a folder

I downloaded a repository from Bitbucket.org as a zip file and unzipped it using iZip to my Mac. Xcode found many compile errors because the zip or unzip did not preserve aliases properly. So I used hg to clone the repo, the aliases were preserved, and the Xcode compile was then clean. I would like to find all the aliases in my folder and replace them by their targets so future zips will work. I've done a lot of searching and can't find anything that says how find them either with the Mac Finder utility or the bash find command. I've tried using Finder->All My Files with search->kind->other->alias and it finds about 100 aliases but not the ones in my local repo that I know are there. Do I need to rebuild/update the OSX 10.9.1 index list of all my files somehow? Does the find command have a flag for OSX alias file types? Can I navigate Finder to a folder and then search recursively for a file type (the search criteria option seems to disappear if All My Files is not selected). Another alternative would be to print to file the contents of a full Finder list with the "Kind" column showing and then sort by that.

mdfind "kMDItemKind == 'Alias'" -onlyin /path/of/repo

Seems like mdfind might work. But it only finds the same files that Finder->All My Files with search->kind->other->alias finds. Those files and the ones in my repo are listed as Kind "Alias" by Finder. I've read that there are 3 kinds of links: Alias, Symbolic Links, and Hard Links. The listing by the ls command for a file found by the mdfind command is:

-rw-r--r--@ 1 kenm staff 45100 Dec 25 2012 GTLDrive.h

The listing by the ls command for a file I would like to find is:

lrwxr-xr-x 1 kenm staff 24 Jan 14 21:38 Headers -> ./Versions/A/Headers

Finder calls them both "Alias" but ls thinks they are different. Is there an mdfind command line that finds the second type of Alias?

like image 289
KenM Avatar asked Jan 15 '14 23:01

KenM


2 Answers

In Terminal:

This should find all aliases (Apple aliases)

mdfind "kMDItemKind == 'Alias'" -onlyin /path/of/your/repo

For finding all symlinks (symbolic links) you can use:

ls -lR /path/of/your/repo | grep ^l

To only show symlinks in current directory:

ls -la | grep ^l

If you want to view the full path of symlinks:

find /path/of/your/repo -type l
like image 70
l'L'l Avatar answered Sep 19 '22 23:09

l'L'l


Apple has conflated alias and symlink. I don't know how to do this at the command line other than using ls.

ls -la | grep ">"

The following Objective-C function would find the target.

#include <sys/stat.h>

NSURL *targetOfAlias(NSURL *url) {
    CFErrorRef *errorRef = NULL;
    CFDataRef bookmark = CFURLCreateBookmarkDataFromFile (NULL, (__bridge CFURLRef)url, errorRef);
    if (bookmark == nil) return nil;
    CFURLRef resolvedUrl = CFURLCreateByResolvingBookmarkData (NULL, bookmark, kCFBookmarkResolutionWithoutUIMask, NULL, NULL, NO, errorRef);
    return CFBridgingRelease(resolvedUrl);
}

NSString *getTarget(NSString *fPath) {
    NSString *resolvedPath = nil;
    // Use lstat to determine if the file is a symlink
    struct stat fileInfo;
    NSFileManager *fileManager = [NSFileManager new];
    if (lstat([fileManager fileSystemRepresentationWithPath:fPath], &fileInfo) < 0)
        return nil;
    if (S_ISLNK(fileInfo.st_mode)) {
        // Resolve the symlink component in the path
        NSError *error = nil;
        resolvedPath = [fileManager destinationOfSymbolicLinkAtPath:fPath error:&error];
        if (resolvedPath == nil) {
            NSAlert *alert = [NSAlert alertWithError:error];
            [alert runModal];
            return nil;
        }
        if ([resolvedPath isAbsolutePath])
            return resolvedPath;
        else
            return [[fPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:resolvedPath];
    }

    // Resolve alias
    NSURL *resolvedUrl = targetOfAlias([NSURL fileURLWithPath:fPath]);
    return [resolvedUrl path];
}
like image 43
Milliways Avatar answered Sep 19 '22 23:09

Milliways