I have shell script for monitoring local area traffic for my system. Now I want to install it, and want to run like other unix command
-
I think we'll need more details. You want to install from a script, or run a script? – philshem Nov 27 '12 at 00:58
-
I need an idea about it. So I can do it myself – ADR Nov 27 '12 at 01:06
3 Answers
The usual location to install local scripts is /usr/local/bin
or /usr/local/sbin
. See man hier
for details of the directory structure.
These directories are usually included if the corresponding /usr/bin
or /usr/sbin
directories are in the path. See man eviron
for information on the standard environment variables including PATH.
The directory hierarchy for many distributions is document in the heir man page. It can be displayed with the command man heir
/usr/local/bin
is for programs that all users should be able to run. It is equivalent to /bin
and /usr/bin
. Normally, most users will not have these on their path.
/usr/local/sbin
is for programs that are used for system administration. It is equivalent to /sbin
and /usr/sbin
.

- 4,698
-
1Much easier than editing a config file. Just copy your script-file to /usr/local/bin or /usr/local/sbin, and you can immediately execute from a command-line. – johny why Aug 25 '15 at 15:51
-
-
-
@Asqiir Sorry, but I couldn't resist: https://unix.stackexchange.com/q/85249/70524 – muru Jun 12 '18 at 05:02
The way I would solve this (with my fairly rudimentary linux skillz) is to make an alias to the shell script.
First ensure the shell script is executable.
chmod u+x,g+x script.sh
Then, edit your .bashrc
file like following:
cd
vi .bashrc
Add this towards the bottom. (I think you can also add this in a specific alias file, such as .bash_aliases
, but I don't.)
alias commandtorun='/home/user/script.sh'
Here, commandtorun
will be the command you type in to run the script, and '/home/user/script.sh'
is the path to the script.
To save changes to your .bashrc
in vi editor, :wq
, which writes to file and quits.
Edit: You'll also need to re-source your .bashrc to use the changes in the current session. (Or just restart the session / close and reopen the terminal).
source ~/.bashrc
Good luck!

- 358
- 1
- 5
- 12
-
4I would (personally) not create the alias and instead put the file in $HOME/bin. Creating an alias for every script is going to be hard to maintain if you create a lot of them. Also the chmod command would be a lot better off if it were either
chmod +x script.sh
orchmod a+x script.sh
There should be no other users in your "user" group so the o+x option is kinda pointless. If you want others to be able to execute the file you need to eitherchown user:group script.sh
and make sure that the "other users" are in that group. Or just let everyone execute it withchmod a+x script.sh
– coteyr Nov 28 '12 at 02:25 -
2Also.... For scripts to be run system wide the convention is to put them in /usr/local/bin/. Scripts that are in your $HOME may not b visible to everyone else. – coteyr Nov 28 '12 at 02:26
-
I had to log out and log in again for this to work on Ubuntu. And ultimately preferred user76204's solution. – Andriy Makukha Mar 03 '18 at 06:47
-
@AndriyMakukha You will need to re-load the .bashrc (see my edit at the end). Thanks for the suggestion. – David_G Mar 06 '18 at 07:01
On Ubuntu, it is possible to create a bin folder in your home folder and place your user scripts in there. Indeed, your ~/.profile
will contain the following:
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
After you have created your bin folder you must logout and login again for /home/$USER/bin
to appear in your path when you enter echo $PATH
. Once it is in your path you will be able to call scripts in there by name and execute them just like you can any other programs.
The bin folder does not need any special permissions, and if you just want your user to be able to execute the scripts enter chmod u+x
when you make them executable.
Your new output of echo $PATH
when your bin folder is added should be:
/home/mike/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
You can place your scripts in /usr/local/bin
, but you will need to use sudo to copy them to the folder and then use sudo again when you want to edit them there, so I find having a bin folder in the home folder is very convenient and sensible, particularly on a single user system.
However, it is important to note that if you have more than one user on your system and you want the script to be available for them all, you would definitely need to put it in /usr/local/bin
.