Where should I put my bash scripts

I have a few very simple bash scripts that I cobbled together for things that I do regularly. One of them is to run duplicity to do my backup tasks. Nothing clever just a bunch of if .. then statements really. As this needs to be run as sudo would it be best practice to put my script in /usr/bin ( or other location in PATH ) , chown to root.root and chmod to 700 ?


Solution 1:

If no other users other than you uses these scripts:

Then you can keep them in /home/$USER/bin. Create the bin folder if it is not there and move the files there. The bin folder in your home will automatically get added to the PATH environment variable. The code is in the .profile:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

See How to add /home/username/bin to $PATH?

Or in some systems it may be in .bashrc:

export PATH=${HOME}/bin/:${HOME}/.local/bin:${PATH}

Thanks Elder Geek

If these script are to be used by other users:

Then either /usr/local/bin or /opt/bin are good options. See Is there a standard place for placing custom Linux scripts?

Hope this helps

Solution 2:

I save my own scripts in /opt/scripts.

If your script should executeable by every system user, you can create a symbolic link to /usr/bin.

If only root should execute the script, you can create a symbolic link to /usr/sbin.

Command to add a symbolic link in /usr/bin/:

ln -s /opt/scripts/<script> /usr/bin/

You can execute the script, because /usr/bin/ is in your PATH by default.

Solution 3:

I have a directory that I use for the quick collection of my local tools or things that I deploy on various computers in /usr/local/apollo. There are branches off this directory for flags, bin and logs.

For the applications that I download and install outside of the default apt-get repositories are placed in /opt/ and a directory by the app's name, with one more sub-directory for the specific version of the application. This way my compiled version of an application like vlc or eclipse won't conflict with the distributed version.

My use of /opt is the way it's basically officially designed.

By the way the directories /usr/local/bin, /usr/local/apollo, and /opt survives a fresh OS version installation overwrite.