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
Yes, you can add any directory to the system path. One way to do this is updating the PATH (environmental variable) definition. You can do this in your .bashrc
by adding the following lines:
PATH="/your/script/dir:${PATH}"
export PATH
I like to add my scripts to $HOME/.local/bin/
(which is a hidden directory) so my home dir stays cleaner.
Your directory will not get inserted into the PATH variable right away, unless you run source .bashrc
.
You can add multiple directories to the path, remember that. Please consult BASH documentation if you do not understand the code.
The previous method will only work for your user. If you need to add a script directory for all users do as bodhi.zazen and add your scripts to /usr/local/bin
.
Best Answer
Using ~/.profile to set $PATH
A path set in
.bash_profile
will only be set in a bash login shell (bash -l
). If you put your path in.profile
it will be available to your complete desktop session. That means even metacity will use it.For example
~/.profile
:Btw, you can check the PATH variable of a process by looking at its environment in
/proc/[pid]/environ
(replace [pid] with the number fromps axf
). E.g. usegrep -z "^PATH" /proc/[pid]/environ
Note:
bash
as a login shell doesn't parse.profile
if either.bash_profile
or.bash_login
exists. Fromman bash
:See the answers below for information about
.pam_environment
, or.bashrc
for interactive non-login shells, or set the value globally for all users by putting a script into/etc/profile.d/
or use/etc/X11/Xsession.d/
to affect the display managers session.