1

Symlinks are different than aliases, although they seem to serve the same purpose (more or less/I think). I need to be able to tell if a file is an alias, and

if [ -h /path/to/file ]

doesn't work. Is there anything like this for aliases? Google was most unhelpful as aliases are apparently the name for something else in bash altogether.

Thanks!

Jared Pochtar
  • 4,925
  • 2
  • 29
  • 39

4 Answers4

4

The Finder stores the information that a file is an alias in the ResourceFork of the file. To read this metadata, I would use spotlight to determine the kind of the file; the following command will return the kind of the file, so you could then compare it in an if-statement.

 mdls -raw -name kMDItemKind /path/to/test.pdf          returns  PDF (Portable Document Format)
 mdls -raw -name kMDItemKind /path/to/test.pdf\ Alias   returns  Alias

An other way would incorporate Applescript, which is executable on the command line via osascript. To return the kind of a file, run:

tell application "Finder" to get kind of ((POSIX file "/path/to/test.pdf\ Alias") as alias)
Asmus
  • 5,117
  • 1
  • 16
  • 21
  • This solution did not work for me, but got name started. Below you can find what worked for me (can't put several lines of code here). – Gab Aug 24 '18 at 20:32
  • 1
    Be careful when using this `kMDItemKind` spotlight property for string comparisons. The value is localized and may not be "Alias" for every system language. – Toby Nov 09 '20 at 12:13
  • 1
    Consider using the `kMDItemContentType` mdls property which returns `com.apple.alias-file` to determine Finder aliases independent from the user's system language. – Toby Nov 09 '20 at 12:47
1

I'm not quite clear if you're asking about:

  • aliases (a way of making one command actually run something else)
  • symlink (a way of making a shortcut to a file)
  • hard link (two file names both pointing to the same file contents)

The command you were trying:

if [ -h /path/to/file ]

helps you figure out if a file is a symlink or not, e.g.:

$ touch newfile
$ ln -s newfile newlink
$ for f in newfile newlink; do
    if [ -h "$f" ]; then
        echo "$f is a symlink"
    else
        echo "$f is not a symlink"
    fi
done
newfile is not a symlink
newlink is a symlink

If you mean: "how can I find out whether typing some command will run an alias", then you can use type or alias, e.g.

$ type ls
ls is aliased to `ls --color=auto --format=across'

$ type less
less is /usr/bin/less

If you're asking about hard links, find -inum and ls -i can help, but that is a more advanced topic.

Mikel
  • 24,855
  • 8
  • 65
  • 66
1

Asmus' solution did not quite work for me, but he got me started. Here is what worked for me (macOS 10.13 , High Sierra, assuming you execute it in bash):

alias=$( mdls -name kMDItemKind "$file" )
if [[ "$alias" = *Alias* ]]
then
    echo "$file is an Alias"
fi

HTH.

Gab
  • 306
  • 3
  • 7
  • Upvote his answer ... if you are checking for alias couldn’t u just check if the file exists? – Mike Q Aug 25 '18 at 03:37
  • Re "if you are checking for alias couldn’t u just check if the file exists" -- what if the alias is broken? – Gab Aug 26 '18 at 20:03
0

You could use the /usr/bin/getfileinfo utility:

if ((`getfileinfo -aa /path/to/file`))
then #
fi
Daniel Bayley
  • 1,493
  • 1
  • 10
  • 8