Skip to content

Instantly share code, notes, and snippets.

@rBurgett
Created February 4, 2022 18:47
Show Gist options
  • Save rBurgett/0d845006ba395b1006852efa12cbdf13 to your computer and use it in GitHub Desktop.
Save rBurgett/0d845006ba395b1006852efa12cbdf13 to your computer and use it in GitHub Desktop.
Running Regular Scripts Easily in Ubuntu

Ubuntu makes it very easy to record scripts into files that can be run anywhere from the command line, allowing you to script out common tasks and easily run them without always having to copy/paste or remember long, very technical commands.

Ubuntu will automatically look to see if there is a ~/.local/bin folder and will allow any script file in there to be run as a command.

For example, you might enter sudo mount /dev/nvme0n1p1 /media/8tb to mount an external drive. You can easily script that to run as a command.

First, check inside the .local folder:

ls ~/.local

If you do not see bin there, then you need to create it:

mkdir ~/.local/bin

Since you are only editing files in your home folder, you do not need to use sudo.

Then, create a file in that bin folder with the name of whatever you want the command to be. e.g.

touch ~/.local/bin/mount-drives

Next, edit the script file (~/.local/bin/mount-drives for our example) and add the command(s) that you want to run when the command is run. The first line just specifies that it is a bash file. Then, you can add any script lines that you need under it. e.g.

#!/bin/bash
sudo mount /dev/nvme0n1p1 /media/8tb

Finally, you need to make sure that the file is executable. e.g.

chmod +x ~/.local/bin/mount-drives

If you just created the ~/.local/bin folder, then you will need to make sure that it is loaded into the PATH (Ubuntu does this automatically for ~/.local/bin, but only if the folder exists) by reloading the .profile file.

source ~/.profile

Now, you can just run mount-drives from anywhere in your terminal any time you need to mount the drives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment