击:递归子目录添加到路径 [英] Bash: Recursively adding subdirectories to the path

查看:173
本文介绍了击:递归子目录添加到路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你是怎么做到的呢?我的code /在工作目录中的文件夹和子文件夹和subsubfolders,所有这一切(至少在理论上)包含我想定期运行的脚本或程序组织。

How do you do it? My code/ directory at work is organized in folders and subfolders and subsubfolders, all of which (at least in theory) contain scripts or programs I want to run on a regular basis.

它的转向我,否则风景如画的.bashrc到碍眼!

Its turning my otherwise picturesque .bashrc into an eyesore!

谢谢!

推荐答案

在你的脚本的末尾,放线:

At the end of your script, put the line:

PATH=${PATH}:$(find ~/code -type d | tr '\n' ':' | sed 's/:$//')

这会在你的〜/ code树的每个目录附加到当前路径。我不喜欢这个主意我自己,preferring有只有一对夫妇的目录举行自己的可执行文件,并明确列出了他们,但每一个自己。

This will append every directory in your ~/code tree to the current path. I don't like the idea myself, preferring to have only a couple of directories holding my own executables and explicitly listing them, but to each their own.

如果您想排除这是隐藏的所有目录,你基本上需要去掉每一个具有序列行/。(确保你不' t检查子目录下隐藏的目录以及):

If you want to exclude all directories which are hidden, you basically need to strip out every line that has the sequence "/." (to ensure that you don't check subdirectories under hidden directories as well):

PATH=${PATH}:$(find ~/code -type d | sed '/\/\\./d' | tr '\n' ':' | sed 's/:$//')

这会阻止你从得到的目录,如〜/ code / 1级/ .hidden / 3级/ (即,它停止子树作为搜索范围内只要发现他们是隐藏的)。如果你只是想保持隐藏的目录出来,但仍然允许非隐藏目录下他们使用:

This will stop you from getting directories such as ~/code/level1/.hidden/level3/ (i.e., it stops searching within sub-trees as soon as it detects they're hidden). If you only want to keep the hidden directories out, but still allow non-hidden directories under them, use:

PATH=${PATH}:$(find ~/code -type d -name '[^\.]*' | tr '\n' ':' | sed 's/:$//')

这将允许〜/ code / 1级/ .hidden2 / 3级/ 但不允许〜/ code / 1级/ .hidden2 / .hidden3 / ,因为 -name 只检查文件的基本名称,而不是全路径名。

This would allow ~/code/level1/.hidden2/level3/ but disallow ~/code/level1/.hidden2/.hidden3/ since -name only checks the base name of the file, not the full path name.

这篇关于击:递归子目录添加到路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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