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
Dans ce numéro, je voulais me plonger dans les fichiers de démarrage, mais prenons un peu de recul et examinons les fichiers en général. (Oui, je sais que je fais des provisions, mais c'est un sujet étrange et intéressant.) Nous utilisons tous des fichiers, mais nous y pensons rarement. Il peut y avoir un fichier dans un dossier ou un fichier pointant vers un autre fichier dans un autre dossier. Cet article concerne uniquement bash, car je ne sais pas si cela est valable pour tous les shells. Je ne veux pas embrouiller les débutants, alors suivez-moi ; cela paraîtra confus, mais vous en sortirez avec une meilleure compréhension, promis.
Dans cet article, nous allons examiner les commandes « readlink » et « dirname », et comment les utiliser pour obtenir le chemin absolu d'un chemin relatif, et le répertoire contenant un chemin ; je vous mets au défi de le répéter trois fois de suite, le plus vite possible ! Si vous voulez le voir en action, voir : 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
Comment obtenir un chemin absolu ? Pour obtenir le chemin complet d'un fichier, on utilise la commande « readlink ». Taper « readlink » affiche le chemin absolu d'un lien symbolique, mais aussi, comme effet secondaire, le chemin absolu d'un chemin relatif. (Cela vous semble-t-il un peu confus ?) À ce stade, saisissez : man readlink - dans votre terminal et consultez la page de manuel. Pour voir des exemples, consultez : https://labex.io/tutorials/linux-how-to-resolve-symbolic-links-with-the-readlink-command-422882
Nous allons aborder ce sujet sous un angle légèrement différent, si vous le voulez bien. Si vous souhaitez simplement trouver votre fichier avec son chemin complet, je vous suggère de rechercher comme dans l'image en bas à gauche.
Plantons maintenant le contexte : nous allons utiliser readlink sur un fichier symbolique, comme dans l'image en bas à droite.
La commande readlink ne fonctionne que si vous avez le chemin complet ; C'est une évidence, car il est impossible de trouver un fichier qui n'existe pas.
La page de manuel indique : -f, –canonicalize, canoniser en suivant récursivement chaque lien symbolique dans chaque composant du nom donné ; tous les composants sauf le dernier doivent exister.
Si vous utilisiez « find », cela ne vous indiquerait pas que le fichier turquoise ci-dessus est un lien symbolique et renverrait :
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.
Pour être honnête, avant de faire des recherches pour cet article, je pensais, et je m'en souviendrai toujours, que l'option -f servait à « suivre » (follow en anglais), c'est-à-dire suivre le chemin complet.
Quoiqu'il en soit, pour poursuivre, dirname est une commande que je n'ai jamais utilisée directement, car elle n'avait aucun intérêt (un peu comme la commande « basename »). Elle est cependant utile dans les scripts. Consultez l'exemple dans l'image ci-dessous pour comprendre son rôle.
Elle vous indique le répertoire contenant votre répertoire. Mais (!), si je suis dans un dossier et que je veux vérifier le nom du répertoire d'un lien symbolique, cela devient intéressant (voir l'image en bas à droite).
OK, vous savez maintenant à quoi servent les deux commandes ; combinons-les (en haut à droite).
On peut aussi procéder autrement (au milieu à droite).
Excusez-moi pour toutes les captures d'écran, mais je dois afficher des éléments comme les guillemets inversés, qui ne fonctionnent tout simplement pas dans Abiword.
En essayant, nous avons réalisé que les fichiers et les liens symboliques se comportent un peu différemment à l'invite de commande. La commande « tree » ( image à droite) peut être un moyen utile de visualiser votre répertoire.
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.
Je voudrais maintenant que vous remarquiez que le lien symbolique pointe vers un autre dossier, mais que ce dossier n'était pas le chemin absolu.
Remuez les sourcils, vous comprenez ? Voyez-vous l'utilité d'obtenir un chemin absolu pour un script ?
C'est d'ailleurs une méthode que j'aime bien faire faire à mes élèves, pour qu'ils comprennent. J'espère que cette lecture vous donnera envie de l'essayer vous-même, pour que vous compreniez aussi.
Maintenant, mettons un grain de sable dans l'engrenage. Nous pouvons utiliser la commande « realpath » pour obtenir le chemin réel (elle fait ce qu'elle promet !).
Il existe évidemment d'autres moyens de contourner le problème, mais je m'arrête là, car cela deviendrait trop lourd en images et Ronnie rejèterait l'article.
Essayez en copiant un lien symbolique vers votre dossier personnel quelque part et en essayant ce qui précède.
Si j'ai fait une erreur, envoyez un e-mail à misc@fullcirclemagazine.org, car nous sommes tous des humains.