An alias is simply a reference to a string, but it makes it possible to shorten a command (or set of commands) inclusive of any options for the command.
Why use aliases?
To save time. Programmers like to refer to themselves as a lazy bunch, but I prefer to think in terms of being efficient rather than lazy. By creating an alias you can save time on the command line. For instance, to view a list of the contents of a directory I like to use “ls -alhF” to generate a nice human readable list. I do this a lot when working from the command line, so it’s much easier to have a alias for this.
Creating aliases
Use the alias command to create your own aliases using the following general syntax: alias [-p] [name=”value”]. For example, to create an alias named ll that executes the command+options ls -alhF, you would execute the following:
alias ll="ls -alhF"
Using aliases
Using the ll alias created in the example above is as simple as typing:
ll
Removing Aliases
Use the general syntax: unalias [-a] name(s). For Example, to remove the alias named ll execute the following:
unalias ll
My favorite aliases
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
alias l="ls -CF"
alias ll="ls -alhF"
alias la="ls -A"
alias cp="cp -iR"
alias df="df -h"
alias v="vim"
alias md="mkdir"
alias rd="rmdir"
# Add an "alert" alias for long running commands. Use like so: sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
Making aliases permanent (survive sessions and reboot)
edit the file /etc/bashrc for system wide aliases
edit the file ~/.bashrc for user specific aliases
For macOS
create the file ~/.bash_profile and add this:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
Leave a Reply
Want to join the discussion?Feel free to contribute!