[et_pb_section bb_built=”1″ admin_label=”section”][et_pb_row admin_label=”row” background_position=”top_left” background_repeat=”repeat” background_size=”initial”][et_pb_column type=”4_4″][et_pb_text admin_label=”Text” _builder_version=”3.0.51″ background_layout=”light” text_orientation=”left” border_style=”solid” background_position=”top_left” background_repeat=”repeat” background_size=”initial”]
Symbolic Links are a neat little feature that you should know about if you’re a programmer, and especially if you’re a system administrator. So let’s take a look at what Symlinks are, and see how we can use them.
Pre-requisites: You’ll need to understand how to move around in your command line to understand this article.
What is a Soft Link?
Simply put, a Symlink, usually a “soft link”, is a “pointer” to another file or directory. It’s a reference to a target file. One of the use cases is to have access to the same file from multiple locations on your computer. When you change the contents of the file or the symlink, the original file aka “target” file is updated. There’s two kinds of links. a “Soft link” and a “hard link”, and we’ll talk about the differences later.
[/et_pb_text][et_pb_video admin_label=”Video” _builder_version=”3.0.51″ src=”https://www.youtube.com/watch?v=4bp63EZwm64″ /][et_pb_button admin_label=”Button” _builder_version=”3.0.51″ button_text=”Subscribe to My Channel!” button_url=”https://www.youtube.com/channel/UCa0s8d-23qP7RmIMZ54x7Ug?view_as=subscriber” url_new_window=”on” button_alignment=”center” background_layout=”dark” custom_button=”on” button_letter_spacing=”0″ button_icon_placement=”right” button_letter_spacing_hover=”0″ custom_css_main_element=”width: 100%;” button_bg_color=”#e02b20″ saved_tabs=”all” global_module=”770″ /][et_pb_text admin_label=”Text” _builder_version=”3.0.51″ background_layout=”light” text_orientation=”left” border_style=”solid” background_position=”top_left” background_repeat=”repeat” background_size=”initial”]
Some Soft Link Examples:
First, let’s enter an empty test directory:
Commands will be slightly different on Windows, so look up the alternative command if you’re a windows user.
Desktop johncurry$ mkdir test && cd test # make directory and change into it. test johncurry$ echo "This command creates a file">>file.txt # create file called file.txt test johncurry$ cat file.txt # view contents of file.txt This command creates a file
Okay, now we’re all setup, let’s create some symlinks!
test johncurry$ ln -s file.txt softlink1 # Creates a softlink called softlink1 test johncurry$ ls -li #take a look at how the softlink "points" to file.txt total 8 26687234 -rw-r--r-- 1 johncurry staff 0 Jul 18 07:17 file.txt 26687255 lrwxr-xr-x 1 johncurry staff 8 Jul 18 07:17 softlink1 -> file.txt test johncurry$ cat softlink1 # view contents of softlink1 This command creates a file test johncurry$ echo "adding text to softlink1">>softlink1 test johncurry$ cat softlink1 This command creates a file adding text to softlink1 test johncurry$ cat file.txt #notice both have the same content This command creates a file adding text to softlink1 test johncurry$ mkdir folder1 test johncurry$ ln -s folder1/ symbolicFolder test johncurry$ cd folder1/ folder1 johncurry$ touch anotherFile.txt dog.txt kitten.txt # create 3 files folder1 johncurry$ cd ../symbolicFolder # move out of folder1 and into symbolicFolder symbolicFolder johncurry$ ls # Yay! It even works on directories! anotherFile.txt dog.txt kitten.txt
Moving things around will break the symlink
Generally, it’s recommended to use relative paths, and to fix your symlinks when you move things around instead of using absolute paths.
Before deciding whether to create the symlink with a relative or absolute path, consider if and how you’ll move things around in the future. With a relative path, if you move the symlink (or the target file) you might break the link and have to fix it.
symbolicFolder johncurry$ cd .. # Change out of symbolicFolder test johncurry$ mv symbolicFolder .. # move symbolicFolder out of the current directory test johncurry$ cd .. # Change out of current directory Desktop johncurry$ cd symbolicFolder # fail to access directory because we broke the relative path by moving the symlink! -bash: cd: symbolicFolder: No such file or directory Desktop johncurry$ mv test/folder1 .. # move the folder1 out of the test/ so the relative path matches again Desktop johncurry$ cd symbolicFolder symbolicFolder johncurry$ ls # it works again because the relative path of the symlink can reach the target file again! file1 file2 file3
An absolute path will work as long as you don’t move the original “target file”, but it has its own set of problems. The problem with absolute paths is they won’t work on other peoples computers. If you create a program for others to use, the symlinks wont work.
You’ll also have problems with absolute path symlinks if you decide to move the directory as a whole. A lot of times symlinks are used in programs, so if you move the program, the symlinks will break. Relative paths are more common. It’s generally better to use relative paths.
$ cd test/ test johncurry$ ln -s /Users/johncurry/Desktop/test/folder1/ absoluteLink # Obviously you should use your own path. test johncurry$ cd absoluteLink # No errors means it worked. Check by running "ls" absoluteLink johncurry$ cd .. test johncurry$ mv absoluteLink .. test johncurry$ cd ../absoluteLink absoluteLink johncurry$ ls # There's all our files! We're able to move our symlinks now, but we can't move the target file. anotherFile.txt dog.txt kitten.txt
Just in case it’s not clear, You can create a symbolic link to anywhere on your computer system. You just need to provide the path to the target, and a path to the symlink. Let’s do One more example of a Soft Link because I love saying the same thing over and over in different ways. Start in our test directory.
test johncurry$ touch anotherfile.txt && mkdir anotherFolder && cd anotherFolder anotherFolder johncurry$ ln -s ../anotherfile.txt anotherSymbolic anotherFolder johncurry$ echo "just another example">>anotherSymbolic anotherFolder johncurry$ cat ../anotherfile.txt just another example
How to FIX broken Soft Links
Nice and simple. You just have to use the -f flag to “unlink” the current symlink, and re-link it with the -s flag. See $ man ln
test johncurry$ mkdir nestedFolder # create folder so I can break the symlink test johncurry$ touch dog.txt && echo "woof">>dog.txt # create dog.txt and add 'woof' test johncurry$ ln -s dog.txt softlink # create soft link test johncurry$ cat softlink # test softlink woof test johncurry$ mv softlink nestedFolder/ # break soft link test johncurry$ cd nestedFolder/ nestedFolder johncurry$ cat softlink # oh no! It's broken! cat: softlink: No such file or directory nestedFolder johncurry$ ln -nfs ../dog.txt softlink # fix the link here! nestedFolder johncurry$ cat softlink # yay it works! woof
What is a HARD link?
A SOFT link is just a reference to a file. If you have a SOFT link at ~/Desktop/symLink that points to /var/importantFile.txt, then when you open ~/Desktop/symLink, it’s just redirecting you to open /var/importantFile.txt. ~/Desktop/symLink is like a shortcut to the real file.
A HARD link basically creating another entrance point to the file. Think of it like having a room in a house that has ONE door. When you create a “room” in a house, there is ONE door to get into that room. A hard link is like cutting a hole in another wall, and putting in another door to get to that room. No matter where you put the doors, it will still lead you to the room. (A soft link would be like different directions that all lead you to the same door. ex. “First door on the left” or “walk through the kitchen, around the corner, into the hall, and first door on the left”)
The “data” of a file is associated with something called an inode. The inode has the directions to the data, as well as the information such as file permissions and other metadata. Because the files have the same “inode”, they have the same data and permissions, and are thus “hard linked”.
test johncurry$ mkdir hardlinks && cd hardlinks hardlinks johncurry$ echo "I am a file">>normalFile.txt #create normalfile.txt hardlinks johncurry$ ls -li total 8 26673474 -rw-r--r-- 1 johncurry staff 12 Jul 17 17:30 normalFile.txt #inode is 26673474 hardlinks johncurry$ cat normalFile.txt I am a file hardlinks johncurry$ ln normalFile.txt hardlink # create hardlink hardlinks johncurry$ ln -s normalFile.txt softlink # create softlink hardlinks johncurry$ ls -li # notice the hardlink & normal file share an inode, but softlink is different. total 24 26673474 -rw-r--r-- 2 johncurry staff 12 Jul 17 17:30 hardlink 26673474 -rw-r--r-- 2 johncurry staff 12 Jul 17 17:30 normalFile.txt 26673520 lrwxr-xr-x 1 johncurry staff 14 Jul 17 17:32 softlink -> normalFile.txt hardlinks johncurry$ cat hardlink # hardlink has access to same data. I am a file hardlinks johncurry$ cat softlink # works for now... I am a file
Because the inode is the same, we can move around both the hardlinks AND the softlinks without having to worry about breaking the link! We could even delete the original file, or the hardlink and still have the data, as long as there is one file/link that has the inode.
hardlinks johncurry$ mv normalFile.txt .. # move original file hardlinks johncurry$ cat softlink #softlink is broken now cat: softlink: No such file or directory hardlinks johncurry$ cat hardlink #hard link still works because the inode (map to data) is the same! I am a file hardlinks johncurry$ mkdir nestedFolder && mv hardlink nestedFolder && cd nestedFolder nestedFolder johncurry$ ls hardlink nestedFolder johncurry$ cat hardlink #hardlink still works!!! I am a file nestedFolder johncurry$ rm ../../normalFile.txt # we can even remove the original file! nestedFolder johncurry$ cat hardlink I am a file
With hardlinks you have the freedom to move the file AND any of the hardlinks around as you please, and you’ll still have access to the data without worrying about breaking the link.
Pros and Cons of hardlinks:
– Move both original file AND hardlinks around as you please
– Accesses the actual data
– Less likely to break
– cannot reach across file systems.
– DO NOT WORK with directories
– Need to delete ALL hardlinks and original file to remove file from system.
Pros and Cons of Soft Links
– CAN cross over to different file systems/partitions.
– Can point to directories
– Can point to non-existent objects
– Easier to break.
– Doesn’t access the data, it just creates a path to the original file
[/et_pb_text][et_pb_text admin_label=”Text – NewsLetter Email Opt in” _builder_version=”3.0.51″ background_layout=”light” text_orientation=”left” border_style=”solid” global_module=”1643″]
Want More Tutorials?
Subscribe to our NewsLetter to get our latest Tutorials, Courses, product & tool reviews, and more! We don't email very often. When we do, it'll be good!
[/et_pb_text][et_pb_text admin_label=”Text” _builder_version=”3.0.51″ background_layout=”light” text_orientation=”left” border_style=”solid” background_position=”top_left” background_repeat=”repeat” background_size=”initial”]
Symlinks are a neat feature with a big responsibility. You have to make sure you understand what your’e doing before using them, otherwise you may face some challenges. That said, they’re a powerful tool. I tried to be thorough, but I may have missed something. Is there any use case I missed, something I got wrong, or challenge I left out? How are you using Symlinks? Let me know in the comments!
[/et_pb_text][et_pb_cta admin_label=”Reading List CTA” _builder_version=”3.0.51″ title=”Reading Makes You Smarter!” button_text=”Get Smarter!” button_url=”https://truthseekers.io/books/” url_new_window=”off” use_background_color=”on” background_layout=”dark” border_style=”solid” custom_button=”off” button_letter_spacing=”0″ button_icon_placement=”right” button_letter_spacing_hover=”0″ background_position=”top_left” background_repeat=”repeat” background_size=”initial” use_background_color_gradient=”on” background_color_gradient_start=”#0e1ef4″ background_color_gradient_end=”#0f1fff” saved_tabs=”all” global_module=”930″]
Check out our recommended reading for anyone in the technology industry!