How to reference a child directory, that is part of cwd's path? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionsymbolic 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?

Antler Helmet: Can it work?

What do you call a phrase that's not an idiom yet?

Center align columns in table ignoring minus signs?

What happens to sewage if there is no river near by?

Dominant seventh chord in the major scale contains diminished triad of the seventh?

Is the Standard Deduction better than Itemized when both are the same amount?

Disable hyphenation for an entire paragraph

Is there a documented rationale why the House Ways and Means chairman can demand tax info?

When is phishing education going too far?

The logistics of corpse disposal

If 'B is more likely given A', then 'A is more likely given B'

Bonus calculation: Am I making a mountain out of a molehill?

Can inflation occur in a positive-sum game currency system such as the Stack Exchange reputation system?

How discoverable are IPv6 addresses and AAAA names by potential attackers?

What is the musical term for a note that continously plays through a melody?

Output the ŋarâþ crîþ alphabet song without using (m)any letters

Letter Boxed validator

Gastric acid as a weapon

Are my PIs rude or am I just being too sensitive?

Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?

How to deal with a team lead who never gives me credit?

What is a Meta algorithm?

Can Pao de Queijo, and similar foods, be kosher for Passover?

Java 8 stream max() function argument type Comparator vs Comparable



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



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionsymbolic 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.




















  • Does this need to be done in bash? And how specific are you about the syntax? Could you call a function, say func / 3 or func ~ 1 to get the result?

    – Jeff Schaller
    Apr 12 at 22:31

















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.




















  • Does this need to be done in bash? And how specific are you about the syntax? Could you call a function, say func / 3 or func ~ 1 to get the result?

    – Jeff Schaller
    Apr 12 at 22:31













1












1








1


0






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 Apr 11 at 19:58









ctrl-alt-delor

12.5k52662




12.5k52662






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 Apr 11 at 16:45









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.












  • Does this need to be done in bash? And how specific are you about the syntax? Could you call a function, say func / 3 or func ~ 1 to get the result?

    – Jeff Schaller
    Apr 12 at 22:31

















  • Does this need to be done in bash? And how specific are you about the syntax? Could you call a function, say func / 3 or func ~ 1 to get the result?

    – Jeff Schaller
    Apr 12 at 22:31
















Does this need to be done in bash? And how specific are you about the syntax? Could you call a function, say func / 3 or func ~ 1 to get the result?

– Jeff Schaller
Apr 12 at 22:31





Does this need to be done in bash? And how specific are you about the syntax? Could you call a function, say func / 3 or func ~ 1 to get the result?

– Jeff Schaller
Apr 12 at 22:31










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() 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() 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() 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() 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() 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 Apr 11 at 19:01









        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

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            NetworkManager fails with “Could not find source connection”Trouble connecting to VPN using network-manager, while command line worksHow can I be notified about state changes to a VPN adapterBacktrack 5 R3 - Refuses to connect to VPNFeed all traffic through OpenVPN for a specific network namespace onlyRun daemon on startup in Debian once openvpn connection establishedpfsense tcp connection between openvpn and lan is brokenInternet connection problem with web browsers onlyWhy does NetworkManager explicitly support tun/tap devices?Browser issues with VPNTwo IP addresses assigned to the same network card - OpenVPN issues?Cannot connect to WiFi with nmcli, although secrets are provided