paint-brush
Bash Aliases to Make Your Life Easierby@andrewchenke
142 reads

Bash Aliases to Make Your Life Easier

by Andrew HenkeJuly 5th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

System Administrator & Cybersecurity Consultant andrewhenke.com publishes a public list of the top aliases he uses on all servers he operates on. Here is the list of aliases that I use daily, and implement on. all servers that I operate on. The list is available for anyone else to use as well as anyone else who wants to. use the same bash aliases every time I access a new server, and it’s not always easy to reconfigure the same. bash aliases.
featured image - Bash Aliases to Make Your Life Easier
Andrew Henke HackerNoon profile picture

I spend a lot of time working on the bash command line, and I’ve come to highly value the availability of my custom set of shortcuts, preferences, and tricks, right at my fingertips. You know, all the stuff that lives in .bashrc, .profile, .vimrc, .git, etc. I operate and develop across an array of servers, from my own to my clients, and it’s not always easy to reconfigure the same bash aliases every time I access a new server. As such, I decided to create a public list of the top aliases I use, and am making them available for anyone else to use as well.

Setup

There are a lot of various aliases and an infinite amount of variations possible. Because of this, we’re going to place our bash aliases in a separate file, that will exclusively contain aliases, called .bash_aliases. However, we need to be sure that the .bashrc file will load the file containing our aliases. To do this, simply open your .bashrc file in your preferred editor, and verify that the following code is in your file:


if [ -e $HOME/.bash_aliases ]; then
   source $HOME/.bash_aliases
fi

Make sure that you have included the .bash_aliases file in your .bashrc file.

Now that you have confirmed that you can load your .bash_aliases file, let’s add the aliases to it! Here is the list of aliases that I use daily, and implement on all servers that I operate on:


## To make sure that your .bash_aliases file is loaded by bash, make sure that you have the following code in your ~/.bashrc file:
# if [ -e $HOME/.bash_aliases ]; then
#    source $HOME/.bash_aliases
# fi
#
# If you don't have the above code snippet in your .bashrc file, add it to the file, save it, and then run the command "source ~/.bashrc"

#All the standard automatic color formatting of the typical commands - these are in your .bashrc file by default. 
alias ls='ls --color=auto'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'


#JSON parser and pretty-printing tool for the command line 
#Usage (Output formatted JSON to command line):  jsp [unformatted.json]
#Usage (Output formatted JSON to a file): jsp [unformatted.json] > output.json
#IMPORTANT: You must have Python 3 and json.tool installed BEFORE this alias will work
alias jsp='python3 -m json.tool < '
#List all files in current working directory in a list format
alias ll='ls -alF'
#List all files in current working directory including hidden files and folders
alias la='ls -A'
#Classify all files in current working directory into columns
alias l='ls -CF'
# Ignore errors and warnings, and force the removal of a given file or directory
alias rm='rm -if'
#Copy file or folder from A to B, with interactive prompt 
alias cp='cp -i'
#Move file or folder from A to B, with interactive prompt
alias mv='mv -i'
#List all files in current directory including hidden files and folders, in a list, with human-readable file sizes. 
alias lsa='ls -alh'
#List all files in currenct directory except for implied . and .. paths
alias lss='ls -A1'
#Show system drive storage information in human-formatted numbers
alias df='df -h'
#Check the systemctl daemon for failed units - format for human interpretation 
alias sysstat='systemctl list-units --state=failed --no-pager'
#Returns the current system time in UTC - this is useful for quickly viewing the time in UTC when debugging log information, which is 
#often in the UTC format of your system's current time
alias udate='env TZ=UTC date'
#List disk usage for a file or folder in human format [defaults to the current working directory unless file/folder path specified]
alias dff='du -sh'
#Count how many files are in a given directory or filepath - NOTE: count ignores directories but NOT the files inside of them. 
alias count='find . -type f | wc -l'
#Find the top level of a Git project, no matter where in said project you are currently working, 
#and then to change directory to it, change to the master branch, and perform a Git pull
alias stargit='cd `git rev-parse --show-toplevel` && git checkout master && git pull'
#Return to the root folder of your current GitHub repository
alias gtop='cd `git rev-parse --show-toplevel`'
#Shows the output of the mount command, but pretty prints the response and shows human-formatted storage sizes
alias mount='mount |column -t'

# Parenting changing perms on / #
alias chown='chown --preserve-root'
alias chmod='chmod --preserve-root'
alias chgrp='chgrp --preserve-root'


# Get system memory, cpu usage, and gpu memory info quickly
## pass options to free ##
alias meminfo='free -m -l -t'
 
## get top process eating memory
alias psmem='ps auxf | sort -nr -k 4'
alias psmem10='ps auxf | sort -nr -k 4 | head -10'
 
## get top process eating cpu ##
alias pscpu='ps auxf | sort -nr -k 3'
alias pscpu10='ps auxf | sort -nr -k 3 | head -10'
 
## Get server cpu info ##
alias cpuinfo='lscpu'
 
## older system use /proc/cpuinfo ##
##alias cpuinfo='less /proc/cpuinfo' ##
 
## get GPU ram on desktop / laptop##
alias gpumeminfo='grep -i --color memory /var/log/Xorg.0.log'


Simply copy and paste the entire file into your .bash_aliases file, save, and exit, and you’re nearly ready to go! The final step is to refresh your bash environment by running source ~/.bashrc.


Congratulations on installing new bash aliases for yourself, and enjoy experimenting with the shortcuts!