Bash Scripts: Upgrade your terminal game
If you spend a decent amount of your time in the terminal then you have probably come to realize that you run some commands over and over, or some specific command that you can’t remember that you need to run every once in a while but is important.
With this in mind some programs use shorter commands to ease the burden a little, such as ‘cat’ instead of ‘catenate’ or ‘ls’ instead of ‘list’, etc. Even then sometimes typing the same command over and over can be utterly draining.
To take some of the strain off typing the same thing repeatedly we can use ‘aliases’ to shortcut commands or make them more memorable. Essentially, we are taking a command adding a custom phrase then having the system run that original command. Types of Bash aliases:
Permanent: Persistent command that lasts past the current shell session
Temporary: Only lasts for the current shell session
Temporary Aliases
A temporary alias will only last for the session that it was created in, so you can create one that is specific for whatever work you are doing at that point of time.
An example would be something like:
$ alias alias_name={command_to_run}
Permanent Aliases
These aliases are persistent through each session. There is a little preparation needed for these in your bashrc file, found $HOME/.bashrc. In there place the below code if its not found in there already.
:~/.bashrc
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
This adds a reference to a file named ‘.bash_aliases’ and will call it’s contents. Create this file with touch ~/.bash_aliases In there you can enter your aliases and save it. Remember to reload your shell once you’ve edited either .bashrc and/or .bash_aliases files with source ~/.bashrc or source ~/.bash_aliases
Some examples
# alias python and pip command
alias python='python3'
alias pip="python -m pip $1"
# Move up a folder level once or twice
alias ..='cd ..'
alias ...='cd ../..'
# Move to folder on mounted drive
# Allows for sub-folders
# Example 'folder testing'
# will move into '/mnt/d/projects/testing'
folder() {
cd "/mnt/d/projects/$1"
}
# LAMP Server control
# lamp start; starts apache2 & mysql
# lamp stop; stops apache2 & mysql
lamp(){
sudo systemctl apache2 $1 && sudo systemctl mysql $1
}
# wttr.in weather command
# displays weather data from wttr.in
# change Paris to your location
# allows you to enter any location or postcode also
# example 'wttr Chicago'
wttr()
{
# change Paris to your default location
local request="wttr.in/${1-Paris}"
[ "$(tput cols)" -lt 125 ] && request+='?n'
curl -H "Accept-Language: ${LANG%_*}" --compressed "$request"
}
# apt-get update && upgrade
alias sysupdate='sudo sh -c "apt update && apt upgrade"'
# clear terminal window
alias c='clear'
alias cl="clear;ls;pwd"
# Get current external IP
alias ipe='curl ipinfo.io/ip'
# Generate random 20 character password
alias makepass="openssl rand -base64 20"
# wget with resume
alias wget='wget -c $1'
# untar a file
alias untar='tar -zxvf $1'
# Open nano and make backup of original file. Useful for config files and things you don't want to edit the original
function nanobk() {
echo "You are making a copy of $1 before you open it. Press enter to continue."
read nul
cp $1 $1.bak
nano $1
}