This script uses tree
...
sudo apt-get install tree
You can change the depth of directories by changeing -L 1
and you can choose whether or not to use ~/
...
UPDATE 2:
.. modified the script to replace only a leading /home/user/ (not "any")
.. and added args..
UPDATE Added check:
.. Don't add directory to PATH when it's already in PATH
.. This check considers '~/' and '/home/user/' to be different.
Re your question 1... yes it is true; each specific directory must be spedified individually in the PATH..
Re your question 2... Here is a script which will do what you want..
I've tested it, but as it stands it will make the new PATH for the current session..
To make it permanent you can use export PATH
(but I'm a bit sketchy on the details of 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