Reputation: 199
I want to create an alias for a file in macOS, but in the Terminal, not in the Finder (the file explorer).
My intent is to run a script and create file aliases for files in the current directory.
Upvotes: 0
Views: 1235
Reputation: 2015
At the heart of macOS is a UNIX-like operating system known as Darwin, which is based on several different UNIX variants (BSD, NeXTSTEP, Mach). The Terminal application presents the user with a shell known as the bash
shell (this is the default). In order to link one file to another in bash
, you'd use ln
and either create a soft link (also known as a symbolic link) or a hard link.
In contrast to a soft link, a hard link is tied to an element on the file system that has nothing to do with the original file's name (this element is known as an inode). The disadvantage of a soft link is that, if the original file that it is linked to is renamed, the process of renaming breaks the symbolic/soft link. However, soft links do have the advantage of being able to span file systems.‡
To create a soft link, you'd use the -s
flag:
ln -s <original> <link>
<link>
is now a symbolic link to <original>
.
To create a hard link, you'd simply use ln
without the -s
flag:
ln <original> <link>
A more concrete example:
[tmp]$ ls -al *.py
-rw-rw-r-- 1 chb chb 0 Jul 31 12:00 script.py
[tmp]$ ln -s script.py alias.py
[tmp]$ ls -al *.py
lrwxrwxrwx 1 chb chb 9 Jul 31 12:01 alias.py -> script.py
-rw-rw-r-- 1 chb chb 0 Jul 31 12:00 script.py
Upvotes: 2