Substitute ./ and ../ directories by actual names

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












4















I have a script which finds files in the directory specified by user.



#!/bin/bash

# make sure about the correct input
if [ -z $1 ]
then
echo "Usage: ./script_name.sh path/to/directory"
else
DIR=$1
if [ $DIR = '.' ]
then
echo "Find files in the directory $PWD"
else
echo "Find files in the directory $DIR"
fi

find $DIR -type f -exec basename ;
fi


if I input



$ ./script_name.sh .


script gives me correct substitution ./ to $PWD and shows (for example)



$ Find files in the directory /root/scripts 


But I can't make a decision how to substitute ../ to the name of the directory immediately above in the hierarchy. If I input



$ ./script_name.sh .. 


script gives me the output



$ Find files in the directory .. 


Does anybody know how to substitute ../ to the actual name of the directory?










share|improve this question
























  • Try the realpath command if it is available.

    – Bodo
    Feb 19 at 14:33











  • @Bodo realpath works good in this case.

    – Alex Kuchin
    Feb 19 at 14:59
















4















I have a script which finds files in the directory specified by user.



#!/bin/bash

# make sure about the correct input
if [ -z $1 ]
then
echo "Usage: ./script_name.sh path/to/directory"
else
DIR=$1
if [ $DIR = '.' ]
then
echo "Find files in the directory $PWD"
else
echo "Find files in the directory $DIR"
fi

find $DIR -type f -exec basename ;
fi


if I input



$ ./script_name.sh .


script gives me correct substitution ./ to $PWD and shows (for example)



$ Find files in the directory /root/scripts 


But I can't make a decision how to substitute ../ to the name of the directory immediately above in the hierarchy. If I input



$ ./script_name.sh .. 


script gives me the output



$ Find files in the directory .. 


Does anybody know how to substitute ../ to the actual name of the directory?










share|improve this question
























  • Try the realpath command if it is available.

    – Bodo
    Feb 19 at 14:33











  • @Bodo realpath works good in this case.

    – Alex Kuchin
    Feb 19 at 14:59














4












4








4


1






I have a script which finds files in the directory specified by user.



#!/bin/bash

# make sure about the correct input
if [ -z $1 ]
then
echo "Usage: ./script_name.sh path/to/directory"
else
DIR=$1
if [ $DIR = '.' ]
then
echo "Find files in the directory $PWD"
else
echo "Find files in the directory $DIR"
fi

find $DIR -type f -exec basename ;
fi


if I input



$ ./script_name.sh .


script gives me correct substitution ./ to $PWD and shows (for example)



$ Find files in the directory /root/scripts 


But I can't make a decision how to substitute ../ to the name of the directory immediately above in the hierarchy. If I input



$ ./script_name.sh .. 


script gives me the output



$ Find files in the directory .. 


Does anybody know how to substitute ../ to the actual name of the directory?










share|improve this question
















I have a script which finds files in the directory specified by user.



#!/bin/bash

# make sure about the correct input
if [ -z $1 ]
then
echo "Usage: ./script_name.sh path/to/directory"
else
DIR=$1
if [ $DIR = '.' ]
then
echo "Find files in the directory $PWD"
else
echo "Find files in the directory $DIR"
fi

find $DIR -type f -exec basename ;
fi


if I input



$ ./script_name.sh .


script gives me correct substitution ./ to $PWD and shows (for example)



$ Find files in the directory /root/scripts 


But I can't make a decision how to substitute ../ to the name of the directory immediately above in the hierarchy. If I input



$ ./script_name.sh .. 


script gives me the output



$ Find files in the directory .. 


Does anybody know how to substitute ../ to the actual name of the directory?







shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 19 at 15:30







Alex Kuchin

















asked Feb 19 at 14:30









Alex KuchinAlex Kuchin

256




256












  • Try the realpath command if it is available.

    – Bodo
    Feb 19 at 14:33











  • @Bodo realpath works good in this case.

    – Alex Kuchin
    Feb 19 at 14:59


















  • Try the realpath command if it is available.

    – Bodo
    Feb 19 at 14:33











  • @Bodo realpath works good in this case.

    – Alex Kuchin
    Feb 19 at 14:59

















Try the realpath command if it is available.

– Bodo
Feb 19 at 14:33





Try the realpath command if it is available.

– Bodo
Feb 19 at 14:33













@Bodo realpath works good in this case.

– Alex Kuchin
Feb 19 at 14:59






@Bodo realpath works good in this case.

– Alex Kuchin
Feb 19 at 14:59











3 Answers
3






active

oldest

votes


















11














GNU coreutils has the realpath command that does just that.



/tmp/a$ realpath ..
/tmp


Though note that if the path contains symlinks, it will also resolve those:



/tmp/b/c$ realpath ..
/tmp/x/y


(Here, /tmp/b was a symlink to /tmp/x/y/)



This may be different from what the shell does with cd ... E.g. cd ../.. from /tmp/b/c in Bash shows the new path as /tmp/, not as /tmp/x.






share|improve this answer

























  • readlink would be another alternative.

    – twalberg
    Feb 19 at 16:55











  • @twalberg readlink only resolves symlinks and not entries like . or .., which were the point of the question. readlink . results in an error (returns status code 1).

    – JoL
    Feb 19 at 18:43







  • 4





    @JoL Try readlink -f .

    – twalberg
    Feb 19 at 18:44


















3














A few ideas:



parent="$(dirname "$(pwd)")"

parent="$(
cd ..
pwd
)"





share|improve this answer






























    1














    You could first cd to .. and then use the $PWD.






    share|improve this answer






















      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%2f501602%2fsubstitute-and-directories-by-actual-names%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      11














      GNU coreutils has the realpath command that does just that.



      /tmp/a$ realpath ..
      /tmp


      Though note that if the path contains symlinks, it will also resolve those:



      /tmp/b/c$ realpath ..
      /tmp/x/y


      (Here, /tmp/b was a symlink to /tmp/x/y/)



      This may be different from what the shell does with cd ... E.g. cd ../.. from /tmp/b/c in Bash shows the new path as /tmp/, not as /tmp/x.






      share|improve this answer

























      • readlink would be another alternative.

        – twalberg
        Feb 19 at 16:55











      • @twalberg readlink only resolves symlinks and not entries like . or .., which were the point of the question. readlink . results in an error (returns status code 1).

        – JoL
        Feb 19 at 18:43







      • 4





        @JoL Try readlink -f .

        – twalberg
        Feb 19 at 18:44















      11














      GNU coreutils has the realpath command that does just that.



      /tmp/a$ realpath ..
      /tmp


      Though note that if the path contains symlinks, it will also resolve those:



      /tmp/b/c$ realpath ..
      /tmp/x/y


      (Here, /tmp/b was a symlink to /tmp/x/y/)



      This may be different from what the shell does with cd ... E.g. cd ../.. from /tmp/b/c in Bash shows the new path as /tmp/, not as /tmp/x.






      share|improve this answer

























      • readlink would be another alternative.

        – twalberg
        Feb 19 at 16:55











      • @twalberg readlink only resolves symlinks and not entries like . or .., which were the point of the question. readlink . results in an error (returns status code 1).

        – JoL
        Feb 19 at 18:43







      • 4





        @JoL Try readlink -f .

        – twalberg
        Feb 19 at 18:44













      11












      11








      11







      GNU coreutils has the realpath command that does just that.



      /tmp/a$ realpath ..
      /tmp


      Though note that if the path contains symlinks, it will also resolve those:



      /tmp/b/c$ realpath ..
      /tmp/x/y


      (Here, /tmp/b was a symlink to /tmp/x/y/)



      This may be different from what the shell does with cd ... E.g. cd ../.. from /tmp/b/c in Bash shows the new path as /tmp/, not as /tmp/x.






      share|improve this answer















      GNU coreutils has the realpath command that does just that.



      /tmp/a$ realpath ..
      /tmp


      Though note that if the path contains symlinks, it will also resolve those:



      /tmp/b/c$ realpath ..
      /tmp/x/y


      (Here, /tmp/b was a symlink to /tmp/x/y/)



      This may be different from what the shell does with cd ... E.g. cd ../.. from /tmp/b/c in Bash shows the new path as /tmp/, not as /tmp/x.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Feb 19 at 14:43

























      answered Feb 19 at 14:40









      ilkkachuilkkachu

      61.5k10100177




      61.5k10100177












      • readlink would be another alternative.

        – twalberg
        Feb 19 at 16:55











      • @twalberg readlink only resolves symlinks and not entries like . or .., which were the point of the question. readlink . results in an error (returns status code 1).

        – JoL
        Feb 19 at 18:43







      • 4





        @JoL Try readlink -f .

        – twalberg
        Feb 19 at 18:44

















      • readlink would be another alternative.

        – twalberg
        Feb 19 at 16:55











      • @twalberg readlink only resolves symlinks and not entries like . or .., which were the point of the question. readlink . results in an error (returns status code 1).

        – JoL
        Feb 19 at 18:43







      • 4





        @JoL Try readlink -f .

        – twalberg
        Feb 19 at 18:44
















      readlink would be another alternative.

      – twalberg
      Feb 19 at 16:55





      readlink would be another alternative.

      – twalberg
      Feb 19 at 16:55













      @twalberg readlink only resolves symlinks and not entries like . or .., which were the point of the question. readlink . results in an error (returns status code 1).

      – JoL
      Feb 19 at 18:43






      @twalberg readlink only resolves symlinks and not entries like . or .., which were the point of the question. readlink . results in an error (returns status code 1).

      – JoL
      Feb 19 at 18:43





      4




      4





      @JoL Try readlink -f .

      – twalberg
      Feb 19 at 18:44





      @JoL Try readlink -f .

      – twalberg
      Feb 19 at 18:44













      3














      A few ideas:



      parent="$(dirname "$(pwd)")"

      parent="$(
      cd ..
      pwd
      )"





      share|improve this answer



























        3














        A few ideas:



        parent="$(dirname "$(pwd)")"

        parent="$(
        cd ..
        pwd
        )"





        share|improve this answer

























          3












          3








          3







          A few ideas:



          parent="$(dirname "$(pwd)")"

          parent="$(
          cd ..
          pwd
          )"





          share|improve this answer













          A few ideas:



          parent="$(dirname "$(pwd)")"

          parent="$(
          cd ..
          pwd
          )"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 19 at 14:44









          ctrl-alt-delorctrl-alt-delor

          12k42360




          12k42360





















              1














              You could first cd to .. and then use the $PWD.






              share|improve this answer



























                1














                You could first cd to .. and then use the $PWD.






                share|improve this answer

























                  1












                  1








                  1







                  You could first cd to .. and then use the $PWD.






                  share|improve this answer













                  You could first cd to .. and then use the $PWD.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 19 at 14:42









                  TomaszTomasz

                  10.2k53068




                  10.2k53068



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501602%2fsubstitute-and-directories-by-actual-names%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

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay