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

那年仲夏 提交于 2019-11-29 04:43:24

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

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];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!