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;
}
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
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 10 hours ago
ctrl-alt-delor
12.4k52662
12.4k52662
New contributor
asked 14 hours ago
J WJ W
61
61
New contributor
New contributor
add a comment |
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() { # 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.
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() { # 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.
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() { # 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.
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() { # 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.
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.
answered 11 hours ago
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