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 using mail from sorted by
Terminal - Commands using mail - 26 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

mail -s "myip" youremail@domain.com
sleep 15 ; `echo "done" | mail -s "done" 4158575309@txt.att.net`
2012-04-16 01:28:16
User: vajrapani666
Functions: mail sleep
-1

Replace "4158575309@txt.att.net" with your carrier's SMS gateway identifier. The one in the sample is for AT&T. More here https://en.wikipedia.org/wiki/List_of_SMS_gateways. Helpful for getting notifications about long running commands. ";" executes regardless of exit status of last command. && will only notify you if the command succeeds.

10,30,50 * * * * ping -q -c1 -w3 192.168.0.14 | grep '1 received' - || mail -ne -s'Host 192.168.0.14 not reachable' admin@example.com
2012-02-06 10:42:46
User: knoppix5
Functions: grep mail ping
-3

Every 20 minutes test if host with IP 192.168.0.14 is 'dead' or not reachable.

The line should be put in your crontab file.

<command>; echo "job done"|mail email@email.com -s'job done'
2011-05-05 18:13:17
User: windfold
Functions: echo mail
5

This is a two part command that comes in really handy if you're running commands that take longer than you're willing to wait. The commands are separated by the semicolon(;) The first command is whatever you're attempting to do. The second commands emails you after the job completes.

mail -H | grep '^.U' | wc -l
2011-04-12 20:15:10
User: Lewix
Functions: grep mail wc
0

Counts the number of new emails in the post office (or wherever mail is set up to check).

quickemail() { echo "$*" | mail -s "$*" email@email.com; }
2011-02-22 20:33:18
User: dbh
Functions: echo mail
3

I created this so I could send myself an email alert when a long-running job was finished, e.g.,

my_long_job.exe ; quickemail my_long_job.exe has finished
for url in `cat urls `; do title=`curl $url 2>&1 | grep -i '<title>.*</title>'` && curl $url > /tmp/u && mail -s "$title" your-private-instapaper-address@instapaper.com < /tmp/u ; done
2010-10-16 19:10:19
Functions: grep mail
-1

Note, you need to replace the email address with your private Instapaper email address.

There are a bunch of possible improvements such as,

- Not writing a temp file

- Doesnt strip tags (tho Instapaper does thankfully)

- Shouldnt require 2 curls

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).

HTMLTEXT=$( curl -s http://www.page.de/test.html > /tmp/new.html ; diff /tmp/new.html /tmp/old.html ); if [ "x$HTMLTEXT" != x ] ; then echo $HTMLTEXT | mail -s "Page has changed." mail@mail.de ; fi ; mv /tmp/new.html /tmp/old.html
2010-07-04 21:45:37
User: Emzy
Functions: diff echo mail mv
2

Checks if a web page has changed. Put it into cron to check periodically.

Change http://www.page.de/test.html and mail@mail.de for your needs.

cd /some/empty/folder/website_diffs/sitename && wget -N http://domain.com/ 2>&1 |grep -q "o newer" || printf "Sites web page appears to have updated.\n\nSuggest you check it out.\n\n"|mail -s "Sites page updated." david@email.com
2010-05-09 07:28:42
User: DaveQB
Functions: cd grep mail printf wget
2

A cronjob command line to email someone when a webpages homepage is updated.

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

mail -s scream-dump user@example.com < /tmp/scream-dump
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.

HDD=$(df | awk ' NR>3 (S=$5) (M=$6) { if (S>90) print "Your Systems "M" is """S" Full" } ') ; [[ $HDD ]] && echo "$HDD" | mail -s "Hard-Drives Full" TO@EMAIL.com -- -f FROM@EMAIL.com >/dev/null
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"

cat filename | uuencode filename | mail -s "Email subject" user@example.com
2009-09-21 04:13:50
User: amaymon
Functions: cat mail uuencode
Tags: cat mail
0

uuencode the file to appear as an attachment

cat filename | mail -s "Email subject" user@example.com
2009-09-20 01:38:23
Functions: cat mail
Tags: cat mail
2

This just reads in a local file and sends it via email. Works with text or binary. *Requires* local mail server.

system_profiler | mail -s "$HOSTNAME System Profiler Report" user@domain.com
2009-08-11 20:16:37
User: monkeymac
Functions: mail
4

Replace "user@domain.com" with the target e-mail address. Thanks to alediaz for "$HOSTNAME" which is very useful when running the command with Apple Remote Desktop to multiple machines simultaneously.

uuencode archive.tar.gz archive.tar.gz | mail -s "Emailing: archive.tar.gz" user@example.com
2009-05-19 07:17:37
Functions: mail uuencode
5

The uuencode utility will encode your file so that it can be sent as an attachment to an email. It is part of the sharutils package in RHEL/CentOS/Fedora.

tar cvzf - data1 data2 | uuencode data.tar.gz | mail -s 'data' you@host.fr
2009-05-01 23:13:08
User: log0
Functions: mail tar uuencode
1

An easy one but nice to keep in mind.

ifconfig en1 | awk '/inet / {print $2}' | mail -s "hello world" email@email.com
2009-04-28 06:01:52
User: rez0r
Functions: awk ifconfig mail
9

This is useful if you have need to do port forwarding and your router doesn't assign static IPs, you can add it to a script in a cron job that checks if you IP as recently changed or with a trigger script.

This was tested on Mac OSX.