I have a bin folder with a bunch of sh files. For example "server-run.sh", "server-stop.sh", "server-do-something.sh". And I want to run file "server-stop.sh" from anywhere. I could add this bin folder to path but "run" and "do-something" files also will be available and I don't want to pollute my console. Also I don't want to add this file to /bin folder, because it could change. Maybe link. Can I somehow add only "server-stop.sh" to path?
The best way is to add a symbolic link to this file in the
cd /usr/local/bin ln -s server-stop.sh /path/to/your/folder/server-stop.sh
In this case, you are adding a link to the original file, so you can always change the original file and you command will always be working.
Creating links in
/usr/bin works, but unless you want the scripts to be available for all users, I would prefer to create the links in
~/bin, with the same result, but not touching the global system. Furthermore, OP has not mentioned where the scripts are stored. creating a link globally while the scripts are stored locally is bad practice, so alltogether I would suggest:
- Create a local directory
- Remove the language extension from the scripts (unless they invoke each other), make them executable (see further below).
- Create links to the scripts from
- Log out and back in.
Either with or without language extension, the script would work perfectly well. However, there are some arguments to use the script without language extension:
- For reasons of clarity, it might be preferable to name the link to the script similar to the script itself. keeping the language extension would then be less convenient when invoking the script.
- Although a bit overdone in cases of user-written scripts, according to lintian conventions, scripts (or links to scripts) in default searchpaths should not have a language extension. -
I would use the
bash built-in command
hash to remember the location of the
hash -p /path/to/folder/server-stop.sh server-stop
Just add the above line to your
You can now use
server-stop everywhere in your
hash: hash [-lr] [-p pathname] [-dt] [name ...] Remember or display program locations. Determine and remember the full pathname of each command NAME. If no arguments are given, information about remembered commands is displayed. Options: -d forget the remembered location of each NAME -l display in a format that may be reused as input -p pathname use PATHNAME is the full pathname of NAME -r forget all remembered locations -t print the remembered location of each NAME, preceding each location with the corresponding NAME if multiple NAMEs are given Arguments: NAME Each NAME is searched for in $PATH and added to the list of remembered commands.
Perhaps an alias is what you are looking for. Open your
~/.bash_aliases file and add the following to the end of the file(the file may be empty depending upon whether you have added an alias previously):
This will only add
server-stop.shas you intend to
This won't change your
Other users of your system are unaffected
You will have to change the alias once you change the path where
server-stop.shis located(I don't think that will be required very frequently)
This will work only for bash shell, you will have to see what other shells use for alias.