Possible to add all subdirectories when adding a directory to $PATH
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
It isn't possible, because UNIX-based systems aren't designed to work like that. Typically, all executable files go in one of a small number of directories. For example, all the stuff that I write for my own purposes goes in ~/bin.
If you don't want your actual binaries and scripts to live in a bin directory, you can create a symlink in, e.g., ~/bin to the actual binary.
Having all subdirectories in $PATH isn't particularly desirable, since it just adds more directories to search, increases the risk of other executables getting in your PATH that shouldn't be there, and doesn't really give much of a benefit.
This should do the job
PATH="$PATH:$(find ~/bin -type d -printf ":%p")"
Please take into account that
- The whole directory tree will be traversed, which can take some time, so try to keep the hierarchy small.
- This won't take into account directories created after the PATH is set.
- If you invoke this multiple times, there will get a lot of duplicates in the PATH. You might want to assign the original PATH to some temporary variable (eg. OLD_PATH).