如何递归地将子目录添加到 PATH? [英] How to recursively add subdirectories to the PATH?

查看:25
本文介绍了如何递归地将子目录添加到 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 '
' ':' | sed 's/:$//')

这会将 ~/code 树中的每个目录附加到当前路径.我自己不喜欢这个想法,更喜欢只有几个目录保存我自己的可执行文件并明确列出它们,但每个目录都有自己的.

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 '
' ':' | 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 '
' ':' | 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屋!

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