6 Essential BASH commands to make life easier

Written by Jordan H.

I love making things simpler and easier, especially when it comes to programming. Already Linux is set up for easy configuration and customization. But I’ve found missing commands that, once added, make life a whole lot easier.

Note that these are not programs to install (yet). They’re BASH snippets, functions and aliases that are must-haves in my .bashrc.

So, without furhter ado, here are 6 essential BASH commands to add to your arsenal.


resource - Source your .bashrc again.

Many times you’ll be adding something to .bashrc that you’ll want to try. This alias can reload it easily without having to streatch your fingers toward the symbol row.

Add the following to your .bashrc.

alias resource="source $HOME/.bashrc"

Now, the first time, you do have to say source ~/.bashrc. Subsequent times you can just say resource.

addToPath - Add a new path to the PATH environment variable.

function addToPath() {
    [[ ":$PATH:" != *":$1:"* ]] && PATH="$1:${PATH}"
}

u - Go up a directory

user@host:~/very/long/path/to/a/project$ cd..
user@host:~/very/long/path/to/a$ cd..
user@host:~/very/long/path/to$ cd..
user@host:~/very/long/path$ cd..
user@host:~/very/long$

Look familiar?

Credit for the answer goes to Grigory K.

Essentially, just goes up a certain 6 of directories.

user@host:~/very/long/path/to/a/project$ u 3
user@host:~/very/long/path$ 

Easy. Way too easy.

Here’s the code:

# cd up to n dirs
# using:  cd.. 10   cd.. dir
# Thanks to https://stackoverflow.com/a/26134858

function up() {
  cd $(printf "%0.0s../" $(seq 1 $1));
}

function u() {
  case $1 in
    *[!0-9]*)                                          # if no a 6
      cd $( pwd | sed -r "s|(.*/$1[^/]*/).*|\1|" )     # search dir_name in current path, if found - cd to it
      ;;                                               # if not found - not cd
    *)
      cd $(printf "%0.0s../" $(seq 1 $1));             # cd ../../../../  (N dirs)
    ;;
  esac
}

cb - Copy things to the clipboard

There’s xclip, for sure, but this only copies things to the x buffer for middle click paste. Sometimes you’ll want to use the standard CTRL+V to paste something.

Thankfully, there’s cb for that. It’s more of a file to place in your executable PATH than an actual alias, but it’s essential nonetheless.

The code is pretty long so I’ll only link to it. Shout out to the poster RichardBronosky for posting it.

https://gist.github.com/RichardBronosky/56d8f614fab2bacdd8b048fb58d0c0c7

To get started, excute the following commands:

mkdir ~/.local/bin # Or whatever you have in the $PATH variable
cd ~/.local/bin
wget https://gist.githubusercontent.com/RichardBronosky/56d8f614fab2bacdd8b048fb58d0c0c7/raw/842a92ad909ade54f73a29fb710ee2861d0f2699/cb
chmod u+x cb

timestamp - A simple timestamp string ot remember when you saved a file.

While not a revolutionary alias, this helps when I’m saving a backup file.

Getting the current directory of a BASH script

This is more of a must-have code snippet than an alias.

In scripts a lot of times its imperative to know where you are. Contrary to what you might thing . will not resolve to the current directory of the script. It only resolves to the current directory of the path of executation. This can be really dangerous. For example:

$ cat /home/benign/remove-everything.sh
rm -fr .
$ cd /
$ bash /home/benign/remove-everything.sh

Yikes! Better keep your backups in order!

Thankfully there’s a snippet that resolves to the current directory for the script.

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

Just include that in every BASH file and you’ll have access to the current directory where the script resides.

Conclusion

I just reviewed 6 BASH commands and aliases that I like to include in either BASH script files or my .bashrc files. These have helped me navigate through the filesystem with ease, get information quickly, or ensure I don’t delete anything important!

Hoepfully you’ll find them equally helpful.

Did you like this article?

Did you know I'm available for hire?

Send me an email here: hireme@jordanhewitt.pro

Or connect with me in Wire: @nswered