Answers:
该脚本使用tree
...
sudo apt-get install tree
您可以通过更改来更改目录的深度,-L 1
还可以选择是否使用~/
...
更新2:
..修改了脚本以仅替换开头的 / home / user /(而不是“ any”)
..并添加了参数。
更新已添加检查:..当目录已经在PATH中时,请勿将目录添加到PATH。此检查将“〜/”和“ / home / user /”视为不同。
关于您的问题1 ...是的,这是真的。每个特定目录都必须在PATH中单独添加。
回复你的问题2 ...这里是一个脚本,它会做你想要什么..
我测试过它,但因为它代表它将使新的路径当前会话..
要使其永久,您可以使用export PATH
(但我对细节有点粗略export
)
#
# name: path-add-dirs
# eg: $ path-add-dirs "$HOME/bin" "tilde" 1
# When directories are $HOME based, choose format.
# Add directories in tidle format: ~/...
# ...or as fullly-qualified: /home/user/...
# mode values: "tilde", or anything else for fully-qualified
mode="$2" # valid mode values: "tilde", or anything else
maindir="$1"; dirs= ; echo
# Buld string of subdirectories to a depth/level of 1
while IFS= read -r dir ; do
# Don't add directory if it is already in PATH
if [[ "$mode" == "tilde" ]] ; then
# replace only a *leading* :/home/user/
homecheck=":$dir"
dircheck="${homecheck/:$HOME\//:~/}"
dircheck="${dircheck#:}"
else
dircheck="${dir}"
fi;
pathcheck=":$PATH:"
if [[ "$pathcheck" != "${pathcheck/:$dircheck:/}" ]] ; then
echo "ALREADY IN PATH: $dircheck"
else
dirs="$dirs:$dir"
echo " added: $dircheck"
fi
done < <(tree --noreport -L $3 -fi -d "$maindir")
# Choose one of these two options
if [[ "$mode" == "tilde" ]]
then PATH="$PATH${dirs//:$HOME\//:~/}" # change :$HOME to :~/
else PATH="$PATH$dirs" # this method has fully expanded $HOME
fi
echo
echo "$PATH"
echo