symbolic link vs shell alias
In this post, we are learning differences and similarities between a symbolic link and a shell alias!
Since Python 2.x is retired, had to make command python
point to Python 3.x in my OS. On a quick lookup, there were two solutions: one through creating a symbolic link and the other through a shell alias to a Python binary file. Which is a recommended way? Hence, clarifying it.
Question: What is a symbolic link and a shell alias?
Answer: A symbolic link, aka soft link or symlink, is a special kind of file that points to another file. A shell alias allows a string to be substituted for a word when it is used as the first word of a simple command.
Syntax for a symbolic link:
$ ln -s source_file myfile
The ln -s
command creates the symbolic link.
Replace source_file
with a name of an existing file across your OS’s filesystems for which you want to create the symbolic link.
Replace myfile
with a name of a symbolic link.
Now you can use normal file management command such as cp
, rm
on the symbolic link.
Let’s see an example:
Currently, my folder structure looks like this:
Now I create a soft link to the file howdy.txt
.
$ ln -s source_folder/howdy.txt joke_file
This creates a link named joke_file
that points to the source_folder/howdy.txt
. The folder structure now looks like this:
Now you can edit the joke_file
. Any edits made in the file are reflected in the howdy.txt
file. You can remove the joke_file
using the rm
command.
First let’s count number of lines in a joke_file
and original howdy.txt
. Then let’s add some more jokes in joke_file
.
$ wc -l joke_file
5 joke_file
$ wc -l source_folder/howdy.txt
5 source_folder/howdy.txt
We see there are five lines in the files.
Let’s add another joke of 2 lines with 1 line for new line, so total 3 lines, to the joke_file
and check line counts.
$ wc -l joke_file
8 joke_file
$ wc -l source_folder/howdy.txt
8 source_folder/howdy.txt
You can now check how jokes added to the joke_file
is reflected to the howdy.txt
file as well.
You can remove the soft link using the command unlink <symbolic-link-name>
.
$ ls -ltr
total 0
lrwxr-xr-x 1 dichha staff 23 May 26 18:23 joke_file -> source_folder/howdy.txt
drwxr-xr-x 4 dichha staff 128 May 26 18:07 source_folder# unlink the symbolic link
$ unlink joke_file# See the link is gone
$ ls -ltr
total 0
drwxr-xr-x 4 dichha staff 128 May 26 18:07 source_folder# Adding back the symbolic link
$ ln -s source_folder/howdy.txt joke_file
Now let’s make a file linked to be a script file so that it gives some dynamic result.
I have the joke_generator.py
file under the source folder. Let’s create a symlink for it.
$ ln -s source_folder/joke_generator.py smiley
Now let’s look at the current file structure with tree .
command.
See, we get a new symlink file named smiley
. Let’s run the file.
$ smiley
Two antennas met on a roof, fell in love and got married.
The ceremony wasn't that much, but the reception was excellent.
It prints one of the jokes in the list. 😜
Let’s make this symlink smiley
accessible in all shell session by putting it in .bash_profile
.
PATH=<path-to-your-folder-where-symlink-lies>:${PATH}
Source your .bash_profile
and now you can run smiley
whenever wherever 🎵 in the bash shell you like!
Now let’s jump to shell alias.
Syntax for an alias:
$ alias name=value
The name
is a name of the alias.
The value
is a command line or a string.
Let’s see an example:
$ alias ll='ls -al'
$ ll
total 8
drwxr-xr-x 4 dichha staff 128 May 25 23:51 .
drwxr-xr-x 16 dichha staff 512 May 25 23:28 ..
-rw-r--r-- 1 dichha staff 181 May 25 23:50 joke_file
drwxr-xr-x 3 dichha staff 96 May 25 23:49 source_folder
Here I named a command ls -al
as ll
. To make an alias permanent, you can add the alias in ~/.bash_profile
for a login-shell. You can remove an alias using unalias <alias-name>
.
Similarities:
Shell Alias and symbolic link both point to some other files.
Differences:
Shell Alias: is a shell specific command.
Symbolic link: almost a file, contains file location, on your filesystem. So if you move a file location, symbolic link is broken.
In conclusion, going a symbolic link route is a better solution in this case since its a reference to another file in a filesystem and not a shell specific.
That’s all for this post. I hope it was useful for your development. If you liked it, 👏 appreciated.
Congratulations on the completion! Thank you for reading.
There’s also another kind of alias, MacOS Finder.
Reference: