6 things you should know about bash aliases
Yassine CHAOUCHE
Linux sys admin, python developer and amateur photo/digital art editor
1. Aliases don't have to be trivial
A few examples
# Window Identification
alias wid='ps $(xprop | (pid=$(command grep PID); echo ${pid##*=}))'
# puts the shrugg ASCII emoji in the clipboard
alias shrugg='echo -n "ˉ\_(ツ)_/ˉ" | xclip -i; echo "ˉ\_(ツ)_/ˉ copied to the clipboard"'
# Capturing desktop with ffmpeg, video = wo/ sound, av = w/ sound.
alias desktop.capture.video='ffmpeg -f x11grab -r 25 -s 1900x1000 -i :0.0+0,24 -vcodec libx264 -threads 0'
alias desktop.capture.av='ffmpeg -f x11grab -r 25 -s 1900x1000 -i :0.0+0,24 -vcodec libx264 -threads 0 -f alsa -i hw:0 '
# how many days I've been working @ my current job.
# $ work
# 2828 days, 0:00:00
# $
alias work='python -c "import datetime; print datetime.date.today() - datetime.date(2014,11,17)"'
# grep an IP address
alias grep.ip="grep '\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}'"
# Remove blanks and comments from text files
# Useful when you want to see that config file in a single terminal page
alias pretty.remove.blanks+comments="egrep -v '(^[[:space:]]*#|^$|^[[:space:]]*//|^[[:space:]]*;)'"
# +--------------------+
# | system information |
# +--------------------+
alias sys.distro="for file in /etc/{*issue*,*version*,*release*}; do [[ -f \$file ]] && cat \$file && break; done;"
alias sys.processes.cpu.highest="ps ux | sort -k3nr | head -1"
alias sys.processes.rss.highest="ps ux | sort -k6nr | head -1"
alias sys.processes.mem.highest="sys.processes.rss.highest"
2. Aliases can be used to suite your own way of thinking about things.
Why do I have to remember the command to print my local IP and the other command for my public IP? Let's have all IP related commands aliased with a net.ip prefix. This also leverages the power of tab completion when looking for such commands so I don't even have to remember them, I just access them.
alias net.ip.private="ifdata -pa $INTERFACE"
alias net.ip.public='curl ifconfig.me; echo'
# Have all package related commands under the package prefix
alias package.is.installed="dpkg-query -W"
alias package.search.byname="apt-cache search"
alias package.search.byfile="apt-file search"
alias package.install='_asroot apt-get install'
alias package.remove='_asroot apt-get remove'
3. Aliases don't have to be oneliners
See this example
# _extract_clean_line : used to parse postfix log files
#
# $ _extract_mail_to_mail /var/log/mail.log | head -2
# Aug 15 06:30:02 messagerie amavis[52012]: (52012-17) Passed CLEAN {RelayedOpenRelay}, [127.0.0.1] <[email protected]> -> <[email protected]>, Message-ID: <[email protected]>, mail_id: qJKvO_yRwa1V, Hits: -0.011, size: 1434, queued_as: BC2573A80097, 293 ms
# Aug 15 06:30:16 messagerie amavis[53456]: (53456-14) Passed CLEAN {RelayedOpenRelay}, [127.0.0.1] <[email protected]> -> <[email protected]>, Message-ID: <[email protected]>, mail_id: V89PBpOj6gnr, Hits: -0.011, size: 13257, queued_as: 716C23A80097, 756 ms
# $ _extract_mail_to_mail /var/log/mail.log | head -2 | _extract_clean_line
# Aug 15 06:30:02 [127.0.0.1] <[email protected]> -> <[email protected]>, Hits: -0.011
# Aug 15 06:30:16 [127.0.0.1] <[email protected]> -> <[email protected]>, Hits: -0.011
# $
#
alias _extract_clean_line="
sed -u \"
# removes prefixes like
# messagerie amavis[31953]: (31953-20) Passed SPAMMY {RelayedTaggedInbound},
s/messagerie[^}]\+\}, //;
# removes trailing info like
# Queue-ID: DFFC83A800A5, Message-ID: <[email protected]>, mail_id: UXj46jSTdsyC, Hits: 7.857,
# becomes :
# Hits: 7.857,
s/\(Queue\|Message\)-ID.*Hits/Hits/;
# removes trailing line after Hits w/o removing Hits itself
# Hits: 7.857, size: 25552, queued_as: 7286B3A800A7, 5432 ms
# becomes
# Hits: 7.857
s/, size.*//;
# puts e-mail addresses on their own lines, instead of all in one line.
s/,<\([[:alnum:]\.@-]\+\)>/\n <\1>/g\""
4. Aliases can be combined
mail.exchange.live is an alias composed of aliases
alias _extract_mail_to_mail="egrep --line-buffered \"<$REGX_EMAIL> -> <$REGX_EMAIL>\""
alias mail.exchange.live='tail -f /var/log/mail.log | _extract_mail_to_mail | _extract_clean_line'
_extract_clean_line was already pasted above
both _extract_clean_line and _extract_mail_to_mail are used in many other aliases and functions
领英推荐
alias mail.spam.report="_extract_mail_to_mail /var/log/mail.log | grep SPAM | _extract_clean_line"
alias mail.exchange.from.outside="_extract_mail_to_mail /var/log/mail.log | grep -v 'mydomain.tld> ->' | _extract_clean_line"
function mail.exchange.from(){
_extract_mail_to_mail /var/log/mail.log | grep -- "$1.*->" | _extract_clean_line
}
function mail.exchange.to(){
_extract_mail_to_mail /var/log/mail.log | grep -- "->.*$1" | _extract_clean_line
}
if you're curious about what these do, here are two examples :
1. mail.exchange.to
$ mail.exchange.to [email protected] | tail -5
Aug 16 11:07:03 [168.100.1.4]:15043 [217.182.79.147] <[email protected]> -> <[email protected]>, Hits: -1.557
Aug 16 11:49:34 LOCAL [10.10.10.123]:64801 <[email protected]> -> <[email protected]>, Hits: -0.769
Aug 16 12:17:49 [168.100.1.4]:60799 [2604:8d00:189::2] <[email protected]> -> <[email protected]>, Hits: -1.557
Aug 16 12:22:10 [168.100.1.4]:55331 [2604:8d00:189::2] <[email protected]> -> <[email protected]>, Hits: -1.557
Aug 16 12:33:56 [185.49.141.25]:33725 [2a04:b900::1:0:0:15] <[email protected]> -> <[email protected]>, Hits: -3.412
$
2. mail.spam.report
$ mail.spam.report | tail -5
Aug 16 10:21:52 [197.112.10.13]:17337 [197.112.10.13] <[email protected]> -> <[email protected]>, Hits: 8.538
Aug 16 12:07:45 LOCAL [192.168.211.95]:36104 <[email protected]> -> <[email protected]>, Hits: 7.891
Aug 16 12:17:30 [63.209.32.180]:39282 [197.201.1.54] <[email protected]> -> <[email protected]>, Hits: 5.907
Aug 16 12:19:05 [192.249.57.241]:59942 [194.111.216.102] <[email protected]> -> <[email protected]>, Hits: 5.907
Aug 16 12:34:44 [178.62.40.198]:43976 [46.183.222.41] <https://www.dhirubhai.net/redir/general-malware-page?url=kites%40enterpreneurdirection%2einfo> -> <[email protected]>, quarantine: e/spam-eaU9U0MofNq1.gz, Hits: 17.991
$
5. Aliases may contain loops and variables
You just have to be careful to escape the dollar sign when creating and using variables inside aliases. For example :
alias flow.tasks.list="for file in ~/NOTES/LOG/TASKS/*.flow; do echo file \$file; echo '---------------------------------------------------'; egrep -h '^\*\* \[[0-9]' \$file; echo; done | less"
This produces the following output
/home/ychaouche/NOTES/LOG/TASKS/dns.www.ibm.com.12s.flow
---------------------------------------------------
** [1] dns query taking too long
** [1.1] query time
/home/ychaouche/NOTES/LOG/TASKS/download-podcasts.flow
---------------------------------------------------
** [1] see if savefrom.net works again
** [2] search for alternatives to savefrom.net
/home/ychaouche/NOTES/LOG/TASKS/erreur-certificat.flow
---------------------------------------------------
** [1] erreur
** [2] copy-heading.flow
/home/ychaouche/NOTES/LOG/TASKS/exploration-de-la-base-roundcube.flow
---------------------------------------------------
** [1] joins
** [2] requêtes sur la table contacts
** [3] cacher le mot de pass
6. Aliases may be called like commands
Consider this example
alias sys.window.pid="pid=\$(xprop | grep PID); echo \${pid##*=}; unset pid"
alias sys.window.trace="_asroot strace -p \$(sys.window.pid)"
Use sys.window.trace to run strace on any window by point and click. This uses sys.window.pid, also an alias that returns the pid of any window by point and click.
Notice how sys.window.pid is called like a command substitution. Without the $, you'd get strace complaining that sys.window.pid is an invalid pid.