Intoduction
I have been using Linux (Ubuntu) well over a decade and I fell in love with command line interface which gives you more control and power over the system.Any one who loves linux would probably agree with me. The more you use command line, you will tend to notice that you use some commands more often on day to day basis that others. So for such commands bash provides a way to create custom shortcuts and this is a time saver. I cant imagine myself without my bash aliases.
Here are some of the handy aliases that I use in my Ubuntu environment.
The complete bash aliases and the help document script can be found on my gist page
Setup
Setup of bash aliases is so simple in Ubuntu operating system. All that is required is to create a .bash_aliases file in the home directory of the user.
touch $HOME/.bash_aliases
Declaration of alias is also simple. You can just open the newly created bash_aliases file in your favourite editor and add your aliases. The sytax looks like the following
alias alias_name="command_to_run"
General
Simple directory traversal alias that would save you heck lot of time
alias ..='cd ..'
alias ...='cd ../../'
We can also customized ls command aliases to make it more useful
#lists contents of current directory with file permisions
alias ll='ls -l -sort'
#list all directories in current directories
alias ldir='ls -l | grep ^d'
# List directory and pipe output to less. Makes viewing of large directory easy
alias lsl="ls -lhFA | less"
Open the working directory in GUI file explorer using Nautilus
#Opens current directory in a file explorer
alias explore='nautilus .'
#Opens current directory in a file explorer with super user privileges
alias suexplore='sudo nautilus .'
Open current directory in Ubuntu's Disk Usage Analyzer GUI with super user privileges in the background
alias analyze='gksudo baobab . &'
Opens a GUI text editor in the background
#Opens a GUI text editor in the background. Can obviously be replaced with your favorite editor
alias text='gedit &'
#Same as above with super user privileges
alias sutext='gksudo gedit &'
Open a file with whatever program would open by double clicking on it in a GUI file explorer. Requires gnome-open to be installed
alias try='gnome-open'
Find files in current directory
alias fhere="find . -name "
Search process in process table
alias psg="ps aux | grep -v grep | grep -i -e VSZ -e"
Aliasing for Alias
We could also create some aliases to diplay the list of configured aliases, edit aliases and load them without having to logout of the system.
Here is the alias to list all aliases. This comes in handy if you ever forget your aliases
alias a='echo "------------Your aliases------------";alias'
Edit your aliases using your favourite editor
alias via='gksudo gedit ~/.bash_aliases &'
Load your aliases after adding new one in the file
alias sa='source ~/.bash_aliases;echo "Bash aliases sourced."'
IP address
If you are like me into networking and web development. You would most probably wanted to check your public IP address or if using VPN you would like to check the location of your IP address from VPN. The following aliases will make it easier for you to check those information
# Get your public ip address
alias ip='curl icanhazip.com'
# Get the location of your public IP address
alias iploc="curl -s http://whatismycountry.com/ | sed -n 's|.*> *\(.*\)</h3>|\1|p'"
# Get complete information of your public IP address
alias ipinfo='curl ipinfo.io'
Restart network service
In ubuntu I have a annoying problem of network disruption due to a old network card and requires restarting network-manager to reconnect to the internet. So here is a alias that I created to restart the network service.
#Restart network manager
alias netstart='sudo /usr/sbin/service network-manager restart'
To make sure there is no password prompt when running the alias, you could add the following entry to the sudoers list
#No password for network-manager service.(Replace your_username with your system username
your_username ALL=NOPASSWD: /usr/sbin/service network-manager restart
tldr;
Here is the complete list of some useful aliases that you can simple use by creating .bash_aliases file in the home directory.
If you are looking to find the most used commands. You could search your history for most commonly used commands. The following one liner would be useful.
history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10
Reference
- Introduction to Useful Bash Aliases - DigitalOcean.