What's the difference between “realpath” and “readlink -f”












54














I've read a lot about realpath command and how it has been deprecated with the readlink -f being now recommended. I have also seen in some places that the reason why realpath was introduced was for the lack of such functionality in readlink and that once it has been introduced, realpath was no longer needed and its support discontinued by most OS vendors.



The reason for my question is that I've also seen many people recommending readlink -f as a command "pretty much similar" to realpath, and that is what is bothering me, because no one elaborates on that "pretty much similar" part. What are the actual differences?










share|improve this question



























    54














    I've read a lot about realpath command and how it has been deprecated with the readlink -f being now recommended. I have also seen in some places that the reason why realpath was introduced was for the lack of such functionality in readlink and that once it has been introduced, realpath was no longer needed and its support discontinued by most OS vendors.



    The reason for my question is that I've also seen many people recommending readlink -f as a command "pretty much similar" to realpath, and that is what is bothering me, because no one elaborates on that "pretty much similar" part. What are the actual differences?










    share|improve this question

























      54












      54








      54


      9





      I've read a lot about realpath command and how it has been deprecated with the readlink -f being now recommended. I have also seen in some places that the reason why realpath was introduced was for the lack of such functionality in readlink and that once it has been introduced, realpath was no longer needed and its support discontinued by most OS vendors.



      The reason for my question is that I've also seen many people recommending readlink -f as a command "pretty much similar" to realpath, and that is what is bothering me, because no one elaborates on that "pretty much similar" part. What are the actual differences?










      share|improve this question













      I've read a lot about realpath command and how it has been deprecated with the readlink -f being now recommended. I have also seen in some places that the reason why realpath was introduced was for the lack of such functionality in readlink and that once it has been introduced, realpath was no longer needed and its support discontinued by most OS vendors.



      The reason for my question is that I've also seen many people recommending readlink -f as a command "pretty much similar" to realpath, and that is what is bothering me, because no one elaborates on that "pretty much similar" part. What are the actual differences?







      shell command






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 10 '14 at 21:01









      Felipe Leão

      400147




      400147






















          2 Answers
          2






          active

          oldest

          votes


















          58














          There are several realpath commands around.



          The realpath utility is a wrapper around the realpath library functions and has been reinvented many times.



          Debian maintains a realpath package (separated from dwww since woody) which hasn't changed except regarding packaging and documentation since 2001. This utility is deprecated because there are now more standard alternatives (GNU readlink and soon GNU realpath), but at the time, GNU utilities didn't even have readlink at all. This implementation of realpath supports a few options to prevent symbolic link resolution or produce null-terminated output.
          BusyBox also includes its own realpath command (which takes no option).



          GNU coreutils introduced a realpath command in version 8.15 in January 2012. This is a compatible replacement for BusyBox's and Debian's realpath, and also has many options in common with GNU readlink.



          realpath has the same effect as readlink -f with GNU readlink. What distinguishes the two commands (or rather the various realpath commands from readlink -f) is the extra options that they support.



          GNU realpath is not deprecated; it has the opposite problem: it's too new to be available everywhere. At this time, Debian still omits GNU realpath from its coreutils package and sticks with its own realpath. I don't know why, since GNU realpath should be a drop-in replacement. As of 14.04, Ubuntu doesn't gainsay Debian on this.



          On Linux systems, at the moment, your best bet to canonicalize a path that may contain symbolic links is readlink -f.



          BSD systems have a readlink command, with different capabilities from GNU readlink. In particular, BSD readlink does not have an option to canonicalize paths, it only traverses the symlink passed to it.



          readlink, incidentally, had the same problem — it was also invented many times (not adding this utility when symbolic links were added to Unix was a regrettable omission). It has now stabilized in several implementations with many incompatible flags (in particular BSD vs. GNU).






          share|improve this answer



















          • 7




            readlink -f was in OpenBSD long before GNU. All of NetBSD, FreeBSD and OpenBSD now have readlink -f (your link even mentions it). realpath has been in FreeBSD and IRIX for a long time (don't know if it predates the one in Debian). HPUX and IRIX also have readlink, though not -f. The realpath package in Debian experimental is now the one from coreutils (as an experiment to see if it breaks things). The dwww realpath acts more like readlink -e while the GNU one like readlink -f so it's not a complete dropin replace
            – Stéphane Chazelas
            Jun 11 '14 at 8:22








          • 2




            realpath has been in FreeBSD since 2002. Before that, pwd was doing it (since 2000, pwd some-file would call realpath() on file). Debian has had a realpath package since 1996. The one on IRIX probably predates it though I found no evidence other than it was in IRIX 6.5 in 1998. OpenBSD added a -f to readlink in 1997. GNU added readlink in 2003 and it had -f from the start.
            – Stéphane Chazelas
            Jun 11 '14 at 9:09






          • 2




            Excellent summary thanks. Note a better bug for the request for debian to switch to the GNU variant is bugs.debian.org/730779 Even the existing realpath maintainer wants the switch to happen
            – Pádraig Brady
            Jun 11 '14 at 12:46








          • 1




            Awesome answer. Missed only references to RHEL implementation of realpath. Does anyone know if it is somehow different from the readlink -f version?
            – Felipe Leão
            Jun 11 '14 at 12:50






          • 1




            @StéphaneChazelas Oh wow, lots of errors in my answers indeed. Thanks for pointing them out. Could you post a correct answer, and I'll delete mine? (Otherwise, I'll correct the errors, but it's more work to tack on to my long todo list…)
            – Gilles
            Jun 11 '14 at 18:05



















          12














          tl;dr readlink -f will return 0 for a non-existent file in an existing directory whereas realpath returns 1. However, readlink -e will behave like realpath and return 1 for a non-existent file (see Editors note at end).



          readlink -f



          $ readlink -f non-existent-file
          /home/user/non-existent-file
          $ echo $?
          0


          readlink -e



          $ readlink -e non-existent-file
          $ echo $?
          1


          realpath



          $ realpath non-existent-file
          non-existent-file: No such file or directory
          $ echo $?
          1



          readlink -f with non-existent directory



          readlink -f behavior varies depending upon which part of the path does not exist.



          $ readlink -f /tmp/non-existent-dir/foo
          $ echo $?
          1


          In summary



          If you want to replace calls to realpath ... then use readlink -e ....






          Tested with readlink (GNU coreutils) 8.21 and realpath version 1.19.



          (Ed.: @AnthonyGeoghegan wrote "this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f")






          share|improve this answer



















          • 3




            I upvoted this answer but I'd suggest clarifying that this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f.
            – Anthony Geoghegan
            Oct 5 '16 at 15:31






          • 1




            Can confirm that this doesn't work on MacOS High Sierra.
            – Pred
            Sep 18 at 21:07











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f136494%2fwhats-the-difference-between-realpath-and-readlink-f%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          58














          There are several realpath commands around.



          The realpath utility is a wrapper around the realpath library functions and has been reinvented many times.



          Debian maintains a realpath package (separated from dwww since woody) which hasn't changed except regarding packaging and documentation since 2001. This utility is deprecated because there are now more standard alternatives (GNU readlink and soon GNU realpath), but at the time, GNU utilities didn't even have readlink at all. This implementation of realpath supports a few options to prevent symbolic link resolution or produce null-terminated output.
          BusyBox also includes its own realpath command (which takes no option).



          GNU coreutils introduced a realpath command in version 8.15 in January 2012. This is a compatible replacement for BusyBox's and Debian's realpath, and also has many options in common with GNU readlink.



          realpath has the same effect as readlink -f with GNU readlink. What distinguishes the two commands (or rather the various realpath commands from readlink -f) is the extra options that they support.



          GNU realpath is not deprecated; it has the opposite problem: it's too new to be available everywhere. At this time, Debian still omits GNU realpath from its coreutils package and sticks with its own realpath. I don't know why, since GNU realpath should be a drop-in replacement. As of 14.04, Ubuntu doesn't gainsay Debian on this.



          On Linux systems, at the moment, your best bet to canonicalize a path that may contain symbolic links is readlink -f.



          BSD systems have a readlink command, with different capabilities from GNU readlink. In particular, BSD readlink does not have an option to canonicalize paths, it only traverses the symlink passed to it.



          readlink, incidentally, had the same problem — it was also invented many times (not adding this utility when symbolic links were added to Unix was a regrettable omission). It has now stabilized in several implementations with many incompatible flags (in particular BSD vs. GNU).






          share|improve this answer



















          • 7




            readlink -f was in OpenBSD long before GNU. All of NetBSD, FreeBSD and OpenBSD now have readlink -f (your link even mentions it). realpath has been in FreeBSD and IRIX for a long time (don't know if it predates the one in Debian). HPUX and IRIX also have readlink, though not -f. The realpath package in Debian experimental is now the one from coreutils (as an experiment to see if it breaks things). The dwww realpath acts more like readlink -e while the GNU one like readlink -f so it's not a complete dropin replace
            – Stéphane Chazelas
            Jun 11 '14 at 8:22








          • 2




            realpath has been in FreeBSD since 2002. Before that, pwd was doing it (since 2000, pwd some-file would call realpath() on file). Debian has had a realpath package since 1996. The one on IRIX probably predates it though I found no evidence other than it was in IRIX 6.5 in 1998. OpenBSD added a -f to readlink in 1997. GNU added readlink in 2003 and it had -f from the start.
            – Stéphane Chazelas
            Jun 11 '14 at 9:09






          • 2




            Excellent summary thanks. Note a better bug for the request for debian to switch to the GNU variant is bugs.debian.org/730779 Even the existing realpath maintainer wants the switch to happen
            – Pádraig Brady
            Jun 11 '14 at 12:46








          • 1




            Awesome answer. Missed only references to RHEL implementation of realpath. Does anyone know if it is somehow different from the readlink -f version?
            – Felipe Leão
            Jun 11 '14 at 12:50






          • 1




            @StéphaneChazelas Oh wow, lots of errors in my answers indeed. Thanks for pointing them out. Could you post a correct answer, and I'll delete mine? (Otherwise, I'll correct the errors, but it's more work to tack on to my long todo list…)
            – Gilles
            Jun 11 '14 at 18:05
















          58














          There are several realpath commands around.



          The realpath utility is a wrapper around the realpath library functions and has been reinvented many times.



          Debian maintains a realpath package (separated from dwww since woody) which hasn't changed except regarding packaging and documentation since 2001. This utility is deprecated because there are now more standard alternatives (GNU readlink and soon GNU realpath), but at the time, GNU utilities didn't even have readlink at all. This implementation of realpath supports a few options to prevent symbolic link resolution or produce null-terminated output.
          BusyBox also includes its own realpath command (which takes no option).



          GNU coreutils introduced a realpath command in version 8.15 in January 2012. This is a compatible replacement for BusyBox's and Debian's realpath, and also has many options in common with GNU readlink.



          realpath has the same effect as readlink -f with GNU readlink. What distinguishes the two commands (or rather the various realpath commands from readlink -f) is the extra options that they support.



          GNU realpath is not deprecated; it has the opposite problem: it's too new to be available everywhere. At this time, Debian still omits GNU realpath from its coreutils package and sticks with its own realpath. I don't know why, since GNU realpath should be a drop-in replacement. As of 14.04, Ubuntu doesn't gainsay Debian on this.



          On Linux systems, at the moment, your best bet to canonicalize a path that may contain symbolic links is readlink -f.



          BSD systems have a readlink command, with different capabilities from GNU readlink. In particular, BSD readlink does not have an option to canonicalize paths, it only traverses the symlink passed to it.



          readlink, incidentally, had the same problem — it was also invented many times (not adding this utility when symbolic links were added to Unix was a regrettable omission). It has now stabilized in several implementations with many incompatible flags (in particular BSD vs. GNU).






          share|improve this answer



















          • 7




            readlink -f was in OpenBSD long before GNU. All of NetBSD, FreeBSD and OpenBSD now have readlink -f (your link even mentions it). realpath has been in FreeBSD and IRIX for a long time (don't know if it predates the one in Debian). HPUX and IRIX also have readlink, though not -f. The realpath package in Debian experimental is now the one from coreutils (as an experiment to see if it breaks things). The dwww realpath acts more like readlink -e while the GNU one like readlink -f so it's not a complete dropin replace
            – Stéphane Chazelas
            Jun 11 '14 at 8:22








          • 2




            realpath has been in FreeBSD since 2002. Before that, pwd was doing it (since 2000, pwd some-file would call realpath() on file). Debian has had a realpath package since 1996. The one on IRIX probably predates it though I found no evidence other than it was in IRIX 6.5 in 1998. OpenBSD added a -f to readlink in 1997. GNU added readlink in 2003 and it had -f from the start.
            – Stéphane Chazelas
            Jun 11 '14 at 9:09






          • 2




            Excellent summary thanks. Note a better bug for the request for debian to switch to the GNU variant is bugs.debian.org/730779 Even the existing realpath maintainer wants the switch to happen
            – Pádraig Brady
            Jun 11 '14 at 12:46








          • 1




            Awesome answer. Missed only references to RHEL implementation of realpath. Does anyone know if it is somehow different from the readlink -f version?
            – Felipe Leão
            Jun 11 '14 at 12:50






          • 1




            @StéphaneChazelas Oh wow, lots of errors in my answers indeed. Thanks for pointing them out. Could you post a correct answer, and I'll delete mine? (Otherwise, I'll correct the errors, but it's more work to tack on to my long todo list…)
            – Gilles
            Jun 11 '14 at 18:05














          58












          58








          58






          There are several realpath commands around.



          The realpath utility is a wrapper around the realpath library functions and has been reinvented many times.



          Debian maintains a realpath package (separated from dwww since woody) which hasn't changed except regarding packaging and documentation since 2001. This utility is deprecated because there are now more standard alternatives (GNU readlink and soon GNU realpath), but at the time, GNU utilities didn't even have readlink at all. This implementation of realpath supports a few options to prevent symbolic link resolution or produce null-terminated output.
          BusyBox also includes its own realpath command (which takes no option).



          GNU coreutils introduced a realpath command in version 8.15 in January 2012. This is a compatible replacement for BusyBox's and Debian's realpath, and also has many options in common with GNU readlink.



          realpath has the same effect as readlink -f with GNU readlink. What distinguishes the two commands (or rather the various realpath commands from readlink -f) is the extra options that they support.



          GNU realpath is not deprecated; it has the opposite problem: it's too new to be available everywhere. At this time, Debian still omits GNU realpath from its coreutils package and sticks with its own realpath. I don't know why, since GNU realpath should be a drop-in replacement. As of 14.04, Ubuntu doesn't gainsay Debian on this.



          On Linux systems, at the moment, your best bet to canonicalize a path that may contain symbolic links is readlink -f.



          BSD systems have a readlink command, with different capabilities from GNU readlink. In particular, BSD readlink does not have an option to canonicalize paths, it only traverses the symlink passed to it.



          readlink, incidentally, had the same problem — it was also invented many times (not adding this utility when symbolic links were added to Unix was a regrettable omission). It has now stabilized in several implementations with many incompatible flags (in particular BSD vs. GNU).






          share|improve this answer














          There are several realpath commands around.



          The realpath utility is a wrapper around the realpath library functions and has been reinvented many times.



          Debian maintains a realpath package (separated from dwww since woody) which hasn't changed except regarding packaging and documentation since 2001. This utility is deprecated because there are now more standard alternatives (GNU readlink and soon GNU realpath), but at the time, GNU utilities didn't even have readlink at all. This implementation of realpath supports a few options to prevent symbolic link resolution or produce null-terminated output.
          BusyBox also includes its own realpath command (which takes no option).



          GNU coreutils introduced a realpath command in version 8.15 in January 2012. This is a compatible replacement for BusyBox's and Debian's realpath, and also has many options in common with GNU readlink.



          realpath has the same effect as readlink -f with GNU readlink. What distinguishes the two commands (or rather the various realpath commands from readlink -f) is the extra options that they support.



          GNU realpath is not deprecated; it has the opposite problem: it's too new to be available everywhere. At this time, Debian still omits GNU realpath from its coreutils package and sticks with its own realpath. I don't know why, since GNU realpath should be a drop-in replacement. As of 14.04, Ubuntu doesn't gainsay Debian on this.



          On Linux systems, at the moment, your best bet to canonicalize a path that may contain symbolic links is readlink -f.



          BSD systems have a readlink command, with different capabilities from GNU readlink. In particular, BSD readlink does not have an option to canonicalize paths, it only traverses the symlink passed to it.



          readlink, incidentally, had the same problem — it was also invented many times (not adding this utility when symbolic links were added to Unix was a regrettable omission). It has now stabilized in several implementations with many incompatible flags (in particular BSD vs. GNU).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 11 '14 at 2:22

























          answered Jun 11 '14 at 2:05









          Gilles

          527k12710561580




          527k12710561580








          • 7




            readlink -f was in OpenBSD long before GNU. All of NetBSD, FreeBSD and OpenBSD now have readlink -f (your link even mentions it). realpath has been in FreeBSD and IRIX for a long time (don't know if it predates the one in Debian). HPUX and IRIX also have readlink, though not -f. The realpath package in Debian experimental is now the one from coreutils (as an experiment to see if it breaks things). The dwww realpath acts more like readlink -e while the GNU one like readlink -f so it's not a complete dropin replace
            – Stéphane Chazelas
            Jun 11 '14 at 8:22








          • 2




            realpath has been in FreeBSD since 2002. Before that, pwd was doing it (since 2000, pwd some-file would call realpath() on file). Debian has had a realpath package since 1996. The one on IRIX probably predates it though I found no evidence other than it was in IRIX 6.5 in 1998. OpenBSD added a -f to readlink in 1997. GNU added readlink in 2003 and it had -f from the start.
            – Stéphane Chazelas
            Jun 11 '14 at 9:09






          • 2




            Excellent summary thanks. Note a better bug for the request for debian to switch to the GNU variant is bugs.debian.org/730779 Even the existing realpath maintainer wants the switch to happen
            – Pádraig Brady
            Jun 11 '14 at 12:46








          • 1




            Awesome answer. Missed only references to RHEL implementation of realpath. Does anyone know if it is somehow different from the readlink -f version?
            – Felipe Leão
            Jun 11 '14 at 12:50






          • 1




            @StéphaneChazelas Oh wow, lots of errors in my answers indeed. Thanks for pointing them out. Could you post a correct answer, and I'll delete mine? (Otherwise, I'll correct the errors, but it's more work to tack on to my long todo list…)
            – Gilles
            Jun 11 '14 at 18:05














          • 7




            readlink -f was in OpenBSD long before GNU. All of NetBSD, FreeBSD and OpenBSD now have readlink -f (your link even mentions it). realpath has been in FreeBSD and IRIX for a long time (don't know if it predates the one in Debian). HPUX and IRIX also have readlink, though not -f. The realpath package in Debian experimental is now the one from coreutils (as an experiment to see if it breaks things). The dwww realpath acts more like readlink -e while the GNU one like readlink -f so it's not a complete dropin replace
            – Stéphane Chazelas
            Jun 11 '14 at 8:22








          • 2




            realpath has been in FreeBSD since 2002. Before that, pwd was doing it (since 2000, pwd some-file would call realpath() on file). Debian has had a realpath package since 1996. The one on IRIX probably predates it though I found no evidence other than it was in IRIX 6.5 in 1998. OpenBSD added a -f to readlink in 1997. GNU added readlink in 2003 and it had -f from the start.
            – Stéphane Chazelas
            Jun 11 '14 at 9:09






          • 2




            Excellent summary thanks. Note a better bug for the request for debian to switch to the GNU variant is bugs.debian.org/730779 Even the existing realpath maintainer wants the switch to happen
            – Pádraig Brady
            Jun 11 '14 at 12:46








          • 1




            Awesome answer. Missed only references to RHEL implementation of realpath. Does anyone know if it is somehow different from the readlink -f version?
            – Felipe Leão
            Jun 11 '14 at 12:50






          • 1




            @StéphaneChazelas Oh wow, lots of errors in my answers indeed. Thanks for pointing them out. Could you post a correct answer, and I'll delete mine? (Otherwise, I'll correct the errors, but it's more work to tack on to my long todo list…)
            – Gilles
            Jun 11 '14 at 18:05








          7




          7




          readlink -f was in OpenBSD long before GNU. All of NetBSD, FreeBSD and OpenBSD now have readlink -f (your link even mentions it). realpath has been in FreeBSD and IRIX for a long time (don't know if it predates the one in Debian). HPUX and IRIX also have readlink, though not -f. The realpath package in Debian experimental is now the one from coreutils (as an experiment to see if it breaks things). The dwww realpath acts more like readlink -e while the GNU one like readlink -f so it's not a complete dropin replace
          – Stéphane Chazelas
          Jun 11 '14 at 8:22






          readlink -f was in OpenBSD long before GNU. All of NetBSD, FreeBSD and OpenBSD now have readlink -f (your link even mentions it). realpath has been in FreeBSD and IRIX for a long time (don't know if it predates the one in Debian). HPUX and IRIX also have readlink, though not -f. The realpath package in Debian experimental is now the one from coreutils (as an experiment to see if it breaks things). The dwww realpath acts more like readlink -e while the GNU one like readlink -f so it's not a complete dropin replace
          – Stéphane Chazelas
          Jun 11 '14 at 8:22






          2




          2




          realpath has been in FreeBSD since 2002. Before that, pwd was doing it (since 2000, pwd some-file would call realpath() on file). Debian has had a realpath package since 1996. The one on IRIX probably predates it though I found no evidence other than it was in IRIX 6.5 in 1998. OpenBSD added a -f to readlink in 1997. GNU added readlink in 2003 and it had -f from the start.
          – Stéphane Chazelas
          Jun 11 '14 at 9:09




          realpath has been in FreeBSD since 2002. Before that, pwd was doing it (since 2000, pwd some-file would call realpath() on file). Debian has had a realpath package since 1996. The one on IRIX probably predates it though I found no evidence other than it was in IRIX 6.5 in 1998. OpenBSD added a -f to readlink in 1997. GNU added readlink in 2003 and it had -f from the start.
          – Stéphane Chazelas
          Jun 11 '14 at 9:09




          2




          2




          Excellent summary thanks. Note a better bug for the request for debian to switch to the GNU variant is bugs.debian.org/730779 Even the existing realpath maintainer wants the switch to happen
          – Pádraig Brady
          Jun 11 '14 at 12:46






          Excellent summary thanks. Note a better bug for the request for debian to switch to the GNU variant is bugs.debian.org/730779 Even the existing realpath maintainer wants the switch to happen
          – Pádraig Brady
          Jun 11 '14 at 12:46






          1




          1




          Awesome answer. Missed only references to RHEL implementation of realpath. Does anyone know if it is somehow different from the readlink -f version?
          – Felipe Leão
          Jun 11 '14 at 12:50




          Awesome answer. Missed only references to RHEL implementation of realpath. Does anyone know if it is somehow different from the readlink -f version?
          – Felipe Leão
          Jun 11 '14 at 12:50




          1




          1




          @StéphaneChazelas Oh wow, lots of errors in my answers indeed. Thanks for pointing them out. Could you post a correct answer, and I'll delete mine? (Otherwise, I'll correct the errors, but it's more work to tack on to my long todo list…)
          – Gilles
          Jun 11 '14 at 18:05




          @StéphaneChazelas Oh wow, lots of errors in my answers indeed. Thanks for pointing them out. Could you post a correct answer, and I'll delete mine? (Otherwise, I'll correct the errors, but it's more work to tack on to my long todo list…)
          – Gilles
          Jun 11 '14 at 18:05













          12














          tl;dr readlink -f will return 0 for a non-existent file in an existing directory whereas realpath returns 1. However, readlink -e will behave like realpath and return 1 for a non-existent file (see Editors note at end).



          readlink -f



          $ readlink -f non-existent-file
          /home/user/non-existent-file
          $ echo $?
          0


          readlink -e



          $ readlink -e non-existent-file
          $ echo $?
          1


          realpath



          $ realpath non-existent-file
          non-existent-file: No such file or directory
          $ echo $?
          1



          readlink -f with non-existent directory



          readlink -f behavior varies depending upon which part of the path does not exist.



          $ readlink -f /tmp/non-existent-dir/foo
          $ echo $?
          1


          In summary



          If you want to replace calls to realpath ... then use readlink -e ....






          Tested with readlink (GNU coreutils) 8.21 and realpath version 1.19.



          (Ed.: @AnthonyGeoghegan wrote "this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f")






          share|improve this answer



















          • 3




            I upvoted this answer but I'd suggest clarifying that this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f.
            – Anthony Geoghegan
            Oct 5 '16 at 15:31






          • 1




            Can confirm that this doesn't work on MacOS High Sierra.
            – Pred
            Sep 18 at 21:07
















          12














          tl;dr readlink -f will return 0 for a non-existent file in an existing directory whereas realpath returns 1. However, readlink -e will behave like realpath and return 1 for a non-existent file (see Editors note at end).



          readlink -f



          $ readlink -f non-existent-file
          /home/user/non-existent-file
          $ echo $?
          0


          readlink -e



          $ readlink -e non-existent-file
          $ echo $?
          1


          realpath



          $ realpath non-existent-file
          non-existent-file: No such file or directory
          $ echo $?
          1



          readlink -f with non-existent directory



          readlink -f behavior varies depending upon which part of the path does not exist.



          $ readlink -f /tmp/non-existent-dir/foo
          $ echo $?
          1


          In summary



          If you want to replace calls to realpath ... then use readlink -e ....






          Tested with readlink (GNU coreutils) 8.21 and realpath version 1.19.



          (Ed.: @AnthonyGeoghegan wrote "this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f")






          share|improve this answer



















          • 3




            I upvoted this answer but I'd suggest clarifying that this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f.
            – Anthony Geoghegan
            Oct 5 '16 at 15:31






          • 1




            Can confirm that this doesn't work on MacOS High Sierra.
            – Pred
            Sep 18 at 21:07














          12












          12








          12






          tl;dr readlink -f will return 0 for a non-existent file in an existing directory whereas realpath returns 1. However, readlink -e will behave like realpath and return 1 for a non-existent file (see Editors note at end).



          readlink -f



          $ readlink -f non-existent-file
          /home/user/non-existent-file
          $ echo $?
          0


          readlink -e



          $ readlink -e non-existent-file
          $ echo $?
          1


          realpath



          $ realpath non-existent-file
          non-existent-file: No such file or directory
          $ echo $?
          1



          readlink -f with non-existent directory



          readlink -f behavior varies depending upon which part of the path does not exist.



          $ readlink -f /tmp/non-existent-dir/foo
          $ echo $?
          1


          In summary



          If you want to replace calls to realpath ... then use readlink -e ....






          Tested with readlink (GNU coreutils) 8.21 and realpath version 1.19.



          (Ed.: @AnthonyGeoghegan wrote "this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f")






          share|improve this answer














          tl;dr readlink -f will return 0 for a non-existent file in an existing directory whereas realpath returns 1. However, readlink -e will behave like realpath and return 1 for a non-existent file (see Editors note at end).



          readlink -f



          $ readlink -f non-existent-file
          /home/user/non-existent-file
          $ echo $?
          0


          readlink -e



          $ readlink -e non-existent-file
          $ echo $?
          1


          realpath



          $ realpath non-existent-file
          non-existent-file: No such file or directory
          $ echo $?
          1



          readlink -f with non-existent directory



          readlink -f behavior varies depending upon which part of the path does not exist.



          $ readlink -f /tmp/non-existent-dir/foo
          $ echo $?
          1


          In summary



          If you want to replace calls to realpath ... then use readlink -e ....






          Tested with readlink (GNU coreutils) 8.21 and realpath version 1.19.



          (Ed.: @AnthonyGeoghegan wrote "this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f")







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 9 at 23:44

























          answered Dec 2 '15 at 22:32









          JamesThomasMoon1979

          257210




          257210








          • 3




            I upvoted this answer but I'd suggest clarifying that this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f.
            – Anthony Geoghegan
            Oct 5 '16 at 15:31






          • 1




            Can confirm that this doesn't work on MacOS High Sierra.
            – Pred
            Sep 18 at 21:07














          • 3




            I upvoted this answer but I'd suggest clarifying that this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f.
            – Anthony Geoghegan
            Oct 5 '16 at 15:31






          • 1




            Can confirm that this doesn't work on MacOS High Sierra.
            – Pred
            Sep 18 at 21:07








          3




          3




          I upvoted this answer but I'd suggest clarifying that this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f.
          – Anthony Geoghegan
          Oct 5 '16 at 15:31




          I upvoted this answer but I'd suggest clarifying that this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f.
          – Anthony Geoghegan
          Oct 5 '16 at 15:31




          1




          1




          Can confirm that this doesn't work on MacOS High Sierra.
          – Pred
          Sep 18 at 21:07




          Can confirm that this doesn't work on MacOS High Sierra.
          – Pred
          Sep 18 at 21:07


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f136494%2fwhats-the-difference-between-realpath-and-readlink-f%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Morgemoulin

          Scott Moir

          Souastre