Shorten very long dir name in bash prompt
How to get from this prompt
~/this/is/a-very-very-long-directory-name/dir
to this
~/this/is/a-ver...name/dir
in a bash prompt?
So shorten directory names longer than nn (20+) characters to something like xxxx...xxxx
note for possible duplicate: I want to shorten a long dir name, not a long path/to/dir
bash directory prompt
add a comment |
How to get from this prompt
~/this/is/a-very-very-long-directory-name/dir
to this
~/this/is/a-ver...name/dir
in a bash prompt?
So shorten directory names longer than nn (20+) characters to something like xxxx...xxxx
note for possible duplicate: I want to shorten a long dir name, not a long path/to/dir
bash directory prompt
Similar: unix.stackexchange.com/q/55930/117549
– Jeff Schaller
Dec 27 '18 at 15:00
5
Possible duplicate of bash prompt with abbreviated current director including dot files?
– Mr Shunz
Dec 27 '18 at 15:09
1
I want to shorten a long dir name, not a long path
– Ju Tutt
Dec 27 '18 at 15:57
Hello @JuTutt. The possible duplicate also shortens a directory name, not a path. That being said, I think your question is more general than the linked one -- and also the answer is more complex (pre and post match). That's why I'm opposing the duplicate vote.
– Sebastian
Dec 28 '18 at 21:15
add a comment |
How to get from this prompt
~/this/is/a-very-very-long-directory-name/dir
to this
~/this/is/a-ver...name/dir
in a bash prompt?
So shorten directory names longer than nn (20+) characters to something like xxxx...xxxx
note for possible duplicate: I want to shorten a long dir name, not a long path/to/dir
bash directory prompt
How to get from this prompt
~/this/is/a-very-very-long-directory-name/dir
to this
~/this/is/a-ver...name/dir
in a bash prompt?
So shorten directory names longer than nn (20+) characters to something like xxxx...xxxx
note for possible duplicate: I want to shorten a long dir name, not a long path/to/dir
bash directory prompt
bash directory prompt
edited Dec 27 '18 at 15:59
Ju Tutt
asked Dec 27 '18 at 14:56
Ju TuttJu Tutt
64
64
Similar: unix.stackexchange.com/q/55930/117549
– Jeff Schaller
Dec 27 '18 at 15:00
5
Possible duplicate of bash prompt with abbreviated current director including dot files?
– Mr Shunz
Dec 27 '18 at 15:09
1
I want to shorten a long dir name, not a long path
– Ju Tutt
Dec 27 '18 at 15:57
Hello @JuTutt. The possible duplicate also shortens a directory name, not a path. That being said, I think your question is more general than the linked one -- and also the answer is more complex (pre and post match). That's why I'm opposing the duplicate vote.
– Sebastian
Dec 28 '18 at 21:15
add a comment |
Similar: unix.stackexchange.com/q/55930/117549
– Jeff Schaller
Dec 27 '18 at 15:00
5
Possible duplicate of bash prompt with abbreviated current director including dot files?
– Mr Shunz
Dec 27 '18 at 15:09
1
I want to shorten a long dir name, not a long path
– Ju Tutt
Dec 27 '18 at 15:57
Hello @JuTutt. The possible duplicate also shortens a directory name, not a path. That being said, I think your question is more general than the linked one -- and also the answer is more complex (pre and post match). That's why I'm opposing the duplicate vote.
– Sebastian
Dec 28 '18 at 21:15
Similar: unix.stackexchange.com/q/55930/117549
– Jeff Schaller
Dec 27 '18 at 15:00
Similar: unix.stackexchange.com/q/55930/117549
– Jeff Schaller
Dec 27 '18 at 15:00
5
5
Possible duplicate of bash prompt with abbreviated current director including dot files?
– Mr Shunz
Dec 27 '18 at 15:09
Possible duplicate of bash prompt with abbreviated current director including dot files?
– Mr Shunz
Dec 27 '18 at 15:09
1
1
I want to shorten a long dir name, not a long path
– Ju Tutt
Dec 27 '18 at 15:57
I want to shorten a long dir name, not a long path
– Ju Tutt
Dec 27 '18 at 15:57
Hello @JuTutt. The possible duplicate also shortens a directory name, not a path. That being said, I think your question is more general than the linked one -- and also the answer is more complex (pre and post match). That's why I'm opposing the duplicate vote.
– Sebastian
Dec 28 '18 at 21:15
Hello @JuTutt. The possible duplicate also shortens a directory name, not a path. That being said, I think your question is more general than the linked one -- and also the answer is more complex (pre and post match). That's why I'm opposing the duplicate vote.
– Sebastian
Dec 28 '18 at 21:15
add a comment |
2 Answers
2
active
oldest
votes
You'll need to use something like sed, bash doesn't have any builtin method.
d='~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one'
# or, d=$(pwd)
e=$( echo "$d" | sed -E 's#([^/]{4})[^/]{13,}([^/.]{3})#1...2#g' )
echo "$e"
~/this/is/a-ve...ame/with...ame/and-...one
On the other hand, you may want to throw a newline into your prompt. I use something like this:
PS1='u@h:wn$ '
which would look like
jackman@myhost:~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one
$ _
add a comment |
With shell's "parameter expansion", try
d='~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one'
IFS=/
for DIR in $d
do [ ${#DIR} -gt 8 ] && { TMP=${DIR%%${DIR#????}}
DIR=$TMP...${DIR##${DIR%????}}
}
NEW="$NEW${NEW:+/}$DIR"
done
echo "$NEW"
~/this/is/a-ve...name/with...name/and-...-one
Save and restore IFS
if need be. Running in a subshell won't work as you want to access the NEW
variable afterwards (unless you use "command substitution"...).
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
});
}
});
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%2f491141%2fshorten-very-long-dir-name-in-bash-prompt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You'll need to use something like sed, bash doesn't have any builtin method.
d='~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one'
# or, d=$(pwd)
e=$( echo "$d" | sed -E 's#([^/]{4})[^/]{13,}([^/.]{3})#1...2#g' )
echo "$e"
~/this/is/a-ve...ame/with...ame/and-...one
On the other hand, you may want to throw a newline into your prompt. I use something like this:
PS1='u@h:wn$ '
which would look like
jackman@myhost:~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one
$ _
add a comment |
You'll need to use something like sed, bash doesn't have any builtin method.
d='~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one'
# or, d=$(pwd)
e=$( echo "$d" | sed -E 's#([^/]{4})[^/]{13,}([^/.]{3})#1...2#g' )
echo "$e"
~/this/is/a-ve...ame/with...ame/and-...one
On the other hand, you may want to throw a newline into your prompt. I use something like this:
PS1='u@h:wn$ '
which would look like
jackman@myhost:~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one
$ _
add a comment |
You'll need to use something like sed, bash doesn't have any builtin method.
d='~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one'
# or, d=$(pwd)
e=$( echo "$d" | sed -E 's#([^/]{4})[^/]{13,}([^/.]{3})#1...2#g' )
echo "$e"
~/this/is/a-ve...ame/with...ame/and-...one
On the other hand, you may want to throw a newline into your prompt. I use something like this:
PS1='u@h:wn$ '
which would look like
jackman@myhost:~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one
$ _
You'll need to use something like sed, bash doesn't have any builtin method.
d='~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one'
# or, d=$(pwd)
e=$( echo "$d" | sed -E 's#([^/]{4})[^/]{13,}([^/.]{3})#1...2#g' )
echo "$e"
~/this/is/a-ve...ame/with...ame/and-...one
On the other hand, you may want to throw a newline into your prompt. I use something like this:
PS1='u@h:wn$ '
which would look like
jackman@myhost:~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one
$ _
answered Dec 27 '18 at 15:19
glenn jackmanglenn jackman
50.4k570107
50.4k570107
add a comment |
add a comment |
With shell's "parameter expansion", try
d='~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one'
IFS=/
for DIR in $d
do [ ${#DIR} -gt 8 ] && { TMP=${DIR%%${DIR#????}}
DIR=$TMP...${DIR##${DIR%????}}
}
NEW="$NEW${NEW:+/}$DIR"
done
echo "$NEW"
~/this/is/a-ve...name/with...name/and-...-one
Save and restore IFS
if need be. Running in a subshell won't work as you want to access the NEW
variable afterwards (unless you use "command substitution"...).
add a comment |
With shell's "parameter expansion", try
d='~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one'
IFS=/
for DIR in $d
do [ ${#DIR} -gt 8 ] && { TMP=${DIR%%${DIR#????}}
DIR=$TMP...${DIR##${DIR%????}}
}
NEW="$NEW${NEW:+/}$DIR"
done
echo "$NEW"
~/this/is/a-ve...name/with...name/and-...-one
Save and restore IFS
if need be. Running in a subshell won't work as you want to access the NEW
variable afterwards (unless you use "command substitution"...).
add a comment |
With shell's "parameter expansion", try
d='~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one'
IFS=/
for DIR in $d
do [ ${#DIR} -gt 8 ] && { TMP=${DIR%%${DIR#????}}
DIR=$TMP...${DIR##${DIR%????}}
}
NEW="$NEW${NEW:+/}$DIR"
done
echo "$NEW"
~/this/is/a-ve...name/with...name/and-...-one
Save and restore IFS
if need be. Running in a subshell won't work as you want to access the NEW
variable afterwards (unless you use "command substitution"...).
With shell's "parameter expansion", try
d='~/this/is/a-very-very-long-directory-name/with_another_very_long_name/and-here-is-yet-another-one'
IFS=/
for DIR in $d
do [ ${#DIR} -gt 8 ] && { TMP=${DIR%%${DIR#????}}
DIR=$TMP...${DIR##${DIR%????}}
}
NEW="$NEW${NEW:+/}$DIR"
done
echo "$NEW"
~/this/is/a-ve...name/with...name/and-...-one
Save and restore IFS
if need be. Running in a subshell won't work as you want to access the NEW
variable afterwards (unless you use "command substitution"...).
answered Dec 27 '18 at 16:25
RudiCRudiC
4,2041312
4,2041312
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f491141%2fshorten-very-long-dir-name-in-bash-prompt%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
Similar: unix.stackexchange.com/q/55930/117549
– Jeff Schaller
Dec 27 '18 at 15:00
5
Possible duplicate of bash prompt with abbreviated current director including dot files?
– Mr Shunz
Dec 27 '18 at 15:09
1
I want to shorten a long dir name, not a long path
– Ju Tutt
Dec 27 '18 at 15:57
Hello @JuTutt. The possible duplicate also shortens a directory name, not a path. That being said, I think your question is more general than the linked one -- and also the answer is more complex (pre and post match). That's why I'm opposing the duplicate vote.
– Sebastian
Dec 28 '18 at 21:15