In this issue, I wanted to dive into startup files, but let’s take a step back and look at files in general. (Yes, I know I’m squirreling, but it is an odd and interesting one.) We all use files, but rarely think about them. We can have a file in a folder or a file pointing to another file in another folder. This article is for bash only, as I don’t know if this is valid for all shells. I don’t want to confuse any newbies, so just forge ahead with me, it will sound confusing, but you will come away with a better understanding, I promise.
In this article, we will look at the ‘readlink’ and ‘dirname’ commands, and how they can be used to obtain the absolute path of a relative path, and the directory containing a path; I dare you to say that three times in a row, as fast as possible! If you want to see it in action, see: https://stackoverflow.com/questions/3373132/get-the-name-of-the-directory-where-a-script-is-executed
How do we get an absolute path you may ask? Well, to obtain the full path of a file, we use the ‘readlink’ command. Typing ‘readlink’ prints the absolute path of a symbolic link, but, as a side-effect, it also prints the absolute path for a relative path. (sound confusing yet?). At this stage, please type: man readlink - in your terminal, and have a look at the man-page. If you would like to see examples, look here: https://labex.io/tutorials/linux-how-to-resolve-symbolic-links-with-the-readlink-command-422882
We will be discussing it at a slightly different level or perspective, if you will. If you just want to find your file with a full path, I suggest find as in the image below:
Now let us set the stage, we will use readlink on a symbolic file, like so:
The readlink command works only if you have the full path; that is a given, as it cannot find a file that does not exist.
The man page tells us: -f, –canonicalize, canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist
If you were to use ‘find’, it would not tell you that the turquoise file above is a symbolic link and would return:
edd@gift:~$ find $PWD -name “index.theme” /home/edd/testfldr/index.theme
Now I will be honest; before researching this for the article, I thought, and will remember it this way forever, that the -f flag was for “follow” as in follow the full path.
Anyway, moving forward, dirname is a command I never used directly, as it had no point (sort of like the ‘basename’ command). It is useful in scripts though. See the following example to get an understanding of what it does:
It gives you the directory, containing your directory. But(!), if I am in a folder and I want to check the directory name of a symbolic link, it starts to get interesting.
OK, you now know what both commands do, let’s combine them.
We can also do it another way:
Guys, I apologise for all the screenshots, but I need to show things like back ticks, that just do not work in Abiword.
As we played around, we realised that files and symbolic links behave a bit differently at the command prompt. The ‘tree’ command can be a useful way to visualise your directory.
Now I want you to notice that the symbolic link points to another folder, but that folder was not the absolute path.
Wiggle your eyebrows, do you ‘get’ it? Do you see how it may be useful to get an absolute path for a script?
This is actually something I like to get students to do, so that the penny drops. I hope reading about it inspires you to try it yourself, so that the penny will drop for you too.
Now let’s put a spanner in the works. We can use the ‘realpath’ command to get the real path (it does what it says on the tin!).
There are obviously other ways to skin the cat, but I’ll stop here, as it would become too image-heavy and Ronnie will reject it.
Please try it out, by copying a symbolic link to your home folder somewhere and try the above.
If I made a mistake anywhere, misc@fullcirclemagazine.org as we are all human.