在Linux终端中的目录之间快速智能导航 [英] quick and smart navigation between directories in the linux terminal

查看:103
本文介绍了在Linux终端中的目录之间快速智能导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事具有相似结构和子目录的多个项目.层次结构的示例如下:

I am working on multiple projects with similar structures and sub-directories. An example of the hierarchy would be something like this:

其中X是项目名称,子目录MainLibsLxMx对于不同的项目都是相同的.插入路径之间的CD,我想创建一个别名(或简单的命令)以跳转到目录(例如:Libs/ M2),而不管项目名称和我所在的子目录(是否我在L1L8project_redproject_yellow中,我想跳到M2).

Where X is the project name and subdirectories Main, Libs, Lx and Mx are all the same for different projects. insteding of cd'ing betweeing ong paths, I would like to create an alias (or a simple command) to jump to a directory (eg:Libs/ M2) regardless of the project name and the sub-directory that I am in (whether I am in L1 or L8, or project_red or project_yellow, I want to jump to M2).

这很容易通过为单个项目创建别名来实现:

This is easy to do by creating an alias for a single project:

alias goM2='cd /projects/project_red/Libs/M1/M2'

但是我不确定如何对所有不同的项目名称执行此操作.我可以创建多个别名,但我想知道是否有一种巧妙的方法可以做到这一点.也许通过解析当前目录以提取项目名称并在最后添加所需的目的地,但是我不确定如何执行此操作.

But I am not sure how to do this for all different project names. I can create multiple aliases but I was wondering if there is a neat way to do this. Perhaps by parsing the current directory to extract the project name and add the desired destination at the end but I am not sure how to do this.

谢谢

推荐答案

在同一项目中的目录之间移动(并假定MainLibs是项目根目录的唯一直接子级),类似于此函数应该可以.

To move between directories within the same project (and assuming that Main and Libs are the only direct children of the project root) something like this function should work.

projcd() {
    projdir=$PWD # Save current directory.
    projdir=${projdir%/Main/*} # Pull off anything after "Main"
    projdir=${projdir%/Libs/*} # Pull off anything after "Libs"

    # Find the target directory by name (more than one match will fail later).
    tgt=$(find "$projdir" -name "$1")

    if [ -z "$tgt" ]; then
        echo "No directory found for $1." >&2
        exit 1
    fi

    relpath=$(sed -e 's#[^/]\+/\?#../#g' <<<"${PWD#$projdir/}")

    cd "$relpath/$tgt"
}

这篇关于在Linux终端中的目录之间快速智能导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆