A Nice tip for those who need to check for Gmail messages from a Linux Terminal.
Before you ask, why anyone would want to use gmail from the command line nowadays?, We all know that Gmail has a clean and nice user friendly interface. But there are people that would prefer to use the terminal for checking emails. One of reasons is the shell scripting on Linux operation systems, you can easily automate the whole process by writing simple bash scripts and make your life much easier. Another reason would be because it uses far less bandwidth compared to the Gmail tab, which on chrome for instance it can take up 550MB of RAM.
Accessing Gmail from the terminal is an alternative of having Evolution, Thunderbird and other clients, for this you need the following softwares on your linux operation system: curl, awk and sed.
In Debian-like distros:
sudo apt-get install curl gawk | original-awk sed
* where: gawk | original-awk are two options, I prefer gawk.
After installing the dependencies, type the command:
curl -u username –silent “https://mail.google.com/mail/feed/atom” | perl -ne ‘print “\ t” if //; print “$ 2 \ n” if /<(title|name)>(.*)<\/\1>/;
And replace “username” for your account information user@gmail.com: password.
The second option is:
curl -u username: password –silent “https://mail.google.com/mail/feed/atom” | tr -d ‘\ n’ | awk -F ” ‘{for (i = 2; i <= NF; i ++) {print $ i}}’ | sed -n “s /\(.*\)<\/ title. * name> \ (. * \). * / \ 2 – \ 1 / p”
* I prefer to keep the –silent option, otherwise the curl will show more information.
To automate it, you can create a BASH script:
#!/bin/sh
curl -u your_user@gmail.com: your_password –silent “https://mail.google.com/mail/feed/atom” | tr -d ‘\ n’ | awk -F ” ‘{for (i = 2; i <= NF; i ++) {print $ i}}’ | sed -n “s /\(.*\)<\/ title. * name> \ (. * \). * / \ 2 – \ 1 / p”
exit 0
Grant execute permission and move it to /usr/local/bin.
chmod + x gmail.sh # this is the script name
sudo mv gmail.sh /usr/local/bin/
To run the script, type gmail.sh in the terminal.
I hope you enjoyed the post, please check our other post on how to create a GMAIL account with step by step information.