{"id":6948,"date":"2018-09-28T13:56:07","date_gmt":"2018-09-28T10:56:07","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-readlink-realpath-command\/"},"modified":"2018-09-28T13:56:07","modified_gmt":"2018-09-28T10:56:07","slug":"linux-readlink-and-realpath-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-readlink-and-realpath-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux readlink and realpath Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<p>We&#8217;ve already discussed the <a href=\"https:\/\/www.howtoforge.com\/linux-ln-command\/\" target=\"_blank\" rel=\"noopener noreferrer\">ln command<\/a>, which lets you create links between files. But there are also tools that allow you to resolve these links.<\/p>\n<p>In this tutorial, we will discuss the basics of <strong>realpath<\/strong> and <strong>readlink<\/strong> commands that are similar (if not same) in the sense that they display resolved symbolic links in output. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on Ubuntu 18.04 LTS.<\/p>\n<h2 id=\"linux-realpath-and-readlink-commands\">Linux realpath and readlink commands<\/h2>\n<p>As already mentioned in the beginning, both realpath and readlink commands display resolved path for symlinks in the output.<\/p>\n<p>Following is their syntax:<\/p>\n<p class=\"command\">realpath [OPTION]&#8230; FILE&#8230;<\/p>\n<p class=\"command\">readlink [OPTION]&#8230; FILE&#8230;<\/p>\n<p>And here&#8217;s what their respective man pages say:<\/p>\n<pre>Print the resolved absolute file name<\/pre>\n<pre>print resolved symbolic links or canonical file names<\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a better idea on how these tools work.<\/p>\n<h2 id=\"q-how-to-resolve-path-using-realpath-and-readlink\">Q1. How to resolve path using realpath and readlink?<\/h2>\n<p>Simple, just provide the file or path as input. Following example shows both these commands resolving a symbolic link.<\/p>\n<p><a class=\"fancybox\" id=\"img-realpath-readlink-basics\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/realpath-readlink-basics.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-readlink-and-realpath-command-tutorial-for-beginners-with-examples.png\" alt=\"resolve link using realpath and readlink\" width=\"324\" height=\"72\" title=\"\"><\/a><\/p>\n<p>And here&#8217;s how they work with paths:<\/p>\n<p><a class=\"fancybox\" id=\"img-realpath-readlink-basics2\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/realpath-readlink-basics2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-readlink-and-realpath-command-tutorial-for-beginners-with-examples-1.png\" alt=\"resolve path using realpath and readlink\" width=\"490\" height=\"72\" title=\"\"><\/a><\/p>\n<p>So you can see both commands successfully resolved symlinks in the two cases.<\/p>\n<h2 id=\"q-how-to-print-resolved-path-relative-to-a-directory\">Q2. How to print resolved path relative to a directory?<\/h2>\n<p>The realpath command lets you easily do this. For example, suppose this is what you are trying to do:<\/p>\n<p class=\"command\">realpath \/var\/local\/Downloadslink<\/p>\n<p>However, an additional requirement is to have the output of this command relative to the \/home\/himanshu directory. Then following is the command that you need to execute:<\/p>\n<p class=\"command\">realpath \/var\/local\/Downloadslink &#8211;relative-to=\/home\/himanshu<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre>Downloads<\/pre>\n<p>So you can see that the &#8216;&#8211;relative-to&#8217; option allowed you to have output relative to the \/home\/himanshu directory.<\/p>\n<h2 id=\"q-how-to-just-get-rid-of-in-paths\">Q3. How to just get rid of .. in paths?<\/h2>\n<p>There may be times when expanding\/resolving symlinks isn&#8217;t the aim, but getting rid of .. in the path is. The realpath command allows you to do this.<\/p>\n<p>All you have to do is to use the -s command line option. Here&#8217;s an example:<\/p>\n<p class=\"command\">realpath -s ..\/..\/var\/local\/Downloadslink<\/p>\n<p>And following is the output of this command:<\/p>\n<pre>\/var\/local\/Downloadslink<\/pre>\n<h2 id=\"q-how-to-change-the-delimiting-character\">Q4. How to change the delimiting character?<\/h2>\n<p>By default, newline is used as the delimiting character. However, both commands provide way to have NUL as delimiter instead.<\/p>\n<p>The option you need to use in both cases is -z.<\/p>\n<p class=\"command\">realpath -z [FILE\/PATH]<\/p>\n<p class=\"command\">readlink -z [FILE\/PATH]<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>What we&#8217;ve discussed here are just a handful of options\/features these commands provide. Once you are done practicing these, head to readlink and realpath man pages (<a href=\"https:\/\/linux.die.net\/man\/1\/readlink\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a> and <a href=\"https:\/\/linux.die.net\/man\/1\/realpath\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>) to learn more about these tools.<\/p>\n<div class=\"authorbox\" readability=\"15\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-readlink-and-realpath-command-tutorial-for-beginners-with-examples.jpg\" alt=\"Himanshu Arora\" title=\"\"> <\/p>\n<p><strong>About Himanshu Arora<\/strong><\/p>\n<p>Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu&#8217;s work has also been featured in some of world&#8217;s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.<\/p>\n<\/div>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-readlink-realpath-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-readlink-and-realpath-command-tutorial-for-beginners-with-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-readlink-realpath-command%2F&amp;text=Linux+readlink+and+realpath+Command+Tutorial+for+Beginners+%28with+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-readlink-and-realpath-command-tutorial-for-beginners-with-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-readlink-and-realpath-command-tutorial-for-beginners-with-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-readlink-realpath-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-readlink-and-realpath-command-tutorial-for-beginners-with-examples-5.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve already discussed the ln command, which lets you create links between files. But there are also tools that allow you to resolve these links. In this tutorial, we will discuss the basics of realpath and readlink commands that are similar (if not same) in the sense that they display resolved symbolic links in output. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-6948","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=6948"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6948\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=6948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=6948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=6948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}