Live data from Hacker News

How to navigate directories faster with Bash (2015)

mhoffman.github.io

151–160 of 179 posts

Re: How to navigate directories faster with Bash (2015)

#152
post #141
post #126

Another one: cdof () { cd "$(dirname $1)" } as in 'Change into Directory Of" Changes into the directory of a file. This is useful when you'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 . Especially useful if the filename component is long: $ pwd => ~/ $ ls -l path/to/whatever/deep/somewhere/2021-04-22-filewithverylongn…

bit longer, but if given a file, change to its directory function cd() { if [[ ${#} -eq 0 ]]; then builtin cd return fi if [[ -f ${1} ]]; then builtin cd $(dirname ${1}) else builtin cd ${1} fi }

Tried in cygwin and was a no-go. Tried some other concoctions in stack-overflow without success. Then I whiped this one and it worked. With a warning that will be promptly subdue to >/dev/null

function gothere {

    cd $(dirname -z `find -iname $1`)

}

Re: How to navigate directories faster with Bash (2015)

#153
post #116

I do the following [1]: - I define "cdn" to be what others call "mkcd", as then if I have a command line "cd foo" and it tells me that foo doesn't exist, I can just add the 'n' to the previous entry. I also overload "cdn" so that when not given any argument, it goes into the newest subdirectory in the current directory. - "u", "uu", "uuu", "uuuu", "uuuuu" for going "up" that many levels, and unlike the aliases in OP,…

cdn is a good idea. I'm going to steal it, and change it to ncd, so that all i have to do to add that 'n' is C-a to the begin of line.

Even better, just do

n!!

(!! repeats the previous command).

Re: How to navigate directories faster with Bash (2015)

#155

Here is one additional bookmark solution [1]: export CDPATH=.:~/.marks/ function mark { ln -sr "$(pwd)" ~/.marks/"$1" } mark @name # create a bookmark cd @name # jump to bookmark cd @ # list bookmarks cd @n # auto-complete cd @name/ # can access sub-directories within bookmarks Works best when bookmarks are prefixed by "@" or other special symbol. Additional advantage is that you can use the same function `cd` to nav…

Might be worth pointing out the author of the post you linked settled in the end on `cd` and `ls` in a different style of organization over bookmarks or `z` or any other tool.

Re: How to navigate directories faster with Bash (2015)

#156
post #67
post #2

I upvoted for this alone: 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/new/directory cd !$ You've no idea how many years I've been thinking "This is Linux fer gawd's sake. Surely there must be some easy way to make a n…

I have an "mkcd" function in my startup files: mkcd() { mkdir -vp "$1" && cd "$1"; }

     alias tcd='a=`date +%s`;mkdir "$a" && cd "$a"'

Re: How to navigate directories faster with Bash (2015)

#157
I've been playing around with the tool FZF recently. It'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:

  # fuzzy search any command
  f(){
    ROOT_DIR="."
    [ -n "$2" ] && ROOT_DIR="$2"
    FZF_OUT=`fd . -H --color=always "$ROOT_DIR" --exclude "Software/"| fzf --query="$3"`
    [ -n "$FZF_OUT" ] && "$1" "$FZF_OUT"
  }
in my case Software/ is a directory with lots of files I don't need so I exclude it from my searches. I also like using ranger, it has very nice bookmarking and launcher features.

Re: How to navigate directories faster with Bash (2015)

#158
post #100

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. https://fishshell.com/

I used bash for maybe 7 years before trying fish. I was productive in bash, and never really had any complaints. But fish made life so much easier once I got used to it and learned how to customize it to my liking.

It get critiqued a lot, but I'd suggest others try it out before dismissing it and see if it works for them.

Re: How to navigate directories faster with Bash (2015)

#159

The author missed to mention one of my favorite tricks to change back in to the last visited directory: cd - Hint: Change into directory called '-' can be done by cd ./- I also use the mkcd alias / function: mkcd() { mkdir -p "$@" && cd !$; } This allows creating multiple dirs and cd into the first one. mkcd test1 test2 test3

I don't think this is documented anywhere that I've found, but git checkout - checks out the previous branch you were on, similar to cd -

Big fan of git-aliasing this to `git back`. I actually use it a ton and that small amount of extra chars plus the "-" makes a pretty decent difference for me.

Re: How to navigate directories faster with Bash (2015)

#160
post #137

The most important tool I found for switching directories is called "z". You just partially type a previously visited directory's name and it takes you there. It also keeps track of your history to rank candidates for popularity. https://github.com/rupa/z/ It's basically CDPATH on stereoids and doesn't require preconfiguration like CDPATH.

Similar is https://lib.rs/crates/zoxide if you're a fan of rust based solutions :) .
Post reply on HN