1. Overview

In this tutorial, we’ll look at how to set up and use a bin folder in our home directory and discuss situations when we would want to use it.

We’ll briefly look at the setup of bin directories in the Linux filesystem. Then, we’ll discuss when using one of these in our home directory could be useful. We’ll check if this directory already exists and how to create it if not. Then, we’ll go through the steps to add it to our path so that it’s automatically included in searches for executable files.

The commands have been tested on Ubuntu 22.04. All code snippets should work in most Linux environments.

2. Linux Directories for Programs

The Linux directory structure has a few different locations for executable files, with some differences in their purposes.

The root /bin directory, short for binaries, contains files that are essential to the operating system. It does not contain subdirectories, only commands or symbolic links to commands.

Another executable location is /sbin, which stores executables for system administration. The files in here require root privileges to run. /usr/bin stores executables that are run by non-root users. Similarly, /usr/sbin stores files that are not essential to the operating system.

The purposes of /usr/local/bin and /usr/local/sbin are for locally installed executables and system executables, respectively. These locations will not get overwritten during system upgrades.

3. Purpose of the $HOME/bin Directory

With many locations for executables already existing on the Linux fileystem, what are some situations when we would want to use our Linux home directory for executable installations?

If a program is to be executed by more than one user on the system, $HOME would not be suitable as an installation location due to the security restrictions that are usually in place on these folders.

So, one use case would be when we want to keep scripts or executable files isolated from the system-wide executables. For example, if we compile a software package from the source and we’re the only users of this software, the local binaries can appropriately be installed in our home directory. Keeping these files in the bin subdirectory keeps our filesystem structure easy to understand.

Another situation is where we do not have root privileges to install software system-wide. We can use $HOME/bin as the location to download and extract a package, then use a package manager to install it.

4. Creating a $HOME/bin Directory

$HOME/bin can also be referred to as ~/bin. It does not exist by default, so let’s use the test command to verify whether it exists:

$ test -d $HOME/bin && echo "bin exists"
$

Since our command gave us an empty reply, we need to create the directory. *Let’s start by checking that we’re in the $HOME directory with pwd. If not, we can cd to place us there.* In this example, our username is user1:

$ pwd
/home
$ cd
$ pwd
/home/user1

Then, we’ll use mkdir to create our bin directory:

$ mkdir bin

We could call bin any name and it would work, but following the naming convention for executable files keeps things simple.

5. Finding Executables in $HOME/bin

Now that we have the directory set up, we want our operating system to automatically traverse it when searching for executables. To do this, we need to ensure it’s included in the $PATH variable. Distributions that use systemd include $HOME/bin by default. This is taken care of by the /etc/skel/.profile file, which customizes new users’ home directories:

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

 We can check if $HOME/bin is included by using the echo command to view the $PATH variable:

$ echo $PATH
/home/user1/bin:/home/user1/.local/bin:/usr/bin:/bin:/usr/sbin:/sbin 

This shows that the path has been included by default for our sample user user1. If it’s not, we can use the export command to append it to our existing PATH variable:

$ export PATH=/home/user1/bin:$PATH

Here, we prepended /home/user1/bin to the existing value for the PATH variable and exported it.

6. Adding $HOME/bin to Startup Files

As an optional step, if we’re using a shell initialization file, we can add $HOME/bin to it. This could be .bashrc, .bash_profile, or .profile.

First, we’ll make a backup of our original file. This is useful in case we make any mistakes that stop our file from working:

$ cd
$ cp .bash_profile .bash_profile_original
$

Here, cd took us to our home folder, then we used cp to make a backup of our original .bash_profile. Now, we can use echo to add our new $HOME/bin directory to the end of the file:

$echo 'export PATH=/home/user1/bin:$PATH' >>.bash_profile

After editing any of the shell initialization files, we then have to source the updated file for the change to take effect:

$ source .bash_profile
$

We also have the option of editing the file using a text editor of our choice. Either method will need to be followed with the source command.

7. Conclusion

In this article, we looked at the Linux filesystem directory structure for executable files. We then considered some situations when using a directory for executables from a home directory could be useful.

Then, we learned how to check if $HOME/bin exists and how to create it if not. We then added it to our $PATH to ensure that programs installed there will run. Finally, we added it to our shell initialization file, .bash_profile.