如何将子目录递归添加到PATH? [英] How to recursively add subdirectories to the PATH?
问题描述
您如何做到的?我的目录code/
在工作中,被组织在文件夹,子文件夹和子文件夹中,所有这些文件夹(至少从理论上来说)都包含我要定期运行的脚本或程序.
How do you do it? My directory code/
, 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.
推荐答案
在脚本的末尾添加以下行:
At the end of your script, put the line:
PATH=${PATH}:$(find ~/code -type d | tr '\n' ':' | sed 's/:$//')
这会将〜/代码树中的每个目录追加到当前路径.我自己不喜欢这个主意,宁愿只有几个目录保存我自己的可执行文件并显式列出它们,但每个目录都属于自己.
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.
如果要排除所有隐藏目录,则基本上需要删除具有序列"/."
的每一行(以确保您也不会检查隐藏目录下的子目录):
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/level1/.hidden/level3/
之类的目录(即,一旦检测到它们被隐藏,它将停止在子树中搜索).如果您只想保留隐藏目录,但仍允许在其下放置非隐藏目录,请使用:
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/level1/.hidden2/level3/
,但不允许~/code/level1/.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.
这篇关于如何将子目录递归添加到PATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!