Hacker News new | ask | show | jobs
by MawKKe 1891 days ago
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 <Alt+.>. Especially useful if the filename component is long:

  $ pwd
  => ~/
  $ ls -l path/to/whatever/deep/somewhere/2021-04-22-filewithverylongnamefoobarbaz.xyz
  $ cdof <Alt+.>
  $ pwd
  => path/to/whatever/deep/somewhere
2 comments

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`)

}
oooh; stealing this nice.