1

Suppose I have a folder /my/dir/ and an alias in ~/Documents (called thisdir) pointing to /my/dir/. How can I find the alias (ie. find all aliases) that points to /my/dir/?

This question is unlike a seemingly similar question on Ask Different, which asks for how to find where an alias points to (even with deleted files). To the careful reader, this questions however asks for the reverse:

/my/dir/ points to ~/Documents/thisdir, ie. the dir/ in /my/dir/ is an alias pointing to ~/Documents/thisdir.

I want to know how I can find out what points to thisdir, if I don't know /my/dir/.

Alex Ixeras
  • 1,358
  • I'm not sure this would be possible without scanning the entire drive for aliases & seeing where they point. If I make an alias, it is generated right next to its original. I then move [or copy/delete] across to another drive. The only place this alias now exists is on the other drive. The original file cannot be 'aware' of where it went, as far as it's concerned the alias right next to it was deleted. – Tetsujin Jan 22 '23 at 08:53
  • Yes, I've tried something like find with the samefile option:find -L /Users/Alex/Library -type l -samefile /Users/Alex/Documents/Google\ Earth 2>&1 | grep -v "Operation not permitted". I have a link to this Google Earth folder but can't remember where exactly it's linking from. It might also not be exactly the same name. Somehow, my CLI command didn't really work. – Alex Ixeras Jan 22 '23 at 13:25
  • 1
    Your find construct would work on hunting symbolic links which is a file type of the OS, but not with aliases which are added meta-data managed by the Finder and md tools. From many years of experience on many MacOSes and Unixes, I highly prefer symbolic links over aliases. – dan Jan 23 '23 at 20:27
  • Thanks, yes, I start to realise that too. Hadn't really had the need to chase down aliases, but now that I moved to a new Mac I have to. – Alex Ixeras Jan 23 '23 at 22:36

1 Answers1

1

Add Search Criteria to Spotlight Search

When you run a Spotlight search using metadata attributes. In your case, use the following search: kind:Alias

Spotlight kind: Alias search

Finder will find all Aliases on your system. If you select Show all in Finder..., it will open a new Finder window with all of the Aliases in one place. Simply select an Alias and press ⌘ CommandI or right click your mouse and select Get Info

That screen will show the metadata attributes of the file. Look for Original which will indicate the original target (where it points to) of the Alias.

Alias Get Info Screen

Use Terminal to get a list of aliases

Use the mdfind command to search for aliases in your system. A simple search would be:

mdfind "kMDItemKind == 'Alias'"

This will pull up all Aliases on your system. Refer to the mdfind man page (man mdfind) for more specifics on searching syntax.

You can get the file's attributes using the mdls command (man mdls):

mdls "Clone Wars"

This will display a complete list of all the metadata associated with that file including the item type. Unfortunately, you can't get the target because that is stored as binary data and must be retrieved through Finder.

For a full list of searchable metadata types, see Narrow your search results on Mac (I keep it bookmarked for quick reference)

Allan
  • 101,432
  • Thanks. I've created an alias from ~/Library/Application Support/Google Earth to ~/Users/Alex/Documents/Maps/Google\ Earth (so that I can find my templates and spaces easier for backup. When I type mdls ~/Library/Application\ Support/Google\ Earth I don't see anywhere a name that would identify the "target" as in "Original" via the Finder's Get Info. Is there a way to output the target here (~/Users/Alex/Documents/Maps/Google\ Earth)? – Alex Ixeras Jan 23 '23 at 14:12
  • 1
    mdls won’t give you the target. Aliases are a Finder convention; I much prefer symlinks over Aliases. To get an Alias’ target, you need to use “Get Info” or write an AppleScript. With a symlink, it literally shows you the target when you list the directory contents (ls) in Terminal – Allan Jan 23 '23 at 14:27
  • Thanks, that makes sense. I'm not too familiar with AppleScript, but as you say it could be the only way. Hadn't really had the need to chase down aliases, but now that I moved to a new Mac I have to. – Alex Ixeras Jan 23 '23 at 22:39
  • 1
    I don’t do AppleScript because I prefer Bash scripting more. But, if memory serves, there is a getTarget function. The AppleScript “programming” would be a new question, though. Start with an AS that searches for the alias. You’ll just have to decide how you want the output and wheter you want it one by one or as a batch – Allan Jan 23 '23 at 22:49