Unix standard directory to put custom executables or scripts?

LinuxShellUnixCommand LineDirectory Structure

Linux Problem Overview


If I have a custom shell script or programs, that I created myself or downloaded from the web, and I want to be able to execute this from the CLI, is there the standard location to put this in Linux/Unix directory structure?

/usr/bin ?
/usr/local/bin ?
/usr/lib ?
/usr/sbin ?
/bin ?
/sbin ?
/var ?

I usually put it under my ~/bin folder and put it in PATH, but it doesn't seem clean. And everytime I downloaded a new program, I have to put it in the PATH again.

Linux Solutions


Solution 1 - Linux

/usr/local/bin exists precisely for this purpose: for system-wide installation. For your own private use, ~/bin is the de facto standard.

If you want to keep each binary in its own subdirectory, you can do that, and add a symlink to a directory already in your PATH. So, for example:

curl -o $HOME/downloads/fnord http://fnord.example.com/script.exe
ln -s $HOME/downloads/fnord $HOME/bin/

This assumes $HOME/bin is in your PATH.

There are tools like stow which do this -- and much more -- behind the scenes for you.

Solution 2 - Linux

This may vary slightly depending on the Unix flavour. I'm assuming Linux here (although this could apply to OSX). According to the Filesystem Hierarchy Standard (FHS) (link obtained from the Linux Standard Base working group):

> The /usr/local hierarchy is for use by the system administrator when > installing software locally. It needs to be safe from being > overwritten when the system software is updated. It may be used for > programs and data that are shareable amongst a group of hosts, but not > found in /usr. > > Locally installed software must be placed within /usr/local rather > than /usr unless it is being installed to replace or upgrade software > in /usr.

/usr/local/bin is often on the path by default.

Note that you should only put the executable or a link to it in /usr/local/bin, the rest may have to go in /usr/local/lib or /usr/local/share.

The /opt tree might also be sensible:

> /opt is reserved for the installation of add-on application software > packages. > > A package to be installed in /opt must locate its static files in a > separate /opt/<package> or /opt/<provider> directory tree, where > <package> is a name that describes the software package and <provider> > is the provider's LANANA registered name. > > [...] > > The directories /opt/bin, /opt/doc, /opt/include, /opt/info, /opt/lib, > and /opt/man are reserved for local system administrator use. Packages > may provide "front-end" files intended to be placed in (by linking or > copying) these reserved directories by the local system administrator, > but must function normally in the absence of these reserved > directories.

(You could make your own link from /opt/your-package/bin/executable into /opt/bin, and put /opt/bin on the PATH if it's not already there.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionatedjaView Question on Stackoverflow
Solution 1 - LinuxtripleeeView Answer on Stackoverflow
Solution 2 - LinuxBrunoView Answer on Stackoverflow