How to reference a child directory, that is part of cwd's path? The 2019 Stack Overflow...

Mathematics of imaging the black hole

Is bread bad for ducks?

What was the last CPU that did not have the x87 floating-point unit built in?

Why are there uneven bright areas in this photo of black hole?

How can I add encounters in the Lost Mine of Phandelver campaign without giving PCs too much XP?

How to charge AirPods to keep battery healthy?

Finding the area between two curves with Integrate

How to type this arrow in math mode?

Output the Arecibo Message

Is an up-to-date browser secure on an out-of-date OS?

How to type a long/em dash `—`

Is Cinnamon a desktop environment or a window manager? (Or both?)

If my opponent casts Ultimate Price on my Phantasmal Bear, can I save it by casting Snap or Curfew?

Cooking pasta in a water boiler

Can withdrawing asylum be illegal?

Why does the nucleus not repel itself?

What do these terms in Caesar's Gallic Wars mean?

What is the most efficient way to store a numeric range?

What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?

How to notate time signature switching consistently every measure

Match Roman Numerals

What is the motivation for a law requiring 2 parties to consent for recording a conversation

What do I do when my TA workload is more than expected?

Why doesn't shell automatically fix "useless use of cat"?



How to reference a child directory, that is part of cwd's path?



The 2019 Stack Overflow Developer Survey Results Are Insymbolic link to a directory and relative pathSay I have a file's path, how do I reference that file's directory from the command line?Quicker Way to Reference a Directory?Simplified navigation in terminalHow does one create a directory that can't be seen and can only be accessed via its absolute path name?How can I find the first missing directory in a long path?How do I remove all files from wtihin a certain directory except for a child directory of that directory?* at end of directory pathScript to check existence of and compare directories file countHow do I create a symbol to represent a path to easily cd into a directory?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















Is there a generic way to reference a path that is nested an arbitrary level deep along the cwd? This is almost like a reverse relative path lookup.



For example:



$ pwd
/Users/somebody/foo/bar/baz

$ echo /[3] <-- 3rd directory from / in current path
/Users/somebody/foo

$ echo ~/[1] <-- 1 directory from ~ in current path
~/foo









share|improve this question









New contributor




J W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



























    1















    Is there a generic way to reference a path that is nested an arbitrary level deep along the cwd? This is almost like a reverse relative path lookup.



    For example:



    $ pwd
    /Users/somebody/foo/bar/baz

    $ echo /[3] <-- 3rd directory from / in current path
    /Users/somebody/foo

    $ echo ~/[1] <-- 1 directory from ~ in current path
    ~/foo









    share|improve this question









    New contributor




    J W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      1












      1








      1


      1






      Is there a generic way to reference a path that is nested an arbitrary level deep along the cwd? This is almost like a reverse relative path lookup.



      For example:



      $ pwd
      /Users/somebody/foo/bar/baz

      $ echo /[3] <-- 3rd directory from / in current path
      /Users/somebody/foo

      $ echo ~/[1] <-- 1 directory from ~ in current path
      ~/foo









      share|improve this question









      New contributor




      J W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      Is there a generic way to reference a path that is nested an arbitrary level deep along the cwd? This is almost like a reverse relative path lookup.



      For example:



      $ pwd
      /Users/somebody/foo/bar/baz

      $ echo /[3] <-- 3rd directory from / in current path
      /Users/somebody/foo

      $ echo ~/[1] <-- 1 directory from ~ in current path
      ~/foo






      bash shell directory






      share|improve this question









      New contributor




      J W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      J W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 10 hours ago









      ctrl-alt-delor

      12.4k52662




      12.4k52662






      New contributor




      J W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 14 hours ago









      J WJ W

      61




      61




      New contributor




      J W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      J W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      J W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Put these functions into your .bashrc:



          parent() {
          local count
          local arg

          count=$(($1+1))
          if [ "$2" = "" ]
          then
          arg="$PWD"
          else
          arg="$2"
          if [[ $arg != /* ]]
          then
          printf 'Warning: "%s" does not begin with "/".n' "$arg"
          fi
          fi
          cut -d/ -f1-"$count" <<< "$arg"
          }

          tparent() { # The ‘t’ stands for “tilde”.
          local count
          local arg
          local home

          count=$(($1+1))
          if [ "$2" = "" ]
          then
          arg="$PWD"
          else
          arg="$2"
          fi
          if [ "$3" = "" ]
          then
          home="$HOME"
          else
          home="$3"
          fi
          if [[ $home != /* ]]
          then
          printf 'Warning: "%s" does not begin with "/".n' "$home"
          fi
          if [[ $arg/ != $home/* ]]
          then
          printf 'Error: "%s" does not begin with "%s".n' "$arg" "$home"
          return 1
          fi
          printf '%s' "~${arg/$home}" | cut -d/ -f1-"$count"
          }


          It uses cut -d/ -f1-number
          to extract the first N components of a pathname. 
          number has to be N+1
          because the null string before the first / counts as the first field.



          Usage:



          $ pwd
          /home/gman/stack/JW

          $ parent 3
          /home/gman/stack

          $ tparent 1
          ~/stack


          This can handle paths with spaces and tabs, but not newlines.






          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
            });


            }
            });






            J W is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511954%2fhow-to-reference-a-child-directory-that-is-part-of-cwds-path%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Put these functions into your .bashrc:



            parent() {
            local count
            local arg

            count=$(($1+1))
            if [ "$2" = "" ]
            then
            arg="$PWD"
            else
            arg="$2"
            if [[ $arg != /* ]]
            then
            printf 'Warning: "%s" does not begin with "/".n' "$arg"
            fi
            fi
            cut -d/ -f1-"$count" <<< "$arg"
            }

            tparent() { # The ‘t’ stands for “tilde”.
            local count
            local arg
            local home

            count=$(($1+1))
            if [ "$2" = "" ]
            then
            arg="$PWD"
            else
            arg="$2"
            fi
            if [ "$3" = "" ]
            then
            home="$HOME"
            else
            home="$3"
            fi
            if [[ $home != /* ]]
            then
            printf 'Warning: "%s" does not begin with "/".n' "$home"
            fi
            if [[ $arg/ != $home/* ]]
            then
            printf 'Error: "%s" does not begin with "%s".n' "$arg" "$home"
            return 1
            fi
            printf '%s' "~${arg/$home}" | cut -d/ -f1-"$count"
            }


            It uses cut -d/ -f1-number
            to extract the first N components of a pathname. 
            number has to be N+1
            because the null string before the first / counts as the first field.



            Usage:



            $ pwd
            /home/gman/stack/JW

            $ parent 3
            /home/gman/stack

            $ tparent 1
            ~/stack


            This can handle paths with spaces and tabs, but not newlines.






            share|improve this answer




























              0














              Put these functions into your .bashrc:



              parent() {
              local count
              local arg

              count=$(($1+1))
              if [ "$2" = "" ]
              then
              arg="$PWD"
              else
              arg="$2"
              if [[ $arg != /* ]]
              then
              printf 'Warning: "%s" does not begin with "/".n' "$arg"
              fi
              fi
              cut -d/ -f1-"$count" <<< "$arg"
              }

              tparent() { # The ‘t’ stands for “tilde”.
              local count
              local arg
              local home

              count=$(($1+1))
              if [ "$2" = "" ]
              then
              arg="$PWD"
              else
              arg="$2"
              fi
              if [ "$3" = "" ]
              then
              home="$HOME"
              else
              home="$3"
              fi
              if [[ $home != /* ]]
              then
              printf 'Warning: "%s" does not begin with "/".n' "$home"
              fi
              if [[ $arg/ != $home/* ]]
              then
              printf 'Error: "%s" does not begin with "%s".n' "$arg" "$home"
              return 1
              fi
              printf '%s' "~${arg/$home}" | cut -d/ -f1-"$count"
              }


              It uses cut -d/ -f1-number
              to extract the first N components of a pathname. 
              number has to be N+1
              because the null string before the first / counts as the first field.



              Usage:



              $ pwd
              /home/gman/stack/JW

              $ parent 3
              /home/gman/stack

              $ tparent 1
              ~/stack


              This can handle paths with spaces and tabs, but not newlines.






              share|improve this answer


























                0












                0








                0







                Put these functions into your .bashrc:



                parent() {
                local count
                local arg

                count=$(($1+1))
                if [ "$2" = "" ]
                then
                arg="$PWD"
                else
                arg="$2"
                if [[ $arg != /* ]]
                then
                printf 'Warning: "%s" does not begin with "/".n' "$arg"
                fi
                fi
                cut -d/ -f1-"$count" <<< "$arg"
                }

                tparent() { # The ‘t’ stands for “tilde”.
                local count
                local arg
                local home

                count=$(($1+1))
                if [ "$2" = "" ]
                then
                arg="$PWD"
                else
                arg="$2"
                fi
                if [ "$3" = "" ]
                then
                home="$HOME"
                else
                home="$3"
                fi
                if [[ $home != /* ]]
                then
                printf 'Warning: "%s" does not begin with "/".n' "$home"
                fi
                if [[ $arg/ != $home/* ]]
                then
                printf 'Error: "%s" does not begin with "%s".n' "$arg" "$home"
                return 1
                fi
                printf '%s' "~${arg/$home}" | cut -d/ -f1-"$count"
                }


                It uses cut -d/ -f1-number
                to extract the first N components of a pathname. 
                number has to be N+1
                because the null string before the first / counts as the first field.



                Usage:



                $ pwd
                /home/gman/stack/JW

                $ parent 3
                /home/gman/stack

                $ tparent 1
                ~/stack


                This can handle paths with spaces and tabs, but not newlines.






                share|improve this answer













                Put these functions into your .bashrc:



                parent() {
                local count
                local arg

                count=$(($1+1))
                if [ "$2" = "" ]
                then
                arg="$PWD"
                else
                arg="$2"
                if [[ $arg != /* ]]
                then
                printf 'Warning: "%s" does not begin with "/".n' "$arg"
                fi
                fi
                cut -d/ -f1-"$count" <<< "$arg"
                }

                tparent() { # The ‘t’ stands for “tilde”.
                local count
                local arg
                local home

                count=$(($1+1))
                if [ "$2" = "" ]
                then
                arg="$PWD"
                else
                arg="$2"
                fi
                if [ "$3" = "" ]
                then
                home="$HOME"
                else
                home="$3"
                fi
                if [[ $home != /* ]]
                then
                printf 'Warning: "%s" does not begin with "/".n' "$home"
                fi
                if [[ $arg/ != $home/* ]]
                then
                printf 'Error: "%s" does not begin with "%s".n' "$arg" "$home"
                return 1
                fi
                printf '%s' "~${arg/$home}" | cut -d/ -f1-"$count"
                }


                It uses cut -d/ -f1-number
                to extract the first N components of a pathname. 
                number has to be N+1
                because the null string before the first / counts as the first field.



                Usage:



                $ pwd
                /home/gman/stack/JW

                $ parent 3
                /home/gman/stack

                $ tparent 1
                ~/stack


                This can handle paths with spaces and tabs, but not newlines.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 11 hours ago









                G-ManG-Man

                13.7k93870




                13.7k93870






















                    J W is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    J W is a new contributor. Be nice, and check out our Code of Conduct.













                    J W is a new contributor. Be nice, and check out our Code of Conduct.












                    J W is a new contributor. Be nice, and check out our Code of Conduct.
















                    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%2f511954%2fhow-to-reference-a-child-directory-that-is-part-of-cwds-path%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

                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                    Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...