如何找到所有 Mac OS X 的“别名”文件并找到它们的目标文件(尽管链接已损坏)?

eri*_*nyc 5 osx alias

我刚刚发现我正在处理符号链接而不是别名本身。(出于某种原因,它们在 finder 中显示为别名。)

我的硬盘上有大量无法解释的符号链接。不幸的是,符号链接不起作用——即,单击它不会让我进入该文件。

所以现在我想在我的硬盘上找到所有的符号链接。然后对于每个文件,搜索它曾经链接到的文件,大概在同一个 HDD 上。然后将该文件复制到与 SYMLINK 相同的子目录中。

所以我有,粗略地说,

FIND all files (in Directory) type=SYMLINK. FOR each one, DO ..
      FIND referentFile (somewhere on the HDD)
      cp referentFile to (Subdirectory that contains the alias).
DONE.
Run Code Online (Sandbox Code Playgroud)

非常感谢。PS——我真的在考虑在这个问题上回到严格的Linux。设置越简单,数据灾难的可能性就越小 - 哈哈。

iga*_*gal 4

这个问题的第一部分在AskDifferent上有答案:

这是查找别名的命令:

mdfind kMDItemKind="Alias"
Run Code Online (Sandbox Code Playgroud)

一般来说,您可以使用mdfind(元数据查找)命令来(非常)快速地搜索文件。从手册页:

mdfind命令查询中央元数据存储并返回与给定元数据查询匹配的文件列表。查询可以是字符串或查询表达式。

所以我还不会放弃 OSX。事实上,mdfind这是我在使用 Linux 时最怀念 OSX 的事情之一。

不幸的是,这个问题的第二部分出人意料地更难解决。我找到了一些相关的 StackExchange 帖子:

这些让我想到了几个潜在的解决方案。

我最喜欢的是博客文章“让终端跟随符号链接一样的别名”。它引用了一个名为 getTrueName.c 的小型开源 C 程序。这是源代码:

// getTrueName.c
// 
// DESCRIPTION
//   Resolve HFS and HFS+ aliased files (and soft links), and return the
//   name of the "Original" or actual file. Directories have a "/"
//   appended. The error number returned is 255 on error, 0 if the file
//   was an alias, or 1 if the argument given was not an alias
// 
// BUILD INSTRUCTIONS
//   gcc-3.3 -o getTrueName -framework Carbon getTrueName.c 
//
//     Note: gcc version 4 reports the following warning
//     warning: pointer targets in passing argument 1 of 'FSPathMakeRef'
//       differ in signedness
//
// COPYRIGHT AND LICENSE
//   Copyright 2005 by Thos Davis. All rights reserved.
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of the
//   License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful, but
//   WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   General Public License for more details.
//
//   You should have received a copy of the GNU General Public
//   License along with this program; if not, write to the Free
//   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
//   MA 02111-1307 USA


#include <Carbon/Carbon.h> 
#define MAX_PATH_SIZE 1024
#define CHECK(rc,check_value) if ((check_value) != noErr) exit((rc))

int main ( int argc, char * argv[] ) 
  { 
    FSRef               fsRef; 
    Boolean             targetIsFolder; 
    Boolean             wasAliased; 
    UInt8               targetPath[MAX_PATH_SIZE+1]; 
    char *              marker;

    // if there are no arguments, go away
    if (argc < 2 ) exit(255); 

    CHECK( 255,
      FSPathMakeRef( argv[1], &fsRef, NULL ));

    CHECK( 1,
      FSResolveAliasFile( &fsRef, TRUE, &targetIsFolder, &wasAliased));

    CHECK( 255,
      FSRefMakePath( &fsRef, targetPath, MAX_PATH_SIZE)); 

    marker = targetIsFolder ? "/" : "" ;
    printf( "%s%s\n", targetPath, marker ); 

    exit( 1 - wasAliased );
  }
Run Code Online (Sandbox Code Playgroud)

我下载了这个源代码并使用以下命令进行编译:

gcc -o getTrueName -framework Carbon getTrueName.c
Run Code Online (Sandbox Code Playgroud)

我当然想测试一下。为了避免离开终端,我使用以下命令创建了一个别名:

user@host:~$ FULL_PATH_TO_TARGET_FILE=/tmp/alias-target

user@host:~$ echo "testing" > "${FULL_PATH_TO_TARGET_FILE}"

user@host:~$ osascript \
-e 'tell application "Finder"' \
-e "make new alias to file (posix file \"${FULL_PATH_TO_TARGET_FILE}\") at desktop" \
-e 'end tell'

alias file alias-target of folder Desktop of folder user of folder Users of disk MacHD
Run Code Online (Sandbox Code Playgroud)

然后我使用该程序验证了别名getTrueName

user@host:~$ ./getTrueName ~/Desktop/alias-target

/tmp/alias-target
Run Code Online (Sandbox Code Playgroud)

胜利!

博客文章愚蠢的 Mac OS X 技巧:解决别名看起来也可能有一个解决方案。它包含以下 Perl 脚本:

user@host:~$ FULL_PATH_TO_TARGET_FILE=/tmp/alias-target

user@host:~$ echo "testing" > "${FULL_PATH_TO_TARGET_FILE}"

user@host:~$ osascript \
-e 'tell application "Finder"' \
-e "make new alias to file (posix file \"${FULL_PATH_TO_TARGET_FILE}\") at desktop" \
-e 'end tell'

alias file alias-target of folder Desktop of folder user of folder Users of disk MacHD
Run Code Online (Sandbox Code Playgroud)

我没有所需的库,而且我不想安装一堆东西,所以我放弃了这个。

我还发现了mac_alias,它看起来很有希望。我花了 5 到 10 分钟摆弄它,但无法弄清楚如何解析别名目标。