我正在处理具有类似结构和子目录的多个项目。层次结构的一个例子是这样的:
其中X是项目名称和子目录Main
,Libs
,Lx
和Mx
对于不同的项目都是相同的。如果想要在ong路径上使用cd,我想创建一个别名(或一个简单的命令)来跳转到一个目录(例如:Libs/ M2
),无论项目名称和子目录是什么我在(L1
或L8
,或project_red
或project_yellow
,我想跳到M2
)。
通过为单个项目创建别名很容易做到:
alias goM2='cd /projects/project_red/Libs/M1/M2'
但我不知道如何为所有不同的项目名称执行此操作。我可以创建多个别名,但我想知道是否有一个简洁的方法来做到这一点。也许通过解析当前目录来提取项目名称并在最后添加所需的目标,但我不知道如何做到这一点。
由于
答案 0 :(得分:2)
如果每个项目的子结构相同(仅Libs
和Main
目录下的 libs 和 mains 的数量不同,假设您的项目目录位于$HOME
目录下,项目目录树,如下所示:
projects/ |-- project1 | |-- Libs | | |-- M1 | | |-- M2 | | `-- M3 | `-- Main | |-- L1 | |-- L2 | `-- L3 `-- project2 |-- Libs | |-- M1 | |-- M2 | `-- M3 `-- Main |-- L1 |-- L2 `-- L3
一个函数(在你的个人资料或你在bash会话中用来加载的任何库中声明),如下面的_go_to_project_dir
将根据需要完成工作。
当然,一些变量只有一个解释性目的,并且根据您的文件系统层次结构已经做了很多假设。
_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
,但核心思想保持不变。
答案 1 :(得分:1)
如果各种L1,L2,M1,M2都是不同的名称,您可以将目录路径的集合添加到CDPATH环境变量(而不是制作别名)。该特征已被多次讨论,例如,。 How to make a custom BASH function to cd into a certain directory with autocomplete
答案 2 :(得分:1)
要在同一个项目中的目录之间移动(并假设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"
}
答案 3 :(得分:0)
别名子文件夹名称=“eval $ \”cd / 从root到项目文件夹路径 /`pwd | cut -d'/' - fn` / 子文件夹路径进入 各个项目 \“”
1)eval命令接受参数并构造它的命令
2)pwd | cut -d'/' - fn用于获取当前项目名称。子文件夹路径名和根到项目文件夹路径是常量here.n表示项目文件夹从根目录的顺序
3)每次执行子文件夹的别名时,它都会获取当前项目名称并将其连接到项目文件夹路径和子文件夹路径名称和cd命令,然后执行。
在你的情况下,命令是
例如:Libs文件夹
别名Libs =“eval $ \”cd / projects / \`pwd | cut -d'/'-f3 \`/ Libs \“”
答案 4 :(得分:0)
如果基本功能不够,您可以尝试https://github.com/frazenshtein/fastcd之类的内容并配置快捷方式