0

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.

chb
  • 1,727
  • 7
  • 25
  • 47
Hadi Norouzi
  • 179
  • 2
  • 8

1 Answers1

1

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
chb
  • 1,727
  • 7
  • 25
  • 47
  • If this is what the OP is asking, the question belongs on [unix.se] rather than Stack Overflow, as nothing in this answer (or presumably the question) is "unique to software development". In [How to Answer](https://stackoverflow.com/help/how-to-answer), see the section "Answer Well-Asked Questions", and therein the bullet point regarding questions which "are not about programming as defined in the help center". – Charles Duffy Jul 31 '19 at 20:49