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;
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
New contributor
add a comment |
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
New contributor
Does this need to be done in bash? And how specific are you about the syntax? Could you call a function, sayfunc / 3
orfunc ~ 1
to get the result?
– Jeff Schaller♦
Apr 12 at 22:31
add a comment |
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
New contributor
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
bash shell directory
New contributor
New contributor
edited Apr 11 at 19:58
ctrl-alt-delor
12.5k52662
12.5k52662
New contributor
asked Apr 11 at 16:45
J WJ W
61
61
New contributor
New contributor
Does this need to be done in bash? And how specific are you about the syntax? Could you call a function, sayfunc / 3
orfunc ~ 1
to get the result?
– Jeff Schaller♦
Apr 12 at 22:31
add a comment |
Does this need to be done in bash? And how specific are you about the syntax? Could you call a function, sayfunc / 3
orfunc ~ 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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Apr 11 at 19:01
G-ManG-Man
13.7k93870
13.7k93870
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Does this need to be done in bash? And how specific are you about the syntax? Could you call a function, say
func / 3
orfunc ~ 1
to get the result?– Jeff Schaller♦
Apr 12 at 22:31