TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How to navigate directories faster with Bash (2015)

329 点作者 _xrjp大约 4 年前

48 条评论

kkoncevicius大约 4 年前
Here is one additional bookmark solution [1]:<p><pre><code> export CDPATH=.:~&#x2F;.marks&#x2F; function mark { ln -sr &quot;$(pwd)&quot; ~&#x2F;.marks&#x2F;&quot;$1&quot; } mark @name # create a bookmark cd @name # jump to bookmark cd @&lt;tab&gt; # list bookmarks cd @n&lt;tab&gt; # auto-complete cd @name&#x2F;&lt;tab&gt; # can access sub-directories within bookmarks </code></pre> Works best when bookmarks are prefixed by &quot;@&quot; or other special symbol. Additional advantage is that you can use the same function `cd` to navigate both bookmarked and regular directories.<p>[1]: <a href="http:&#x2F;&#x2F;karolis.koncevicius.lt&#x2F;posts&#x2F;fast_navigation_in_the_command_line&#x2F;" rel="nofollow">http:&#x2F;&#x2F;karolis.koncevicius.lt&#x2F;posts&#x2F;fast_navigation_in_the_c...</a>
评论 #26907313 未加载
评论 #26903293 未加载
评论 #26905605 未加载
评论 #26901542 未加载
Normille大约 4 年前
I upvoted for this alone:<p><pre><code> Another neat short hand is !$ which is an alias for the last argument of the previous command. This can be handy if one creates a new directory and wants to change into it without typing the the directory again. So commands would be mkdir -p make&#x2F;new&#x2F;directory cd !$ </code></pre> You&#x27;ve no idea how many years I&#x27;ve been thinking <i>&quot;This is Linux fer gawd&#x27;s sake. Surely there must be some easy way to make a new directory and &#x27;cd&#x27; to it in a single command!&quot;</i> But was never able to find or guess the right incantation.
评论 #26901631 未加载
评论 #26899760 未加载
评论 #26900549 未加载
评论 #26900015 未加载
评论 #26901966 未加载
评论 #26899886 未加载
评论 #26904425 未加载
评论 #26899711 未加载
评论 #26902651 未加载
评论 #26901831 未加载
评论 #26901526 未加载
评论 #26900431 未加载
评论 #26902078 未加载
评论 #26899863 未加载
alpb大约 4 年前
The most important tool I found for switching directories is called &quot;z&quot;. You just partially type a previously visited directory&#x27;s name and it takes you there. It also keeps track of your history to rank candidates for popularity. <a href="https:&#x2F;&#x2F;github.com&#x2F;rupa&#x2F;z&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rupa&#x2F;z&#x2F;</a> It&#x27;s basically CDPATH on stereoids and doesn&#x27;t require preconfiguration like CDPATH.
评论 #26906077 未加载
评论 #26908171 未加载
sandreas大约 4 年前
The author missed to mention one of my favorite tricks to change back in to the last visited directory:<p><pre><code> cd - </code></pre> Hint: Change into directory called &#x27;-&#x27; can be done by<p><pre><code> cd .&#x2F;- </code></pre> I also use the mkcd alias &#x2F; function:<p><pre><code> mkcd() { mkdir -p &quot;$@&quot; &amp;&amp; cd !$; } </code></pre> This allows creating multiple dirs and cd into the first one.<p><pre><code> mkcd test1 test2 test3</code></pre>
评论 #26904859 未加载
评论 #26901779 未加载
jnpatel大约 4 年前
I use `z` as a less manual way to search common directories instead of changing $CDPATH. It&#x27;s based on &#x27;frecency&#x27;.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;rupa&#x2F;z" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rupa&#x2F;z</a>
评论 #26899913 未加载
评论 #26899948 未加载
评论 #26900240 未加载
评论 #26903150 未加载
andix大约 4 年前
Or you just use a shell like fish that already comes with a lot of useful defaults. No need to customize everything by yourself, just use what other smart people already created.<p><a href="https:&#x2F;&#x2F;fishshell.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;fishshell.com&#x2F;</a>
评论 #26903541 未加载
评论 #26907912 未加载
评论 #26904457 未加载
prussian大约 4 年前
I&#x27;m surprised autocd was not mentioned. I saw<p><pre><code> alias ..=&#x27;cd ..&#x27; # etc ... </code></pre> But not.<p><pre><code> shopt -s autocd </code></pre> zsh also has this. This seems like the fastest way to navigate since you no longer need to type, or retype, cd.
评论 #26901769 未加载
MawKKe大约 4 年前
Another one:<p><pre><code> cdof () { cd &quot;$(dirname $1)&quot; } </code></pre> as in &#x27;Change into Directory Of&quot;<p>Changes into the directory of a file. This is useful when you&#x27;re doing complicated commands with long path names, but realize it would be easier if you were to cd into that directory. Use in combination with &lt;Alt+.&gt;. Especially useful if the filename component is long:<p><pre><code> $ pwd =&gt; ~&#x2F; $ ls -l path&#x2F;to&#x2F;whatever&#x2F;deep&#x2F;somewhere&#x2F;2021-04-22-filewithverylongnamefoobarbaz.xyz $ cdof &lt;Alt+.&gt; $ pwd =&gt; path&#x2F;to&#x2F;whatever&#x2F;deep&#x2F;somewhere</code></pre>
评论 #26905061 未加载
评论 #26904277 未加载
chalst大约 4 年前
Good selection of tips, like Normille I didn&#x27;t know $! even though I have read Bash&#x27;s man page through more than once.<p>Note that many of these are not just for Bash. pushd&#x2F;popd is near universal among shells and CDPATH is standard among SYSV shells.<p>The idea of a shortcut for cd .. and its repetitions is good, but I&#x27;d prefer alias p=&#x27;cd ..&#x27;, pp=&#x27;cd ..&#x2F;..&#x27; and so on, since I balk at the idea of having to distinguish, e.g., ..... and ...., but I find pppp and ppp are easier to discriminate by eyeball.<p>The shells I use most often are bash and the Emacs eshell, and over the years I appreciate eshell more and more. It has a few footguns, but the availability of the various selection-narrowing libraries, dired and tramp are huge pluses. zsh can pretty much do anything bash can do and more, and it&#x27;s default on Macos and Arch Linux.
评论 #26899937 未加载
评论 #26899870 未加载
jamespwilliams大约 4 年前
<a href="https:&#x2F;&#x2F;github.com&#x2F;junegunn&#x2F;fzf" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;junegunn&#x2F;fzf</a> also has a “cd mode”, by default triggerable using Alt-C, which can be useful.
评论 #26900018 未加载
评论 #26902830 未加载
0x23大约 4 年前
I use jump commands:<p>export MARKPATH=$HOME&#x2F;.marks function jump { cd -P $MARKPATH&#x2F;$1 2&gt;&#x2F;dev&#x2F;null || echo &quot;No such mark: $1&quot; } function ju { cd -P $MARKPATH&#x2F;$1 2&gt;&#x2F;dev&#x2F;null || echo &quot;No such mark: $1&quot; } function mark { mkdir -p $MARKPATH; ln -s $(pwd) $MARKPATH&#x2F;$1 } function unmark { rm -i $MARKPATH&#x2F;$1 } function marks { ls -l $MARKPATH | sed &#x27;s&#x2F; &#x2F; &#x2F;g&#x27; | cut -d&#x27; &#x27; -f9- | sed &#x27;s&#x2F; -&#x2F;\t-&#x2F;g&#x27; &amp;&amp; echo }<p>I think the original source is #<a href="http:&#x2F;&#x2F;jeroenjanssens.com&#x2F;2013&#x2F;08&#x2F;16&#x2F;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><p>Another neat trick is to set you .inputrc to vi mode with<p>set editing-mode vi set keymap vi<p>and then put this in your .bashrc<p>bind -m vi-insert -x &#x27;&quot;\C-h&quot;&#x27;:&#x27;&quot;pushd . 1&gt;&#x2F;dev&#x2F;null 2&gt;&#x2F;dev&#x2F;null &amp;&amp; cd ..&#x2F; &amp;&amp; echo -e ${LILA} $PWD &amp;&amp; ls&quot;&#x27; bind -m vi-insert -x &#x27;&quot;\C-n&quot;&#x27;:&#x27;&quot;popd 1&gt;&#x2F;dev&#x2F;null 2&gt;&#x2F;dev&#x2F;null &amp;&amp; echo -e ${LILA} $PWD &amp;&amp; ls&quot;&#x27; bind -m vi-insert -x &#x27;&quot;\C-b&quot;:&quot;cd $HOME &amp;&amp; echo -e ${LILA} $PWD &amp;&amp; ls&quot;&#x27;<p>This enables you to go a directory up using ctrl-h and go back using ctrl-n.
robmccoll大约 4 年前
I have a few functions in my bashrc for fast directory navigation.<p>up &lt;#&gt; - cd .. # times<p>upto &lt;dir&gt; - find dir in the current path above you and cd to it<p>down &lt;dir&gt; - find dir in the current tree below you including inside any number of subdirectories and cd to it<p><a href="https:&#x2F;&#x2F;github.com&#x2F;robmccoll&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;bashrc#L154" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;robmccoll&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;bashrc#L15...</a>
评论 #26902175 未加载
pneto大约 4 年前
I&#x27;ve been playing around with the tool FZF recently. It&#x27;s speeding up my command line workflow quite a lot. I made a small script that allows me to open a fuzzy search for any command, so I can write the command f followed by something like vim, evince, cd, mpv... I can optionally give a starting directory and a first query but I rarely do it. I have it in my .bashrc, but should work in any posix compliant shell. It needs fzf and fd:<p><pre><code> # fuzzy search any command f(){ ROOT_DIR=&quot;.&quot; [ -n &quot;$2&quot; ] &amp;&amp; ROOT_DIR=&quot;$2&quot; FZF_OUT=`fd . -H --color=always &quot;$ROOT_DIR&quot; --exclude &quot;Software&#x2F;&quot;| fzf --query=&quot;$3&quot;` [ -n &quot;$FZF_OUT&quot; ] &amp;&amp; &quot;$1&quot; &quot;$FZF_OUT&quot; } </code></pre> in my case Software&#x2F; is a directory with lots of files I don&#x27;t need so I exclude it from my searches. I also like using ranger, it has very nice bookmarking and launcher features.
gfiorav大约 4 年前
Honestly what made the most difference for me was learning that `cd -` is &quot;go back&quot;.<p>Works for git too:<p>`git checkout -` (checkout previous branch)<p>`git merge -` (merge with previous branch; useful for pulling master before merging)
riccardomc大约 4 年前
I exhort you all to take a look at Fasd:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;clvv&#x2F;fasd" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;clvv&#x2F;fasd</a>
Oddskar大约 4 年前
No mentions of <a href="https:&#x2F;&#x2F;github.com&#x2F;jarun&#x2F;nnn" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jarun&#x2F;nnn</a> yet?<p>I&#x27;ve really taken a shine to using a real file manager with lots of nifty features without any bloat.
评论 #26902628 未加载
pflanze大约 4 年前
I do the following [1]:<p>- I define &quot;cdn&quot; to be what others call &quot;mkcd&quot;, as then if I have a command line &quot;cd foo&quot; and it tells me that foo doesn&#x27;t exist, I can just add the &#x27;n&#x27; to the previous entry. I also overload &quot;cdn&quot; so that when not given any argument, it goes into the newest subdirectory in the current directory.<p>- &quot;u&quot;, &quot;uu&quot;, &quot;uuu&quot;, &quot;uuuu&quot;, &quot;uuuuu&quot; for going &quot;up&quot; that many levels, and unlike the aliases in OP, I define them as functions and if those are given an argument, they descends into the path from there: &quot;u foo&quot; is equivalent to &quot;cd ..&#x2F;foo&quot;, &quot;uu foo&quot; to &quot;cd ..&#x2F;..&#x2F;foo&quot;.<p>- I also have a function called &quot;mvcd foo bar&quot; that moves foo to bar and then goes into bar. &quot;mvcdnewdir foo bar&quot; that does the same but will create bar. (I&#x27;m pondering unifying them to a version that always calls mkdir -p)<p>- an alias &quot;c&quot; for cd [2]. The single letter messes with the history search though (ctl-r c space or ctl-r cd space ?), so it&#x27;s not necessarily a good idea. `shopt -s autocd` that I just learned about here may be better.<p>- some functions for special locations, &quot;cs&quot; for ~&#x2F;scratch, &quot;cb&quot; for ~&#x2F;bookmarks, etc.<p>[1] see .bashrc at <a href="https:&#x2F;&#x2F;github.com&#x2F;pflanze&#x2F;chj-home" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pflanze&#x2F;chj-home</a> [2] but I never use bash&#x27;s &quot;alias&quot; since aside of being shorter it is always worse than a function (no way to extend it later to take parameters, and messes up live redefinitions)
评论 #26903369 未加载
评论 #26904092 未加载
xiekomb大约 4 年前
I vote for creating a « mkcd » alias !!
评论 #26901418 未加载
评论 #26900278 未加载
danwills大约 4 年前
I independently also have a stack of &#x27;...&#x27;, &#x27;....&#x27; etc aliases almost exactly the same as this! Extremely useful I reckon!<p>Another thing that I&#x27;ve found surprisingly invaluable is an alias (i went for &#x27;ccd&#x27;) that figures out (via python, so a bit slower on the 1st run) what part of the argument is a valid directory and then cd&#x27;s you there. It&#x27;s very handy indeed when working with file paths all the time (a film-vfx-on-Linux use-case) otherwise you end up using backspace to delete the file part constantly! In fact, about that: hitting Esc-Backspace in sequence, should delete back to the next word separator instead of one char at a time.
评论 #26901403 未加载
boxfoxdox大约 4 年前
For me once I moved to zsh and added `oh my zsh` it really became so much easier.<p>I like the autocomplete, the fact that when cd-ing a known path you can just type the first leter of each directory with slashes, showing the git branch in working dir.<p>Its the little things.
评论 #26900800 未加载
评论 #26908272 未加载
spurgu大约 4 年前
I just read the title and thought &quot;Switch to Zsh&quot;.<p>Bash is so amazingly clunky in comparison.
评论 #26901881 未加载
hk1337大约 4 年前
I haven’t tested this exact scenario but I like doing something like this when needed<p><pre><code> mkdir -p path&#x2F;to&#x2F;{one,two,three}&#x2F;done </code></pre> To create:<p>* path&#x2F;to&#x2F;one&#x2F;done<p>* path&#x2F;to&#x2F;two&#x2F;done<p>* path&#x2F;to&#x2F;three&#x2F;done
评论 #26901579 未加载
评论 #26901755 未加载
brendanfalk大约 4 年前
We tried to somewhat mimic the finder experience when implementing directory navigation at Fig (<a href="https:&#x2F;&#x2F;withfig.com" rel="nofollow">https:&#x2F;&#x2F;withfig.com</a>). Except of course, we make it all keyboard driven!<p>The methods listed in the article are almost certainly better if you know exactly which directory you need to go to (like a specific project repo, your bin etc) however often you don’t!<p>Another interesting tool is Z: <a href="https:&#x2F;&#x2F;github.com&#x2F;rupa&#x2F;z" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rupa&#x2F;z</a>
Syzygies大约 4 年前
On a Mac, Findspot supports fuzzy directory search, either as a key sequence or via the command line (my alias is &quot;c&quot;).<p><a href="https:&#x2F;&#x2F;gumroad.com&#x2F;l&#x2F;findspot" rel="nofollow">https:&#x2F;&#x2F;gumroad.com&#x2F;l&#x2F;findspot</a><p>I experiment with many forms of automation and stick with few of them. Interface complexity has a cost; there&#x27;s a reason keyboards don&#x27;t have more keys. For me, Findspot is indispensable.
aphrax大约 4 年前
FWIW I learnt a lot of my Bash tricks from: <a href="https:&#x2F;&#x2F;www.commandlinefu.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.commandlinefu.com&#x2F;</a>
nishparadox大约 4 年前
One of my favorite custom bash function is to use up() function to abstract away all the cd ..&#x2F;..&#x2F;..&#x2F; command.<p>Up 3 will put me 3 directories behind the current one.
iwalton3大约 4 年前
I have been using this &quot;magic cd&quot; alias for a while. You just type c and then the name of a folder, and it will find the most commonly visited folder with that name from your history and send you to it. It also supports tab completion.<p><a href="https:&#x2F;&#x2F;gist.github.com&#x2F;iwalton3&#x2F;3f9bfce510f959782404be25cab1c871" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;iwalton3&#x2F;3f9bfce510f959782404be25cab...</a>
yboris大约 4 年前
My preference is &quot;u&quot; for &quot;up&quot;:<p>&gt; alias u=&quot;cd ..&quot;<p>And on Dvorak, &quot;u&quot; is under the left index finger. Can&#x27;t get faster than that ;)
euske大约 4 年前
I&#x27;m using this bash script that adds a web-browser like history management (going forward with &quot;+&quot; and backward with &quot;-&quot;). It&#x27;s been used for a couple of decades now.<p><a href="https:&#x2F;&#x2F;gist.github.com&#x2F;euske&#x2F;4f06a6e060131949085c493bd28a0cee" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;euske&#x2F;4f06a6e060131949085c493bd28a0c...</a><p>edit: url
评论 #26901198 未加载
mraza007大约 4 年前
Wow I’m just blown away by this.<p>Really cool trick to navigate directories fast I’m definitely going to add this to my .zshrc<p>Honestly commandline has made so many things easier for me that I don’t even use regular apps besides my browser for browsing web anyhow using commandline has really made me productive<p>And I always look for automating or making the workflow better<p>Anyways great post thanks for sharing:)
评论 #26900552 未加载
pmontra大约 4 年前
I happily use bashmarks to jump to well known directories<p><a href="https:&#x2F;&#x2F;github.com&#x2F;huyng&#x2F;bashmarks" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;huyng&#x2F;bashmarks</a><p><pre><code> cd &#x2F;path&#x2F;to&#x2F;project1 s p1 # sets bookmark cd &#x2F;elsewhere g p1 </code></pre> It also autocompletes bookmarks.
anticodon大约 4 年前
I use zoxide: <a href="https:&#x2F;&#x2F;github.com&#x2F;ajeetdsouza&#x2F;zoxide" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ajeetdsouza&#x2F;zoxide</a><p>There&#x27;re only few directories I need to change to daily. So when I have to visit them, I need to type only `z first_two_characters_of_the_directory_name`
gpspake大约 4 年前
These sorts of things are really useful and it&#x27;s one of the reasons I think everyone should keep their dotfiles on github or similar so they can share their little tricks. Mine are just a compilation of stuff like this that I&#x27;ve picked up from other people over the years. (I even try to put attributions in there if I got them from friends)<p>My favorite one is c to change to my ~&#x2F;code directory with tab completion from anywhere. So I can type c pr&lt;tab&gt;&lt;enter&gt; to cd in to ~&#x2F;code&#x2F;project from anywhere. (Full disclosure This is zsh and I don&#x27;t have a bash equivalent for that)<p>Another thing I can&#x27;t live without and some people hate is ls -la anytime I cd in to a directory. In a gui, I get that visual feedback immediately and it&#x27;s usually the first thing I type when I land anyway so I just have a function for it <a href="https:&#x2F;&#x2F;github.com&#x2F;gpspake&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;zsh&#x2F;gpspake&#x2F;gpspake.plugin.zsh#L28" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gpspake&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;zsh&#x2F;gpspake&#x2F;...</a>
wiz21c大约 4 年前
Is it me or the elephant in the room is : you can&#x27;t augment regular &quot;cd&quot; functionality for autocomplete (for example via fzf) without resorting to super intricate bash code ? And so one has to introduce a new command to &quot;replace&quot; cd ?
Saint_Genet大约 4 年前
Those bunch of dots aliases are a bit ridiculous. What are you gonna do, sit and count dots?
评论 #26909519 未加载
评论 #26900012 未加载
评论 #26902111 未加载
motoboi大约 4 年前
Not a word about command editing.<p>Learn keyboard shortcuts to edit the current command line and you&#x27;ll save years of your life.<p>ESC-. (dot) will get the last argument of the last command. This one alone will save you months.<p>CTRL-R will search backwards. This other will save more months.
评论 #26905068 未加载
chmaynard大约 4 年前
If you frequently need to switch between several git branches, pushb and popb are useful commands.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;pwoolcoc&#x2F;pushb" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pwoolcoc&#x2F;pushb</a>
iR5ugfXGAE大约 4 年前
<a href="https:&#x2F;&#x2F;github.com&#x2F;ranger&#x2F;ranger" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ranger&#x2F;ranger</a> nobody? And as others have pointed out, fasd and fzf of course.
hackerbrother大约 4 年前
Vim is a good way to browse directories also! Run &quot;vim .&quot; to see. It&#x27;s nice because you can search folder names to jump to them (in the same way you&#x27;d search any other file).
atleta大约 4 年前
And if you want to make bash history usable, use hstr [1].<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;dvorka&#x2F;hstr" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dvorka&#x2F;hstr</a>
a3n大约 4 年前
&gt; Maybe less well-known is that ~ is an alias for one’s home directory.<p>And ~mylogin is another alias for ones home directory.<p>And more generally, ~otherdev is an alias for otherdev&#x27;s home directory.
评论 #26902132 未加载
_jsnk大约 4 年前
The cdable_vars bash option is among my favorites<p><pre><code> shopt -s cdable_vars p=&#x2F;some&#x2F;path&#x2F;you&#x2F;cd&#x2F;to&#x2F;a&#x2F;bunch cd p</code></pre>
Cicero22大约 4 年前
Figure I might as well share an alias I get some use out of.<p><pre><code> alias gd=&#x27;cd &quot;$(git rev-parse --show-toplevel)&quot;&#x27;</code></pre>
tpoacher大约 4 年前
I&#x27;ve heard so many horror stories about bugs related to CDPATH that I immediately assume it&#x27;s a bad idea to have one now.
thenoblesunfish大约 4 年前
OT a bit, but while I don‘t use it often, sometimes fzf really takes the pain out of navigation.
mlang23大约 4 年前
Anther thing I regularily use is incrementally searching backward in the history with C-r.
ape4大约 4 年前
Would it possible to alias the escape codes make by the up arrow as &#x27;cd ..&#x27;
评论 #26904979 未加载
chasedehan大约 4 年前
I know this is about bash, but for zsh, autojump is absolutely amazing.