Outils pour utilisateurs

Outils du site


issue219:tutoriel1

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

issue219:tutoriel1 [2025/07/17 18:04] – créée philou511issue219:tutoriel1 [2025/07/30 06:51] (Version actuelle) d52fr
Ligne 1: Ligne 1:
 +**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 issueI wanted to dive into startup files, but let’s take a step back and look at files in general. (Yes, I know Im squirrelingbut it is an odd and interesting one.) We all use files, but rarely think about them. We can have file in 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 confusingbut you will come away with better understanding, I promise.+In this articlewe will look at the ‘readlink’ and ‘dirname’ commands, and how they can be used to obtain the absolute path of relative path, and the directory containing path; I dare you to say that three times in a row, as fast as possible! If you want to see it in actionsee: https://stackoverflow.com/questions/3373132/get-the-name-of-the-directory-where-a-script-is-executed**
  
-In this articlewe will look at the ‘readlink’ and ‘dirname’ commandsand how they can be used to obtain the absolute path of a relative pathand the directory containing a path; I dare you to say that three times in a rowas fast as possible! If you want to see it in actionsee: https://stackoverflow.com/questions/3373132/get-the-name-of-the-directory-where-a-script-is-executed+Dans ce numéroje voulais me plonger dans les fichiers de démarragemais prenons un peu de recul et examinons les fichiers en général. (Ouije sais que je fais des provisionsmais c'est un sujet étrange et intéressant.) Nous utilisons tous des fichiersmais nous y pensons rarementIl 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.
  
-How do we get an absolute path you may ask? Wellto obtain the full path of a filewe use the ‘readlink’ command. Typing ‘readlink’ prints the absolute path of a symbolic linkbutas 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 exampleslook here: https://labex.io/tutorials/linux-how-to-resolve-symbolic-links-with-the-readlink-command-422882 +Dans cet articlenous allons examiner les commandes « readlink » et « dirname »et comment les utiliser pour obtenir le chemin absolu d'un chemin relatifet le répertoire contenant un chemin ; je vous mets au défi de le répéter trois fois de suitele plus vite possible ! Si vous voulez le voir en actionvoir : https://stackoverflow.com/questions/3373132/get-the-name-of-the-directory-where-a-script-is-executed
  
-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: 
  
 +**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 
  
-Now let us set the stage, we will use readlink on symbolic file, like so:+We will be discussing it at slightly different level or perspective, if you will. If you just want to find your file with a full pathI 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 readlink command works only if you have the full path; that is a given, as it cannot find a file that does not exist. 
Ligne 17: Ligne 19:
  
 If you were to use ‘find’, it would not tell you that the turquoise file above is a symbolic link and would return: 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" edd@gift:~$ find $PWD -name "index.theme"
 /home/edd/testfldr/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. + 
 +**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: 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.  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. OK, you now know what both commands do, let’s combine them.
- 
  
 We can also do it another way: 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. **
  
-GuysI apologise for all the screenshotsbut I need to show things like back ticksthat just do not work in Abiword.+Pour être honnêteavant de faire des recherches pour cet articleje pensaiset je m'en souviendrai toujours, que l'option -f servait à « suivre » (follow en anglais), c'est-à-dire suivre le chemin complet.
  
-As we played aroundwe realised that files and symbolic links behave a bit differently at the command promptThe ‘tree’ command can be a useful way to visualise your directory+Quoiqu'il en soitpour 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 scriptsConsultez 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).
  
-Now I want you to notice that the symbolic link points to another folder, but that folder was not the absolute path. +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? Wiggle your eyebrows, do you ‘get’ it? Do you see how it may be useful to get an absolute path for a script?
Ligne 54: Ligne 81:
 Please try it out, by copying a symbolic link to your home folder somewhere and try the above.  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.+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.
  
  
issue219/tutoriel1.txt · Dernière modification : 2025/07/30 06:51 de d52fr