TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Quickly go back to a directory instead of typing cd ../../..

85 pointsby vigneshwaranalmost 12 years ago
Description: Quickly go back to a specific parent directory in bash instead of typing &quot;cd ..&#x2F;..&#x2F;..&quot; redundantly.<p>How to use:<p>If you are in this path &#x2F;home&#x2F;user&#x2F;project&#x2F;src&#x2F;org&#x2F;main&#x2F;site&#x2F;utils&#x2F;file&#x2F;reader&#x2F;whatever and you want to go to site directory quickly, then just type: &#x27;bd site&#x27;<p>In fact, You can simply type &#x27;bd &lt;starting few letters&gt;&#x27; like &#x27;bd s&#x27; or &#x27;bd si&#x27;<p>If there are more than one directories with same name up in the hierarchy, bd will take you to the closest. (Not considering the immediate parent.)<p>Using bd within backticks (`bd &lt;letter(s)&gt;`) prints out the path without changing the current directory.<p>You can take advantage of that by combining `bd &lt;letter(s)&gt;` with other commands such as ls, ln, echo, zip, tar etc..<p>Check out the screenshot.

25 comments

philfreoalmost 12 years ago
Two other simple tips without requiring a script:<p>&quot;cd -&quot; will jump back to the last directory you were at, regardless of where it is.<p>Add this to your .bashrc &#x2F; .zshrc &#x2F; whatever:<p>alias cdg=&#x27;cd $(git rev-parse --show-cdup)&#x27;<p>And then type &quot;cdg&quot; to keep going up directories until you reach whichever folder contains your git repo. Useful when going from &quot;&#x2F;Users&#x2F;philfreo&#x2F;Projects&#x2F;myproject&#x2F;myproject&#x2F;src&#x2F;assets&#x2F;js&#x2F;views&#x2F;&quot; back up to &quot;&#x2F;Users&#x2F;philfreo&#x2F;Projects&#x2F;myproject&#x2F;&quot;
评论 #6311587 未加载
评论 #6311133 未加载
评论 #6311597 未加载
评论 #6311798 未加载
tytsoalmost 12 years ago
Why not put something like this in your .bashrc or your .profile file instead?<p>function bd () {<p><pre><code> OLDPWD=`pwd` NEWPWD=`echo $OLDPWD | sed &#x27;s|\(.*&#x2F;&#x27;$1&#x27;[^&#x2F;]*&#x2F;\).*|\1|&#x27;` index=`echo $NEWPWD | awk &#x27;{ print index($1,&quot;&#x2F;&#x27;$1&#x27;&quot;); }&#x27;` if [ $index -eq 0 ] ; then echo &quot;No such occurrence.&quot; else echo $NEWPWD cd &quot;$NEWPWD&quot; fi</code></pre> }<p>That way you don&#x27;t have to execute a separate shell script and source its output. Also note that the &quot;export OLDPWD&quot; wasn&#x27;t necessary in your script, either. Why pollute the environment of future processes spawned by your shell?
评论 #6310149 未加载
lognalmost 12 years ago
This article covers more advanced functionality than what&#x27;s built in to shells but don&#x27;t forget:<p><pre><code> cd - </code></pre> That goes to previous dir. A utility that follows this convention but adds some more features would be nice. For instance, maybe typing &#x27;cd -2&#x27; could take me back two spots in history, like a web browser.
评论 #6310365 未加载
评论 #6310340 未加载
cygnialmost 12 years ago
I have these two aliases in my .zshrc file:<p><pre><code> alias ..=&quot;cd ..&quot; alias ...=&quot;cd ..&#x2F;..&quot; </code></pre> For moving around to other directories I use autojump (<a href="https://github.com/joelthelion/autojump" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;joelthelion&#x2F;autojump</a>).
评论 #6311116 未加载
B-Conalmost 12 years ago
pushd and popd can be used maintain an easily rewindable history of places you have been. Use pushd to add the cwd to the stack and cd to another directory. Use popd to cd to the top dir on the stack and remove it.<p><pre><code> $ pwd &#x2F;dir1 $ pushd &#x2F;some&#x2F;other&#x2F;dir &#x2F;some&#x2F;other&#x2F;dir &#x2F;dir1 $ pwd &#x2F;some&#x2F;other&#x2F;dir $ popd &#x2F;dir1 $ pwd &#x2F;dir1</code></pre>
nkuttleralmost 12 years ago
<p><pre><code> wget -O &#x2F;usr&#x2F;bin&#x2F;bd https:[snip] chmod +rx &#x2F;usr&#x2F;bin&#x2F;bd </code></pre> Really? Run wget as root to get a random script, and put it into every user&#x27;s PATH and a directory that should only be used by the system? :-|
评论 #6311930 未加载
ck2almost 12 years ago
You know when you are getting old by when you have to start using <i>history</i> a lot to try to remember what the heck you did.
评论 #6310547 未加载
评论 #6310686 未加载
taericalmost 12 years ago
This has to be one of the first alternatives to a pushd&#x2F;popd workflow that actually sounds compelling. Kudos.
评论 #6310078 未加载
Skunkletonalmost 12 years ago
I have tried stuff like this in the past, but I find it creates too much of a dependence on special configuration. My keymapping and vimrc are already enough. Instead, I usually just do a `cd ..` followed by up&#x2F;enter&#x2F;up&#x2F;enter, which is very easy to type quickly.
tobralmost 12 years ago
An interesting aside: I can&#x27;t seem to right click that link in Safari (6.0.5). It seems to be the &quot;..&#x2F;..&#x2F;..&quot; in the tag content that does it, because it works if I edit it out in the inspector. Does anyone else run in to this?
mtdewcmualmost 12 years ago
I implemented a fairly sophisticated system for navigating directories based on pushd and popd. I also set up a system based on CDPATH to hop quickly to directories I go to frequently. I fell out of the habit of using them once my usage patterns changed, though, and, since I gave &quot;cd -&quot; a new meaning, sometimes it confuses me. It&#x27;s hard to come up with an interface that&#x27;s intuitive enough to be future-proof.
VaucGiapsalmost 12 years ago
alias ..=&#x27;cd ..&#x27;<p>alias ..1=&#x27;cd ..&#x27;<p>alias ..2=&quot;cd ..&#x2F;..&quot;<p>alias ..3=&quot;cd ..&#x2F;..&#x2F;..&quot;<p>alias ..4=&quot;cd ..&#x2F;..&#x2F;..&#x2F;..&quot;<p>alias ..5=&quot;cd ..&#x2F;..&#x2F;..&#x2F;..&#x2F;..&quot;
评论 #6310831 未加载
jvehentalmost 12 years ago
Simple bashrc function<p><pre><code> # go up X directories up_dir() { num_of_dirs=$1 for i in $(seq 1 $num_of_dirs) do cd .. done } alias up=up_dir </code></pre> Usage:<p><pre><code> $ pwd &#x2F;home&#x2F;user&#x2F;svn&#x2F;automation&#x2F;puppet&#x2F;trunk&#x2F;modules&#x2F;ossec&#x2F;templates $ up 4 $ pwd &#x2F;home&#x2F;user&#x2F;svn&#x2F;automation&#x2F;puppet</code></pre>
评论 #6311595 未加载
typicalbenderalmost 12 years ago
I&#x27;ve also found this extremely useful. It lets you mark directories with a keyword and jump directly to them regardless of where you are. <a href="http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html" rel="nofollow">http:&#x2F;&#x2F;jeroenjanssens.com&#x2F;2013&#x2F;08&#x2F;16&#x2F;quickly-navigate-your-f...</a>
imurrayalmost 12 years ago
Here&#x27;s my, yet another, alternative to the many nice shell hacks out there: a bash&#x2F;zsh &quot;up&quot; command that does &quot;cd ..&quot;, or &quot;up &lt;n&gt;&quot; does &quot;cd ..&quot; <i>n</i> times.<p><pre><code> function up () { if test $# = 1 ; then s=$( printf &quot;%$1s&quot; ); s=${s&#x2F;&#x2F; &#x2F;..\&#x2F;}; cd $s ; else cd .. ; fi; }</code></pre>
DiabloD3almost 12 years ago
So why don&#x27;t you just use jump? A lot of people already use it and you&#x27;re not limited to just directories parenting yours.
评论 #6310052 未加载
jmulderalmost 12 years ago
Very useful! Thank you ver much! Although I&#x27;d like to add one suggestion and that is to surpress an &#x27;echo&#x27; on successful navigation. I&#x27;d see my path twice right underneath of eachother, which just clutters up the screen.
wicknicksalmost 12 years ago
Smart idea. Good job! Could you extend bd to jump to bookmarked directories. I often jump between &#x2F;data... and ~&#x2F;dir&#x2F;containing&#x2F;code&#x2F; -- would be useful to have one command help with both scenarios.
评论 #6313572 未加载
helpermethodalmost 12 years ago
Shameless plug: This works similar to a script called &#x27;up&#x27; which I am the author of: <a href="https://github.com/helpermethod/up" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;helpermethod&#x2F;up</a>
MetaCosmalmost 12 years ago
For those using ZSH: <a href="http://stackoverflow.com/questions/3986760/cd-1-2-3-etc-in-z-shell" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3986760&#x2F;cd-1-2-3-etc-in-z...</a>
Tarraschalmost 12 years ago
For zsh users, I created a similar command with auto-completion! No root access needed to install!<p><a href="https://github.com/Tarrasch/zsh-bd" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Tarrasch&#x2F;zsh-bd</a>
paulkrokaalmost 12 years ago
How about using<p>bind -x &#x27;&quot;\C-h&quot;:&quot;cd ..&#x2F;;ls&quot;&#x27;<p>(putting it into .bashrc) and then using control-h as a command on the shell to move a directory upwards. very useful for me.
评论 #6311240 未加载
ballardalmost 12 years ago
zsh can be set to automagically track directories (like using `pushd` for you when using `cd`), so all that&#x27;s needed is `popd`. No scripting required.
D9ualmost 12 years ago
In FreeBSD I use the following to change to an arbitrary directory when I&#x27;m unsure of the correct path:<p>`whereis -sq &lt;dirname&gt;`<p>No extra software necessary.
jostmeyalmost 12 years ago
Why not &quot;cd -&quot; ?
评论 #6310838 未加载