drr*_*nik 5 shell bash directory cd-command shell-script
按“/”的出现次数对结果进行排序$ find . -name scripts -type d然后选择第一个结果的最佳方法是什么?
我想创建一个函数,该函数将cd指向项目中的公共文件夹。我希望它能够根据您的相对目录进行灵活调整。
因此,如果我有 10 个项目都具有相似的文件夹结构:
~/project-a/project/folder/structure
~/project-b/project/folder/structure
~/project-c/project/folder/structure
Run Code Online (Sandbox Code Playgroud)
我可以:
$ cd ~/project-a
$ cdd structure
Run Code Online (Sandbox Code Playgroud)
并被放入 ~/project-a/project/folder/struct
更新
我无法以任何可预测的方式对结果进行排序,例如:
$ find . -type d -name "themes"
./wp-content/plugins/contact-form-7/includes/js/jquery-ui/themes
./wp-content/plugins/jetpack/modules/infinite-scroll/themes
./wp-content/plugins/smart-youtube/themes
./wp-content/plugins/wptouch-pro/themes
./wp-content/themes
./wp-includes/js/tinymce/themes
Run Code Online (Sandbox Code Playgroud)
我希望该cdd函数能够下降到最接近的结果。在这个例子中,它是./wp-content/themes.
也许使用 bash 的递归通配符功能:
shopt -s globstar # in ~/.bashrc, or just in cdd() {}
cd **/structure/
Run Code Online (Sandbox Code Playgroud)
我认为**扩展的发生顺序或多或少与查找的顺序相同,因此存在大多数相同的问题。但它是如此简单和有用,以至于你可能会忽略这一点!
最新版本的bash拒绝cd具有多个参数,因此只有在恰好有一个匹配项时才有效。否则你会得到cd: too many arguments.
所以你需要一个 shell 函数。array=( **/"$1"/ )如果非空,也许会使用第一个元素,但如果您正在编写一个函数,您可能想要或find -depth -type d | tail -1某些东西来获取最后的结果(最浅的结果之一)。-maxdepth或者如果您的树可能很深,则逐步使用。
| 归档时间: |
|
| 查看次数: |
2118 次 |
| 最近记录: |