Substitute ./ and ../ directories by actual names












3















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
    15 hours ago











  • @Bodo realpath works good in this case.

    – Alex Kuchin
    15 hours ago


















3















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
    15 hours ago











  • @Bodo realpath works good in this case.

    – Alex Kuchin
    15 hours ago
















3












3








3


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 14 hours ago







Alex Kuchin

















asked 15 hours ago









Alex KuchinAlex Kuchin

205




205













  • Try the realpath command if it is available.

    – Bodo
    15 hours ago











  • @Bodo realpath works good in this case.

    – Alex Kuchin
    15 hours ago





















  • Try the realpath command if it is available.

    – Bodo
    15 hours ago











  • @Bodo realpath works good in this case.

    – Alex Kuchin
    15 hours ago



















Try the realpath command if it is available.

– Bodo
15 hours ago





Try the realpath command if it is available.

– Bodo
15 hours ago













@Bodo realpath works good in this case.

– Alex Kuchin
15 hours ago







@Bodo realpath works good in this case.

– Alex Kuchin
15 hours ago












3 Answers
3






active

oldest

votes


















10














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
    13 hours ago











  • @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
    11 hours ago








  • 2





    @JoL Try readlink -f .

    – twalberg
    11 hours ago



















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









      10














      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
        13 hours ago











      • @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
        11 hours ago








      • 2





        @JoL Try readlink -f .

        – twalberg
        11 hours ago
















      10














      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
        13 hours ago











      • @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
        11 hours ago








      • 2





        @JoL Try readlink -f .

        – twalberg
        11 hours ago














      10












      10








      10







      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 15 hours ago

























      answered 15 hours ago









      ilkkachuilkkachu

      59.4k894168




      59.4k894168













      • readlink would be another alternative.

        – twalberg
        13 hours ago











      • @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
        11 hours ago








      • 2





        @JoL Try readlink -f .

        – twalberg
        11 hours ago



















      • readlink would be another alternative.

        – twalberg
        13 hours ago











      • @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
        11 hours ago








      • 2





        @JoL Try readlink -f .

        – twalberg
        11 hours ago

















      readlink would be another alternative.

      – twalberg
      13 hours ago





      readlink would be another alternative.

      – twalberg
      13 hours ago













      @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
      11 hours ago







      @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
      11 hours ago






      2




      2





      @JoL Try readlink -f .

      – twalberg
      11 hours ago





      @JoL Try readlink -f .

      – twalberg
      11 hours ago













      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 15 hours ago









          ctrl-alt-delorctrl-alt-delor

          11.6k42159




          11.6k42159























              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 15 hours ago









                  TomaszTomasz

                  9,59652965




                  9,59652965






























                      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 label and detect the document text images

                      Vallis Paradisi

                      Tabula Rosettana