Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged mail from sorted by
Terminal - Commands tagged mail - 33 results
nohup bash -c "while true; do ps -x | mail pascalv@mmmmail.com; sleep 3600; done" | mail pascalv@mmmmail.com &
2013-08-19 17:21:37
User: pascalv
Functions: bash mail nohup
1

Run "ps -x" (process status) in the background every hour (in this example).

The outputs of both "nohup" and "ps -x" are sent to the e-mail (instead of nohup.out and stdout and stderr).

If you like it, replace "ps -x" by the command of your choice, replace 3600 (1 hour) by the period of your choice.

You can run the command in the loop any time by killing the sleep process. For example

ps -x

2925 ? S 0:00.00 sh -c unzip E.zip >/dev/null 2>&1

11288 ? O 0:00.00 unzip E.zip

25428 ? I 0:00.00 sleep 3600

14346 pts/42- I 0:00.01 bash -c while true; do ps -x | mail pascalv; sleep 3600; done

643 pts/66 Ss 0:00.03 -bash

14124 pts/66 O+ 0:00.00 ps -x

kill 25428

You have mail in /mail/pascalv

sudo /usr/sbin/exim -bp | sed -n '/\*\*\* frozen \*\*\*/,+1!p' | awk '{print $1}' | tr -d [:blank:] | grep @ | sort | uniq -c | sort -n
notify-send -i mail_new 'Mail watcher' 'You have new mail!'
chromium --app="https://mail.google.com/mail/mu/mp/745/?mui=ca"
find . -path ".*/cur/*" -type f ! -newermt "1 week ago" -delete
2012-03-05 15:48:21
User: evolix
Functions: find
0

This can be used to delete or archive old mails. In fact, for archiving its a bit different, you need to archive mails with any tools (e.g archivemail), and then deleting (if you want!).

Here we use -path ".*/cur/*" to avoid files limit in bash globbing and to search in any inbox (e.g .mymail .spam .whatever).

! -newermt "1 week ago" can be read: All files which is older than "1 week ago", adapt it in consequence.

recoll -t -q "keyword" | grep message/rfc822 | sed -s 's,^.*\('$MAILDIR'[^]]*\)\].*$,\"\1\",' | xargs ln -sft $MAILDIR/bingo/cur/
mailme(){ mailx -s "$@" $USER <<< "$@"; }
2011-10-07 08:55:47
User: sharfah
Functions: mailx
Tags: email mail mailx
2

Usage: mailme message

This is a useful function if you want to get notified about process completion or failure. e.g.

mailme "process X completed"
mailq |awk '/MAILER-DAEMON/{gsub("*","");printf("postsuper -d %s\n",$1)}'|bash
2010-11-24 11:42:54
User: unefunge
Functions: awk mailq
-1

Used it on daily basis, not sure if it's any better than the OPs version, tho'

One advantage is - you can replace 'bash' at the end of the line with eg. cat - to check if the generated command is OK.

mailq | grep MAILER-DAEMON | awk '{print $1}' | tr -d '*' | postsuper -d -
dsniff -i any 'tcp port pop3'
2

dsniff is general purpose password sniffer, it handles *lots* of different protocols, but it also handles tcp-style expressions for limiting analyzed traffic - so I can limit it to work on pop3 only.

tcpdump -i eth0 "tcp port pop3 and ip[40] = 85 and ip[41] = 83" -s 1500 -n -w "sniff"
2010-11-18 09:03:08
User: ironmarc
Functions: tcpdump
0

The command is useful for monitoring the use of the boxes and their connection IP.

Result file "sniff" is readable with GUI program "wireshark" or through CLI with the command:

tcpdump -f "sniff" -XX

mailutil transfer {imap.gmail.com/ssl/user=john@gmail.com} Gmail/
2010-10-07 07:30:00
User: bogomips
Tags: mail gmail
8

Copies an entire hierarchy of mailboxes from the named POP3/IMAP/etc. source to the named destination. Mailboxes are created on the destination as needed. NOTE: The 'mailutil' is Washington's University 'mailutil' (apt-get install uw-mailutils). More examples

mailutil transfer {imap.gmail.com/ssl/user=you@gmail.com}INBOX Gmail/ ; mailutil check imap.gmail.com/ssl/user=you@gmail.com}\[Gmail\]/Spam

If you use the utility in the first, append -v|-d flag(s) to the end the commands above (man mailutil).

nc -zw2 www.example.com 80 || echo http service is down | mail -s 'http is down' admin@example.com
2010-09-08 13:18:16
User: putnamhill
Functions: echo mail
1

This version uses netcat to check a particular service.

ping -q -c1 -w3 server.example.com >& /dev/null || echo server.example.com ping failed | mail -ne -s'Server unavailable' admin@example.com
2010-09-08 12:19:29
User: brainstorm
Functions: echo mail ping
Tags: bash ping mail
2

For some reason the 2&>1 does not work for me, but the shorter stdout/stderr redirection >& works perfectly (Ubuntu 10.04).

google contacts list name,name,email|perl -pne 's%^((?!N\/A)(.+?)),((?!N\/A)(.+?)),([a-z0-9\._-]+\@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+([a-z]+\.)?([a-z]+))%${1}:${3} <${5}>%imx' #see below for full command
2010-07-12 16:50:44
User: Raymii
Functions: perl
1

Full Command:

google contacts list name,name,email|perl -pne 's%^((?!N\/A)(.+?)),((?!N\/A)(.+?)),([a-z0-9\._-]+\@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+([a-z]+\.)?([a-z]+))%${1}:${3} <${5}>%imx'|grep -oP '^((?!N\/A)(.+?)) <[a-z0-9\._-]+\@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+([a-z]+\.)?([a-z]+)>' | sort

You'll need googlecl and python-gdata. First setup google cl via:

google

Then give your PC access

google contacts list name,email

Then do the command, save it or use this one to dump it in the cone-address.txt file in your home dir:

google contacts list name,name,email | perl -p -n -e 's%^((?!N\/A)(.+?)),((?!N\/A)(.+?)),([a-z0-9\._-]+\@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+([a-z]+\.)?([a-z]+))%${1}:${3} <${5}>%imx' | grep -o -P '^((?!N\/A)(.+?)) <[a-z0-9\._-]+\@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+([a-z]+\.)?([a-z]+)>' | sort > ~/cone-adress.txt

Then import into cone.

It filters out multiple emails, and contacts with no email that have N/A. (Picasa photo persons without email for example...)

{ u="http://twitter.com/commandlinefu"; echo "Subject: $u"; echo "Mime-Version: 1.0"; echo -e "Content-Type: text/html; charset=utf-8\n\n"; curl $u ; } | sendmail recipient@example.com
2010-02-24 04:18:30
User: pascalv
Functions: echo sendmail
-1

This will send the web page at $u to recipient@example.com . To send the web page to oneself, recipient@example.com can be replaced by $(whoami) .

The "charset" is UTF-8 here, but any alternative charset of your choice would work.

`wget -O - -o /dev/null $u` may be considered instead of `curl $u` .

On some systems the complete path to sendmail may be necessary, for instance /sys/pkg/libexec/sendmail/sendmail for some NetBSD.

sed -i 's/oldname@example.com/newname@example.com/g' `grep oldname@example.com -rl .`
2010-02-18 18:26:09
User: and3k
Functions: sed
0

Do a recursive (-r) search with grep for all files where your old mail address is mentioned (-l shows only the file names) and use sed to replace it with your new address. Works with other search/replacement patterns too.

mailq | grep MAILER-DAEMON | awk ?{print $1}? | tr -d ?*? | postsuper -d -
mail -s "subject" user@todomain.com <emailbody.txt -- -f customfrom@fromdomain.com -F 'From Display Name'
2010-01-18 19:55:27
User: dmmst19
Functions: mail
Tags: cronjob mail
9

It's very common to have cron jobs that send emails as their output, but the From: address is whatever account the cron job is running under, which is often not the address you want replies to go to. Here's a way to change the From: address right on the command line.

What's happening here is that the "--" separates the options to the mail client from options for the sendmail backend. So the -f and -F get passed through to sendmail and interpreted there. This works on even on a system where postfix is the active mailer - looks like postfix supports the same options.

I think it's possible to customize the From: address using mutt as a command line mailer also, but most servers don't have mutt preinstalled.

{ echo -e "$body"; uuencode "$outfile" "$outfile"; } | mail -s "$subject" "$destaddr" ;
2009-12-10 18:08:59
User: glaudiston
Functions: echo mail uuencode
Tags: cat mail
0

on this way we can define the body too

awk '/q=/{print $11}' /var/log/httpd/access_log.4 | awk -F 'q=' '{print $2}' | sed 's/+/ /g;s/%22/"/g;s/q=//' | cut -d "&" -f 1
cat /var/log/httpd/access_log | grep q= | awk '{print $11}' | awk -F 'q=' '{print $2}' | sed 's/+/ /g;s/%22/"/g;s/q=//' | cut -d "&" -f 1 | mail youremail@isp.com -s "[your-site] search strings for `date`"
2009-11-22 03:03:06
User: isma
Functions: awk cat grep sed strings
-2

It's not a big line, and it *may not* work for everybody, I guess it depends on the detail of access_log configuration in your httpd.conf. I use it as a prerotate command for logrotate in httpd section so it executes before access_log rotation, everyday at midnight.

curl -fs brandx.jp.sme 2&>1 > /dev/null || echo brandx.jp.sme ping failed | mail -ne -s'Server unavailable' joker@jp.co.uk
2009-10-23 14:29:06
User: mccalni
Functions: echo mail ping
Tags: bash ping curl mail
2

Alternative to the ping check if your firewall blocks ping. Uses curl to get the landing page silently, or fail with an error code. You can probably do this with wget as well.

ping -q -c1 -w3 brandx.jp.sme 2&>1 /dev/null || echo brandx.jp.sme ping failed | mail -ne -s'Server unavailable' joker@jp.co.uk
2009-10-13 14:13:04
User: mccalni
Functions: echo mail ping
Tags: bash ping mail
6

Joker wants an email if the Brand X server is down. Set a cron job for every 5 mins with this line and he gets an email when/if a ping takes longer than 3 seconds.

echo "see attached file" | mail -a filename -s "subject" email@address
2009-09-21 11:58:49
User: gnpf
Functions: echo mail
Tags: mail
1

if "mail -a" fail, try "mutt -a" or "nail -a"