[英]quick and smart navigation between directories in the linux terminal
我正在处理具有相似结构和子目录的多个项目。 层次结构的一个例子是这样的:
其中 X 是项目名称,子目录Main
、 Libs
、 Lx
和Mx
对于不同的项目都是相同的。 在 ong 路径之间插入 cd'ing,我想创建一个别名(或一个简单的命令)来跳转到一个目录(例如: Libs/ M2
),而不管我所在的项目名称和子目录(无论是我在L1
或L8
,或project_red
或project_yellow
,我想跳到M2
)。
这很容易通过为单个项目创建别名来实现:
alias goM2='cd /projects/project_red/Libs/M1/M2'
但我不确定如何为所有不同的项目名称执行此操作。 我可以创建多个别名,但我想知道是否有一种巧妙的方法可以做到这一点。 也许通过解析当前目录来提取项目名称并在最后添加所需的目标,但我不确定如何执行此操作。
谢谢
如果每个项目的子结构都相同(仅在Libs
和Main
目录下的libs和mains数量不同),则假设您的项目目录位于$HOME
目录下,并且项目目录树如下所示:
projects/ |-- project1 | |-- Libs | | |-- M1 | | |-- M2 | | `-- M3 | `-- Main | |-- L1 | |-- L2 | `-- L3 `-- project2 |-- Libs | |-- M1 | |-- M2 | `-- M3 `-- Main |-- L1 |-- L2 `-- L3
像以下_go_to_project_dir
这样的函数(在您的配置文件或您在 bash 会话期间用于加载的任何库中声明)将按需要完成工作。 当然,有些变量仅用于解释目的,并且已经根据您的文件系统层次结构进行了许多假设。
_go_to_project_dir() {
local projects_HOME="$HOME/projects" # this could be configured as needed
local current_project_dir # this is only an auxiliar variable
printf -v current_project_dir "%b" "${PWD%%/[(Libs)(Main)]*}" # printf -v will store the formated message into the variable following -v option
# '%%' will remove the longest occurrence of the following pattern '/[(Libs)(Main)]*'
# from the 'PWD' variable using pathname expansion
local current_project_name
printf -v current_project_name "%b" "${current_project_dir#${projects_HOME}/}" # '#' will remove shortest occurrence of the following pattern
# '${projects_HOME}/' from the 'current_project_dir' variable
# using pathname expansion
local dir_name="$1" # Directory where you want to go
local project_name="${2-$current_project_name}" # this will store second parameter if provided or 'current_project_name' content elsewhere
local dir_type="${dir_name:0:1}" # this will extract the first character from your directory name but you could select a different length to match your actual pattern names
case ${dir_type} in # here we select the path according to the previously extracted directory type
M )
path_to_go="${projects_HOME}/${project_name}/Libs/${dir_name}"
;;
L )
path_to_go="${projects_HOME}/${project_name}/Main/${dir_name}"
;;
esac
cd "${path_to_go}" # And it's done
}
当然,您可以根据您的目录名称模式以不同的方式提取dir_type
,但核心思想保持不变。
如果各种 L1、L2、M1、M2 都是不同的名称,您可以将目录路径集合添加到 CDPATH 环境变量中(而不是创建别名)。 该功能已被多次讨论,例如。 如何使用自动完成功能使自定义 BASH 函数 cd 到某个目录
要在同一个项目中的目录之间移动(并假设Main
和Libs
是项目根目录的唯一直接子项),这样的功能应该可以工作。
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"
}
alias subfolder name=" eval $\\"cd /从根目录到项目文件夹路径/` pwd | 将 -d'/' -fn`/子文件夹路径剪切到各自的项目\\""
1)eval 命令接受参数并构造它的命令。
2)密码| cut -d'/' -fn 用于获取当前项目名称。 子文件夹路径名和根到项目文件夹路径在这里是常量。n代表项目文件夹从根目录开始的顺序
3)每次如果您为子文件夹执行别名,它会获取当前项目名称并将其连接到根到项目文件夹路径和子文件夹路径名称和 cd 命令,然后执行。
在你的情况下命令是
例如:Libs文件夹
alias Libs=" eval $\\"cd /projects/\\` pwd | cut -d'/' -f3\\`/Libs\\""
如果基本功能不够,您可以尝试类似https://pypi.org/project/fastcd并配置快捷方式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.