When working on Linux, some tasks are done faster using the terminal and are done even faster if you start to use some aliases and some shell functions.
Aliases
A shell alias is a way to assign a short name to commands, even with a list of default arguments.
The followings are a list of useful aliases that I use:
alias ls="ls --color=auto -F"
alias ll="ls --color=auto -l -h -F"
alias exa="exa --color-scale --git --group --header"
alias tree="exa --color-scale --git --group --header --tree"
alias diff="diff --color=auto"
alias ip="ip -color -human"
alias pstree="pstree -alpsSugU"
alias ssh-tmp="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
alias scp-tmp="scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
alias scan-ports="nmap -p 1-65535 -T4 -A -v"
alias python-http="python -m http.server"
Some of those aliases are used to execute the command with a default list of parameters (this is the case of ll
or tree
, for example), while others are used to add a default list of parameters to an existing command, using the name of the command itself for the alias (this is the case of ls
, exa
, diff
, ip
or pstree
, for example).
Just a note on the two aliases ssh-tmp
and scp-tmp
: are very useful to connect in SSH to a remote host one time, without saving in ~/.ssh/known_hosts
the host key (as you will never connect again to that host so it is useless to save the key).
The definition for the aliases can be saved in different places. It is sufficient that the file is sourced by the shell at startup. One of my prefered places is in a file inside /etc/profile.d/
, let’s call that file alias.sh
, for example.
For the exa
command, look at the exa website, it’s a modern and colourized ls
replacement. scan-ports
uses nmap to scan all ports on a destination host or network and python-http
just starts a HTTP server to serve the current directory on port 8000 (useful to share files on LAN, for example).
Functions
Shell functions are a way to automatize one or more command with a single name. Shell functions are like aliases, but you can have more than one command inside a function.
The followings are a list of useful functions that I use:
function mkcd()
{
mkdir -p "$1"
cd "$1"
}
function showjson()
{
echo "$@" | jq -C '.' | less
}
function b64()
{
echo "$@" | base64 -d
}
function code_here() {
/usr/bin/visual-studio-code . >/dev/null 2>&1 &!
}
It is possible, then, to use one of those functions as a regular command: mkcd /tmp/foo/bar/baz
, for example, will create the destination folder (if it does not exist yet) and then cd into it.
showjson
will use jq to format and colourize a JSON string passed as an argument, b64
will decode the base64 encoded string in the argument and code_here
will start Visual Studio Code in the current folder.