Personal Script Directories
Have you written shell scripts or helper programs and wondered how to organize them? Or been frustrated with always updating path or aliases? Here are two tips that helped me.
- Create a /bin or /script directory in your home directory and add it to your path. Use it for your custom scripts and other executables you want to treat as shell commands.
- Use Symlinks (symbolic link files) for scripts or binaries that won't go directly into the bin directory. For example, scripts or binaries that are part of a repository or project structure.
While these may seem obvious, it took a tip from a colleague before I organized this way. I've never looked back.
A personal “bin” directory
Bin is short for binary, though the idea of a storage bin also works!
On Unix systems there are various bin directories containing runable programs and scripts. It's no surprise the naming convention was extended to a bin directory in the user home directory. On some Unix systems the $HOME/bin directory is automatically added to your path if the directory exists.
If your system does not automatically add the bin directory to your path, you'll have to do it manually. A command such as the following placed in your shell init file will do it:
export PATH=/your_home_dir/bin:$PATH
Shell init file names vary by system and shell type. On my system it's .bashrc or .bash_profile.
After adding the directory and path, don't forget it won't take effect until you start a new shell.
Use Symbolic Links (“symlinks”)
A symbolic link, or symlink, makes a file behave like it's in two locations at once; it's actual location and the symlink location. You can read more about them here: Symbolic Links (Wikipedia).
If you have a script or program that's incovenient to place directly in your bin directory because it's part of something else, simply create a symlink to it in your bin directory.
For example, if you have a set of tools that originate in a git repository, clone the repository to an appropriate directory and symlink the tools you want to use into your bin directory.
An Example
Here is a sampling of my current /bin directory:
- wp-cli.phar file renamed to wp (the WordPress command line tool)
- composer phar file
- drush launcher phar file
- symlinks to bash scripts in a local clone of https://github.com/dale42/shell-scripts
- symlinks to 2 different versions of drush (the Drupal comand line tool).
Each version is built its own directory with composer.