Shorten very long dir name in bash prompt












0














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










share|improve this question
























  • 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
















0














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










share|improve this question
























  • 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














0












0








0


1





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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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










2 Answers
2






active

oldest

votes


















2














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
$ _





share|improve this answer





























    0














    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"...).






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


      }
      });














      draft saved

      draft discarded


















      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









      2














      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
      $ _





      share|improve this answer


























        2














        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
        $ _





        share|improve this answer
























          2












          2








          2






          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
          $ _





          share|improve this answer












          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
          $ _






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 27 '18 at 15:19









          glenn jackmanglenn jackman

          50.4k570107




          50.4k570107

























              0














              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"...).






              share|improve this answer


























                0














                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"...).






                share|improve this answer
























                  0












                  0








                  0






                  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"...).






                  share|improve this answer












                  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"...).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 27 '18 at 16:25









                  RudiCRudiC

                  4,2041312




                  4,2041312






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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

                      Morgemoulin

                      Scott Moir

                      Souastre