How to make `cd dir/filename` take me to dir/?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite
4












I would find it very convenient to be able to use cd with a file argument.



cd myDirectory/anyname.anyExtension
would be equivalent to
cd myDirectory/



What would be the best alias or function to achieve this behavior ?



EDIT:
Sorry I didn't mention it in the 1st place: I use zsh










share|improve this question



























    up vote
    8
    down vote

    favorite
    4












    I would find it very convenient to be able to use cd with a file argument.



    cd myDirectory/anyname.anyExtension
    would be equivalent to
    cd myDirectory/



    What would be the best alias or function to achieve this behavior ?



    EDIT:
    Sorry I didn't mention it in the 1st place: I use zsh










    share|improve this question

























      up vote
      8
      down vote

      favorite
      4









      up vote
      8
      down vote

      favorite
      4






      4





      I would find it very convenient to be able to use cd with a file argument.



      cd myDirectory/anyname.anyExtension
      would be equivalent to
      cd myDirectory/



      What would be the best alias or function to achieve this behavior ?



      EDIT:
      Sorry I didn't mention it in the 1st place: I use zsh










      share|improve this question















      I would find it very convenient to be able to use cd with a file argument.



      cd myDirectory/anyname.anyExtension
      would be equivalent to
      cd myDirectory/



      What would be the best alias or function to achieve this behavior ?



      EDIT:
      Sorry I didn't mention it in the 1st place: I use zsh







      shell cd-command






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 22 '13 at 13:26

























      asked May 17 '13 at 16:39









      Sébastien

      312310




      312310




















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          In zsh, I often do:



          cd /path/to/somefile(:h)


          (h for head).



          If somefile is a symlink, you can also do:



          cd somefile(:A:h)


          To get to the directory where the target of the symlink may be found.




          The zsh equivalent of Chris' now bash-only solution would be:



          cd() [[ -d $argv[-1] ]] 



          In zsh, you can also redefine what "words" Ctrl-W removes.



          In zsh, "words" in the context of the word-based motion/transpose/delete widgets are sequences of alnums plus the characters in the $WORDCHARS variable which by default includes /.



          You could remove / from $WORDCHARS so that Ctrl-W only deletes one path component:



          WORDCHARS=$WORDCHARS//


          Another useful extension is the select-word-style widget which you can use to interactively choose between different word styles.



          autoload select-word-style
          zle -N select-word-style
          bindkey 'ew' select-word-style


          Then pressing Alt-W allows you to choose between different word styles.



          $ cd /blah/blih<Alt-W>
          Word styles (hit return for more detail):
          (b)ash (n)ormal (s)hell (w)hitespace (d)efault (q)uit
          (B), (N), (S), (W) as above with subword matching
          ?





          share|improve this answer






















          • Interesting. (I use zsh too).
            – Sébastien
            May 18 '13 at 9:17










          • Very interesting edit on the power of zsh and word styles. Thank you also for the zsh command. It does the job perfectly.
            – Sébastien
            May 22 '13 at 13:33


















          up vote
          15
          down vote













          I assume you still want to retain the original functionality if you input a directory, and you are using bash.



          cd() 
          local file="$!#"

          if (( "$#" )) && ! [[ -d "$file" ]]; then
          builtin cd "$@:1:($#-1)" "$file%/*"
          else
          builtin cd "$@"
          fi



          If you are never going to use cd's options (-P, etc), then this will also suffice:



          cd() [ -z "$1" ]; then
          builtin cd "$@"
          else
          builtin cd "$1%/*"
          fi






          share|improve this answer


















          • 4




            if you change the function name to cd make sure to add builtin in front of the cd calls
            – Ulrich Dangel
            May 17 '13 at 17:22










          • @Chris: You assumed right ;) Thanks for this ready to use function
            – Sébastien
            May 18 '13 at 9:21










          • The version that preserves cd options does not work with zsh. The simpler one works fine with zsh, BUT cd is no longer equivalent as cd ~ :/
            – Sébastien
            May 22 '13 at 13:24










          • @Sebastien Try now, I think that should fix that.
            – Chris Down
            May 22 '13 at 14:20










          • @Chris, indeed adding a check on [ -z "$1" ] was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution for zsh.
            – Sébastien
            May 23 '13 at 8:18


















          up vote
          7
          down vote













          You could use dirname to strip the filename from the path, e.g.



          mycd() cd "$(dirname "$1")"; 


          See man dirname.






          share|improve this answer
















          • 3




            Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
            – Chris Down
            May 17 '13 at 18:08







          • 1




            @ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
            – Adrian Frühwirth
            May 17 '13 at 18:10






          • 1




            @AdrianFrühwirth An exercise! Hah!
            – user13742
            May 17 '13 at 18:24

















          up vote
          3
          down vote













          If you add this to your .profile, then load it (source ~/.profile or log out and log in again), then mycd [file or directory] will take you to the right directory:



          mycd() if [ -d "$1" ]; then cd "$1"; else cd "$( dirname "$1" )"; fi ; 


          If you name it cd, then strange things will happen.






          share|improve this answer


















          • 1




            This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
            – Chris Down
            May 17 '13 at 17:38











          • @ChrisDown, though that's true of other shells, you don't need the closing semicolon in zsh.
            – Stéphane Chazelas
            May 18 '13 at 9:42










          • Also, this breaks if passing args to cd, like -P.
            – Chris Down
            May 18 '13 at 11:37

















          up vote
          0
          down vote













          cd2() 
          arg=() dir= cmd= IFS=" " msg='[-L





          share|improve this answer






















          • Given a directory called $(sudo reboot), this function may reboot the system. Also, the user is using zsh, not bash.
            – Kusalananda
            Dec 2 at 9:34











          • Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
            – ecwpz91
            Dec 3 at 23:57










          • No, cd '$(sudo reboot)' would change directory, while your function would try to evaluate the name.
            – Kusalananda
            Dec 4 at 6:17










          • Ah, I see and made changes. Let me know if that works? Good catch.
            – ecwpz91
            Dec 5 at 7:21










          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',
          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%2f76227%2fhow-to-make-cd-dir-filename-take-me-to-dir%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          In zsh, I often do:



          cd /path/to/somefile(:h)


          (h for head).



          If somefile is a symlink, you can also do:



          cd somefile(:A:h)


          To get to the directory where the target of the symlink may be found.




          The zsh equivalent of Chris' now bash-only solution would be:



          cd() [[ -d $argv[-1] ]] 



          In zsh, you can also redefine what "words" Ctrl-W removes.



          In zsh, "words" in the context of the word-based motion/transpose/delete widgets are sequences of alnums plus the characters in the $WORDCHARS variable which by default includes /.



          You could remove / from $WORDCHARS so that Ctrl-W only deletes one path component:



          WORDCHARS=$WORDCHARS//


          Another useful extension is the select-word-style widget which you can use to interactively choose between different word styles.



          autoload select-word-style
          zle -N select-word-style
          bindkey 'ew' select-word-style


          Then pressing Alt-W allows you to choose between different word styles.



          $ cd /blah/blih<Alt-W>
          Word styles (hit return for more detail):
          (b)ash (n)ormal (s)hell (w)hitespace (d)efault (q)uit
          (B), (N), (S), (W) as above with subword matching
          ?





          share|improve this answer






















          • Interesting. (I use zsh too).
            – Sébastien
            May 18 '13 at 9:17










          • Very interesting edit on the power of zsh and word styles. Thank you also for the zsh command. It does the job perfectly.
            – Sébastien
            May 22 '13 at 13:33















          up vote
          5
          down vote



          accepted










          In zsh, I often do:



          cd /path/to/somefile(:h)


          (h for head).



          If somefile is a symlink, you can also do:



          cd somefile(:A:h)


          To get to the directory where the target of the symlink may be found.




          The zsh equivalent of Chris' now bash-only solution would be:



          cd() [[ -d $argv[-1] ]] 



          In zsh, you can also redefine what "words" Ctrl-W removes.



          In zsh, "words" in the context of the word-based motion/transpose/delete widgets are sequences of alnums plus the characters in the $WORDCHARS variable which by default includes /.



          You could remove / from $WORDCHARS so that Ctrl-W only deletes one path component:



          WORDCHARS=$WORDCHARS//


          Another useful extension is the select-word-style widget which you can use to interactively choose between different word styles.



          autoload select-word-style
          zle -N select-word-style
          bindkey 'ew' select-word-style


          Then pressing Alt-W allows you to choose between different word styles.



          $ cd /blah/blih<Alt-W>
          Word styles (hit return for more detail):
          (b)ash (n)ormal (s)hell (w)hitespace (d)efault (q)uit
          (B), (N), (S), (W) as above with subword matching
          ?





          share|improve this answer






















          • Interesting. (I use zsh too).
            – Sébastien
            May 18 '13 at 9:17










          • Very interesting edit on the power of zsh and word styles. Thank you also for the zsh command. It does the job perfectly.
            – Sébastien
            May 22 '13 at 13:33













          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          In zsh, I often do:



          cd /path/to/somefile(:h)


          (h for head).



          If somefile is a symlink, you can also do:



          cd somefile(:A:h)


          To get to the directory where the target of the symlink may be found.




          The zsh equivalent of Chris' now bash-only solution would be:



          cd() [[ -d $argv[-1] ]] 



          In zsh, you can also redefine what "words" Ctrl-W removes.



          In zsh, "words" in the context of the word-based motion/transpose/delete widgets are sequences of alnums plus the characters in the $WORDCHARS variable which by default includes /.



          You could remove / from $WORDCHARS so that Ctrl-W only deletes one path component:



          WORDCHARS=$WORDCHARS//


          Another useful extension is the select-word-style widget which you can use to interactively choose between different word styles.



          autoload select-word-style
          zle -N select-word-style
          bindkey 'ew' select-word-style


          Then pressing Alt-W allows you to choose between different word styles.



          $ cd /blah/blih<Alt-W>
          Word styles (hit return for more detail):
          (b)ash (n)ormal (s)hell (w)hitespace (d)efault (q)uit
          (B), (N), (S), (W) as above with subword matching
          ?





          share|improve this answer














          In zsh, I often do:



          cd /path/to/somefile(:h)


          (h for head).



          If somefile is a symlink, you can also do:



          cd somefile(:A:h)


          To get to the directory where the target of the symlink may be found.




          The zsh equivalent of Chris' now bash-only solution would be:



          cd() [[ -d $argv[-1] ]] 



          In zsh, you can also redefine what "words" Ctrl-W removes.



          In zsh, "words" in the context of the word-based motion/transpose/delete widgets are sequences of alnums plus the characters in the $WORDCHARS variable which by default includes /.



          You could remove / from $WORDCHARS so that Ctrl-W only deletes one path component:



          WORDCHARS=$WORDCHARS//


          Another useful extension is the select-word-style widget which you can use to interactively choose between different word styles.



          autoload select-word-style
          zle -N select-word-style
          bindkey 'ew' select-word-style


          Then pressing Alt-W allows you to choose between different word styles.



          $ cd /blah/blih<Alt-W>
          Word styles (hit return for more detail):
          (b)ash (n)ormal (s)hell (w)hitespace (d)efault (q)uit
          (B), (N), (S), (W) as above with subword matching
          ?






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 18 '13 at 20:41

























          answered May 17 '13 at 18:14









          Stéphane Chazelas

          297k54561907




          297k54561907











          • Interesting. (I use zsh too).
            – Sébastien
            May 18 '13 at 9:17










          • Very interesting edit on the power of zsh and word styles. Thank you also for the zsh command. It does the job perfectly.
            – Sébastien
            May 22 '13 at 13:33

















          • Interesting. (I use zsh too).
            – Sébastien
            May 18 '13 at 9:17










          • Very interesting edit on the power of zsh and word styles. Thank you also for the zsh command. It does the job perfectly.
            – Sébastien
            May 22 '13 at 13:33
















          Interesting. (I use zsh too).
          – Sébastien
          May 18 '13 at 9:17




          Interesting. (I use zsh too).
          – Sébastien
          May 18 '13 at 9:17












          Very interesting edit on the power of zsh and word styles. Thank you also for the zsh command. It does the job perfectly.
          – Sébastien
          May 22 '13 at 13:33





          Very interesting edit on the power of zsh and word styles. Thank you also for the zsh command. It does the job perfectly.
          – Sébastien
          May 22 '13 at 13:33













          up vote
          15
          down vote













          I assume you still want to retain the original functionality if you input a directory, and you are using bash.



          cd() 
          local file="$!#"

          if (( "$#" )) && ! [[ -d "$file" ]]; then
          builtin cd "$@:1:($#-1)" "$file%/*"
          else
          builtin cd "$@"
          fi



          If you are never going to use cd's options (-P, etc), then this will also suffice:



          cd() [ -z "$1" ]; then
          builtin cd "$@"
          else
          builtin cd "$1%/*"
          fi






          share|improve this answer


















          • 4




            if you change the function name to cd make sure to add builtin in front of the cd calls
            – Ulrich Dangel
            May 17 '13 at 17:22










          • @Chris: You assumed right ;) Thanks for this ready to use function
            – Sébastien
            May 18 '13 at 9:21










          • The version that preserves cd options does not work with zsh. The simpler one works fine with zsh, BUT cd is no longer equivalent as cd ~ :/
            – Sébastien
            May 22 '13 at 13:24










          • @Sebastien Try now, I think that should fix that.
            – Chris Down
            May 22 '13 at 14:20










          • @Chris, indeed adding a check on [ -z "$1" ] was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution for zsh.
            – Sébastien
            May 23 '13 at 8:18















          up vote
          15
          down vote













          I assume you still want to retain the original functionality if you input a directory, and you are using bash.



          cd() 
          local file="$!#"

          if (( "$#" )) && ! [[ -d "$file" ]]; then
          builtin cd "$@:1:($#-1)" "$file%/*"
          else
          builtin cd "$@"
          fi



          If you are never going to use cd's options (-P, etc), then this will also suffice:



          cd() [ -z "$1" ]; then
          builtin cd "$@"
          else
          builtin cd "$1%/*"
          fi






          share|improve this answer


















          • 4




            if you change the function name to cd make sure to add builtin in front of the cd calls
            – Ulrich Dangel
            May 17 '13 at 17:22










          • @Chris: You assumed right ;) Thanks for this ready to use function
            – Sébastien
            May 18 '13 at 9:21










          • The version that preserves cd options does not work with zsh. The simpler one works fine with zsh, BUT cd is no longer equivalent as cd ~ :/
            – Sébastien
            May 22 '13 at 13:24










          • @Sebastien Try now, I think that should fix that.
            – Chris Down
            May 22 '13 at 14:20










          • @Chris, indeed adding a check on [ -z "$1" ] was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution for zsh.
            – Sébastien
            May 23 '13 at 8:18













          up vote
          15
          down vote










          up vote
          15
          down vote









          I assume you still want to retain the original functionality if you input a directory, and you are using bash.



          cd() 
          local file="$!#"

          if (( "$#" )) && ! [[ -d "$file" ]]; then
          builtin cd "$@:1:($#-1)" "$file%/*"
          else
          builtin cd "$@"
          fi



          If you are never going to use cd's options (-P, etc), then this will also suffice:



          cd() [ -z "$1" ]; then
          builtin cd "$@"
          else
          builtin cd "$1%/*"
          fi






          share|improve this answer














          I assume you still want to retain the original functionality if you input a directory, and you are using bash.



          cd() 
          local file="$!#"

          if (( "$#" )) && ! [[ -d "$file" ]]; then
          builtin cd "$@:1:($#-1)" "$file%/*"
          else
          builtin cd "$@"
          fi



          If you are never going to use cd's options (-P, etc), then this will also suffice:



          cd() [ -z "$1" ]; then
          builtin cd "$@"
          else
          builtin cd "$1%/*"
          fi







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 22 '13 at 14:19

























          answered May 17 '13 at 17:07









          Chris Down

          78.5k13187200




          78.5k13187200







          • 4




            if you change the function name to cd make sure to add builtin in front of the cd calls
            – Ulrich Dangel
            May 17 '13 at 17:22










          • @Chris: You assumed right ;) Thanks for this ready to use function
            – Sébastien
            May 18 '13 at 9:21










          • The version that preserves cd options does not work with zsh. The simpler one works fine with zsh, BUT cd is no longer equivalent as cd ~ :/
            – Sébastien
            May 22 '13 at 13:24










          • @Sebastien Try now, I think that should fix that.
            – Chris Down
            May 22 '13 at 14:20










          • @Chris, indeed adding a check on [ -z "$1" ] was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution for zsh.
            – Sébastien
            May 23 '13 at 8:18













          • 4




            if you change the function name to cd make sure to add builtin in front of the cd calls
            – Ulrich Dangel
            May 17 '13 at 17:22










          • @Chris: You assumed right ;) Thanks for this ready to use function
            – Sébastien
            May 18 '13 at 9:21










          • The version that preserves cd options does not work with zsh. The simpler one works fine with zsh, BUT cd is no longer equivalent as cd ~ :/
            – Sébastien
            May 22 '13 at 13:24










          • @Sebastien Try now, I think that should fix that.
            – Chris Down
            May 22 '13 at 14:20










          • @Chris, indeed adding a check on [ -z "$1" ] was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution for zsh.
            – Sébastien
            May 23 '13 at 8:18








          4




          4




          if you change the function name to cd make sure to add builtin in front of the cd calls
          – Ulrich Dangel
          May 17 '13 at 17:22




          if you change the function name to cd make sure to add builtin in front of the cd calls
          – Ulrich Dangel
          May 17 '13 at 17:22












          @Chris: You assumed right ;) Thanks for this ready to use function
          – Sébastien
          May 18 '13 at 9:21




          @Chris: You assumed right ;) Thanks for this ready to use function
          – Sébastien
          May 18 '13 at 9:21












          The version that preserves cd options does not work with zsh. The simpler one works fine with zsh, BUT cd is no longer equivalent as cd ~ :/
          – Sébastien
          May 22 '13 at 13:24




          The version that preserves cd options does not work with zsh. The simpler one works fine with zsh, BUT cd is no longer equivalent as cd ~ :/
          – Sébastien
          May 22 '13 at 13:24












          @Sebastien Try now, I think that should fix that.
          – Chris Down
          May 22 '13 at 14:20




          @Sebastien Try now, I think that should fix that.
          – Chris Down
          May 22 '13 at 14:20












          @Chris, indeed adding a check on [ -z "$1" ] was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution for zsh.
          – Sébastien
          May 23 '13 at 8:18





          @Chris, indeed adding a check on [ -z "$1" ] was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution for zsh.
          – Sébastien
          May 23 '13 at 8:18











          up vote
          7
          down vote













          You could use dirname to strip the filename from the path, e.g.



          mycd() cd "$(dirname "$1")"; 


          See man dirname.






          share|improve this answer
















          • 3




            Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
            – Chris Down
            May 17 '13 at 18:08







          • 1




            @ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
            – Adrian Frühwirth
            May 17 '13 at 18:10






          • 1




            @AdrianFrühwirth An exercise! Hah!
            – user13742
            May 17 '13 at 18:24














          up vote
          7
          down vote













          You could use dirname to strip the filename from the path, e.g.



          mycd() cd "$(dirname "$1")"; 


          See man dirname.






          share|improve this answer
















          • 3




            Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
            – Chris Down
            May 17 '13 at 18:08







          • 1




            @ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
            – Adrian Frühwirth
            May 17 '13 at 18:10






          • 1




            @AdrianFrühwirth An exercise! Hah!
            – user13742
            May 17 '13 at 18:24












          up vote
          7
          down vote










          up vote
          7
          down vote









          You could use dirname to strip the filename from the path, e.g.



          mycd() cd "$(dirname "$1")"; 


          See man dirname.






          share|improve this answer












          You could use dirname to strip the filename from the path, e.g.



          mycd() cd "$(dirname "$1")"; 


          See man dirname.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 17 '13 at 16:44









          Adrian Frühwirth

          1,6191014




          1,6191014







          • 3




            Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
            – Chris Down
            May 17 '13 at 18:08







          • 1




            @ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
            – Adrian Frühwirth
            May 17 '13 at 18:10






          • 1




            @AdrianFrühwirth An exercise! Hah!
            – user13742
            May 17 '13 at 18:24












          • 3




            Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
            – Chris Down
            May 17 '13 at 18:08







          • 1




            @ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
            – Adrian Frühwirth
            May 17 '13 at 18:10






          • 1




            @AdrianFrühwirth An exercise! Hah!
            – user13742
            May 17 '13 at 18:24







          3




          3




          Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
          – Chris Down
          May 17 '13 at 18:08





          Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
          – Chris Down
          May 17 '13 at 18:08





          1




          1




          @ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
          – Adrian Frühwirth
          May 17 '13 at 18:10




          @ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
          – Adrian Frühwirth
          May 17 '13 at 18:10




          1




          1




          @AdrianFrühwirth An exercise! Hah!
          – user13742
          May 17 '13 at 18:24




          @AdrianFrühwirth An exercise! Hah!
          – user13742
          May 17 '13 at 18:24










          up vote
          3
          down vote













          If you add this to your .profile, then load it (source ~/.profile or log out and log in again), then mycd [file or directory] will take you to the right directory:



          mycd() if [ -d "$1" ]; then cd "$1"; else cd "$( dirname "$1" )"; fi ; 


          If you name it cd, then strange things will happen.






          share|improve this answer


















          • 1




            This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
            – Chris Down
            May 17 '13 at 17:38











          • @ChrisDown, though that's true of other shells, you don't need the closing semicolon in zsh.
            – Stéphane Chazelas
            May 18 '13 at 9:42










          • Also, this breaks if passing args to cd, like -P.
            – Chris Down
            May 18 '13 at 11:37














          up vote
          3
          down vote













          If you add this to your .profile, then load it (source ~/.profile or log out and log in again), then mycd [file or directory] will take you to the right directory:



          mycd() if [ -d "$1" ]; then cd "$1"; else cd "$( dirname "$1" )"; fi ; 


          If you name it cd, then strange things will happen.






          share|improve this answer


















          • 1




            This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
            – Chris Down
            May 17 '13 at 17:38











          • @ChrisDown, though that's true of other shells, you don't need the closing semicolon in zsh.
            – Stéphane Chazelas
            May 18 '13 at 9:42










          • Also, this breaks if passing args to cd, like -P.
            – Chris Down
            May 18 '13 at 11:37












          up vote
          3
          down vote










          up vote
          3
          down vote









          If you add this to your .profile, then load it (source ~/.profile or log out and log in again), then mycd [file or directory] will take you to the right directory:



          mycd() if [ -d "$1" ]; then cd "$1"; else cd "$( dirname "$1" )"; fi ; 


          If you name it cd, then strange things will happen.






          share|improve this answer














          If you add this to your .profile, then load it (source ~/.profile or log out and log in again), then mycd [file or directory] will take you to the right directory:



          mycd() if [ -d "$1" ]; then cd "$1"; else cd "$( dirname "$1" )"; fi ; 


          If you name it cd, then strange things will happen.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 17 '13 at 17:38









          Chris Down

          78.5k13187200




          78.5k13187200










          answered May 17 '13 at 17:13









          jwhitlock

          42644




          42644







          • 1




            This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
            – Chris Down
            May 17 '13 at 17:38











          • @ChrisDown, though that's true of other shells, you don't need the closing semicolon in zsh.
            – Stéphane Chazelas
            May 18 '13 at 9:42










          • Also, this breaks if passing args to cd, like -P.
            – Chris Down
            May 18 '13 at 11:37












          • 1




            This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
            – Chris Down
            May 17 '13 at 17:38











          • @ChrisDown, though that's true of other shells, you don't need the closing semicolon in zsh.
            – Stéphane Chazelas
            May 18 '13 at 9:42










          • Also, this breaks if passing args to cd, like -P.
            – Chris Down
            May 18 '13 at 11:37







          1




          1




          This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
          – Chris Down
          May 17 '13 at 17:38





          This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
          – Chris Down
          May 17 '13 at 17:38













          @ChrisDown, though that's true of other shells, you don't need the closing semicolon in zsh.
          – Stéphane Chazelas
          May 18 '13 at 9:42




          @ChrisDown, though that's true of other shells, you don't need the closing semicolon in zsh.
          – Stéphane Chazelas
          May 18 '13 at 9:42












          Also, this breaks if passing args to cd, like -P.
          – Chris Down
          May 18 '13 at 11:37




          Also, this breaks if passing args to cd, like -P.
          – Chris Down
          May 18 '13 at 11:37










          up vote
          0
          down vote













          cd2() 
          arg=() dir= cmd= IFS=" " msg='[-L





          share|improve this answer






















          • Given a directory called $(sudo reboot), this function may reboot the system. Also, the user is using zsh, not bash.
            – Kusalananda
            Dec 2 at 9:34











          • Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
            – ecwpz91
            Dec 3 at 23:57










          • No, cd '$(sudo reboot)' would change directory, while your function would try to evaluate the name.
            – Kusalananda
            Dec 4 at 6:17










          • Ah, I see and made changes. Let me know if that works? Good catch.
            – ecwpz91
            Dec 5 at 7:21














          up vote
          0
          down vote













          cd2() 
          arg=() dir= cmd= IFS=" " msg='[-L





          share|improve this answer






















          • Given a directory called $(sudo reboot), this function may reboot the system. Also, the user is using zsh, not bash.
            – Kusalananda
            Dec 2 at 9:34











          • Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
            – ecwpz91
            Dec 3 at 23:57










          • No, cd '$(sudo reboot)' would change directory, while your function would try to evaluate the name.
            – Kusalananda
            Dec 4 at 6:17










          • Ah, I see and made changes. Let me know if that works? Good catch.
            – ecwpz91
            Dec 5 at 7:21












          up vote
          0
          down vote










          up vote
          0
          down vote









          cd2() 
          arg=() dir= cmd= IFS=" " msg='[-L





          share|improve this answer














          cd2() 
          arg=() dir= cmd= IFS=" " msg='[-L






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 5 at 7:19

























          answered Dec 2 at 1:27









          ecwpz91

          1012




          1012











          • Given a directory called $(sudo reboot), this function may reboot the system. Also, the user is using zsh, not bash.
            – Kusalananda
            Dec 2 at 9:34











          • Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
            – ecwpz91
            Dec 3 at 23:57










          • No, cd '$(sudo reboot)' would change directory, while your function would try to evaluate the name.
            – Kusalananda
            Dec 4 at 6:17










          • Ah, I see and made changes. Let me know if that works? Good catch.
            – ecwpz91
            Dec 5 at 7:21
















          • Given a directory called $(sudo reboot), this function may reboot the system. Also, the user is using zsh, not bash.
            – Kusalananda
            Dec 2 at 9:34











          • Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
            – ecwpz91
            Dec 3 at 23:57










          • No, cd '$(sudo reboot)' would change directory, while your function would try to evaluate the name.
            – Kusalananda
            Dec 4 at 6:17










          • Ah, I see and made changes. Let me know if that works? Good catch.
            – ecwpz91
            Dec 5 at 7:21















          Given a directory called $(sudo reboot), this function may reboot the system. Also, the user is using zsh, not bash.
          – Kusalananda
          Dec 2 at 9:34





          Given a directory called $(sudo reboot), this function may reboot the system. Also, the user is using zsh, not bash.
          – Kusalananda
          Dec 2 at 9:34













          Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
          – ecwpz91
          Dec 3 at 23:57




          Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
          – ecwpz91
          Dec 3 at 23:57












          No, cd '$(sudo reboot)' would change directory, while your function would try to evaluate the name.
          – Kusalananda
          Dec 4 at 6:17




          No, cd '$(sudo reboot)' would change directory, while your function would try to evaluate the name.
          – Kusalananda
          Dec 4 at 6:17












          Ah, I see and made changes. Let me know if that works? Good catch.
          – ecwpz91
          Dec 5 at 7:21




          Ah, I see and made changes. Let me know if that works? Good catch.
          – ecwpz91
          Dec 5 at 7:21

















          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%2f76227%2fhow-to-make-cd-dir-filename-take-me-to-dir%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

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)