These Unix Spotlights are about some Unix commands that you might not know exist and a few things you can do with them.
ln makes "aliases" between files or locations.
Hard Links and Symbolic Links
A hard link takes a file on the file system (the actual data of the file) and associates it with a name. We see this all the time: you might have a file that says "hello world!" inside of it but we can find and access it through the name
hello.txt or whatever name you choose to give it. Technically, this is a directory entry which tells Unix, "Hey, if the user ever asks for
hello.txt, point them at this data, okay?"
Note that, while you can have multiple hard links on some file systems, we don't normally think of files as having more than one hard link. A symbolic link is a file which refers to something. This is only a reference, not a pointer at the data itself. Note also that this reference doesn't even have to exist: you could delete the data it's refering to, and the symbolic link still exists.
Most of the time we see symbolic links on directories, so let's do an example with that. You might have a long directory
~/this/is/a/very/long/name/ with a bunch of files in them. It would be nice if, instead, we could link it to a shorter directory. So, let's make one and see what happens. First, I'll make the
~/shorter directory. Then...
$ ls -l ~/this/is/a/very/long/name/ total 0 -rw-rw-r-- 1 james james 0 Feb 19 17:42 file1.txt -rw-rw-r-- 1 james james 0 Feb 19 17:42 file2.txt -rw-rw-r-- 1 james james 0 Feb 19 17:42 file3.sh # Here's where we make the link! # The notation is `ln -s [target_path] [link_path]` $ ln -s ~/this/is/a/very/long/name/ ~/shorter $ ls -l ~/shorter/ total 0 lrwxrwxrwx 1 james james 37 Feb 19 17:45 name -> /home/james/this/is/a/very/long/name/
IMPORTANT: Note the trailing / when we typed our target directory. This makes it clear it is a directory, not a file, for the link. If you do not include the slash, unexpected things will happen. Always use the ending slash when you mean a directory.
(Somewhat confusingly, though, we do not need to make the link path end in a slash.)
Note at the bottom this gives us the explicit symbolic link. What happens if I want to see what's on a file?
$ echo "hello world" >> ~/this/is/a/very/long/name/file1.txt $ cat ~/shorter/file1.txt hello world
Pretty much creates us a nice alias to use if we need to. This happens a bunch in development when we need to have a folder which is huge (like
node_modules for example) "pretend" like it's in a few places at once for the sake of a build system or something similar.
To remove the symbolic link, you can remove the directory you've created to link to the target directory; in this case we'd
rm -r ~/shorter.