How to add shell script to my linux system [duplicate]
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
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
.
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!
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
.