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 grep from sorted by
Terminal - Commands using grep - 1,583 results
netstat | grep EST | awk '{print $5}' | sort
2009-11-24 13:38:28
User: unixoid
Functions: awk grep netstat
-2

Sometimes I need a quick visual way to determine if there is a particular server who is opening too many connections to the database machine.

grep -H -n "pattern" *
2009-11-24 08:48:38
Functions: grep
Tags: find
0

if its the current directory, no need find command. just grep will do

find ./ -name $1 -exec grep -H -n $2 '{}' ';'
grep -Eio '([[:alnum:]_.-]+@[[:alnum:]_.-]+?\.[[:alpha:].]{2,6})'
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.

lsusb -v | grep -o "[0-9A-Z]{16}"
2009-11-22 00:58:00
User: matthewbauer
Functions: grep lsusb
Tags: ipod
-1

This is a bit of a hack, but it will get your fwguid which is needed sometimes when using your iPod.

video=$(ls /tmp | grep -e Flash\w*); ffmpeg -i /tmp/$video -f mp3 -ab 192k ~/ytaudio.mp3
for item in *;do echo -n "$item - ";find "$item" -type f -print0 | xargs -0 file -iNf - | grep video | cut -d: -f1 | xargs -d'\n' /usr/share/doc/mplayer/examples/midentify | grep ID_LENGTH | awk -F= '{sum+=$2} END {print(sum/60)}'; done | grep -v ' - 0$'
2009-11-19 06:28:15
User: jnash
Functions: awk cut echo file grep xargs
0

I know this has been beaten to death but finding video files using mime types and printing the "hours of video" for each directory is (IMHO) easier to parse than just a single total. Output is in minutes.

Among the other niceties is that it omits printing of non-video files/folders

PS: Barely managed to fit it within the 255 character limit :D

find ./ -type f -print0 | xargs -0 file -iNf - | grep video | cut -d: -f1
2009-11-19 06:05:36
User: jnash
Functions: cut file find grep xargs
0

Uses mime-type of files rather than relying on file extensions to find files of a certain type.

This can obviously be extended to finding files of any other type as well.. like plain text files, audio, etc..

In reference to displaying the total hours of video (which was earlier posted in command line fu, but relied on the user having to supply all possible video file formats) we can now do better:

find ./ -type f -print0 | xargs -0 file -iNf - | grep video | cut -d: -f1 | xargs -d'\n' /usr/share/doc/mplayer/examples/midentify | grep ID_LENGTH | awk -F "=" '{sum += $2} END {print sum/60/60; print "hours"}'
apod(){ local x=http://antwrp.gsfc.nasa.gov/apod/;feh $x$(curl -s ${x}astropix.html|grep -Pom1 'image/\d+/.*\.\w+');}
2009-11-18 12:06:03
User: eightmillion
Functions: grep
3

Substitute feh for the image viewer of your choice. display (part of imagemagick) seems to be a popular choice.

/usr/share/mplayer/midentify.sh `find . -name "*.avi" -print` | grep ID_LENGTH | awk -F "=" '{sum += $2} END {print sum/60/60; print "hours"}'
2009-11-17 03:33:20
User: equant
Functions: awk grep
4

midentify.sh is part of mplayer, but you might have to locate it on your box.

sensors | grep "Core 1" | [[ `sed -e 's/^.*+\([0-9]\{2,3\}\).*(.*/\1/'` -gt 50 ]] && notify-send "Core 1 temperature exceeds 50 degrees"
2009-11-12 21:51:21
Functions: grep sensors
1

You'll be notified if your core 1 temperature exceeds 50 degrees, you can change the monitored device by editing the "Core 1" or change the critical temperature by editing the "-gt 50" part.

Note: you must have lm-sensors installed and configured in order to get this command working.

curl http://www.spam.la/?f=sender | grep secs| awk '{print; exit}' | osd_cat -i 40 -d 30 -l 2
2009-11-12 21:33:06
User: m33600
Functions: awk grep
-1

I have a custmer's Geovision DVR installed on a closed proxi (only logme-in reaches it).

I have to check for reliability but logmein hangs and is too slow a process

I made the Geovision software send e-mail every minute to the www.spam.la site.

All this script does is to retrieve the e-mail header from spam.la ( no login!), filtering sender, stopping at the first occurrence of the word "secs" ( the age of the last e-mail ).

The result is the age of the sender's last e-mail, tiny published on top of my screen once a minute.

I can refresh www.spam.la via web browser, but have other things to do.

I use it inside Kalarm ( kde task schedule ) set to 1 minute repeat.

It can be done without kalarm, using Watch outside the script.

Try it out now using my account = geo1 ( change sender by geo1 in this script)

Needs curl , osd-bin

find -name '*.avi' | while read i ; do echo $(mplayer -identify -frames 0 -vo null -nosound "$i" 2>&1 | grep ID_LENGTH | cut -d= -f2)" ""$i" ;done | sort -k1 -r -n | sed 's/^\([^\ ]*\)\ \(.*\)$/\2:\1/g'
2009-11-09 17:14:59
User: ZungBang
Functions: cut echo find grep read sed sort
2

handles file names with spaces and colons, fixes sort (numeric!), uses mplayer, same output format as other alternatives

for i in *.avi; do echo -n "$i:";mediainfo $i|head | grep PlayTime | cut -d: -f2 ; done | sort -t: -k2 -r
2009-11-09 12:42:20
User: yooreck
Functions: cut echo grep head sort
0

Similar but using mediainfo instead of totem-something

for i in *.avi; do echo -n "$i:";totem-gstreamer-video-indexer $i | grep DURATION | cut -d "=" -f 2 ; done | sort -t: -k2 -r
2009-11-09 02:59:53
User: iadbungler
Functions: cut echo grep sort
1

Sort .avi movies by time length, print the longest first, and so on...

mpg123 `curl -s http://blip.fm/all | sed -e 's#"#\n#g' | grep mp3$ | xargs`
grep -c '^From ' mbox_file
GeoipLookUp(){ curl -A "Mozilla/5.0" -s "http://www.geody.com/geoip.php?ip=$1" | grep "^IP.*$1" | html2text; }
2009-11-06 00:32:27
User: sputnick
Functions: grep
Tags: sed curl
3

That makes a function you can put in your ~/.bashrc to run it when you need in any term with an IP as argument

ip link show eth0 | grep "link/ether" | awk '{print $2}'
2009-11-05 17:06:15
User: maxmanders
Functions: awk grep link
Tags: mac
0

...or for a particular interface...

grep "model name" /proc/cpuinfo
2009-11-05 05:23:30
User: getkaizer
Functions: grep
Tags: grep cpuinfo
3

Extracts the model name of the CPU and displays it on screen.

lynx --dump http://ip.boa.nu|sed -e 's/^[[:space:]]*//' -e 's/*[[:space:]]$//'|grep -v ^$
2009-11-04 22:23:45
User: xeor
Functions: grep sed
-5

For those of us that still uses lynx :)

ip link | grep 'link/ether' | awk '{print $2}'
2009-11-04 19:41:26
User: markdrago
Functions: awk grep link
Tags: mac
1

I much prefer using /sbin/ip over /sbin/ifconfig for most everything. I find the interface and output to be much more consistent and it has many abilities that ifconfig, route, etc. do not. To get the mac address for only one interface, add 'show dev [interface]' to the 'ip link' part of the command: ip link show dev eth0 | grep 'link/ether' | awk '{print $2}' . Also, both this command and the ifconfig one do not require root access to run, so the sudo is not necessary.

sudo ifconfig -a | grep eth | grep HW | cut -d' ' -f11
ps -ec -o command,rss | grep Stainless | awk -F ' ' '{ x = x + $2 } END { print x/(1024) " MB."}'
2009-11-04 19:01:22
Functions: awk grep ps
0

Adds up the total memory used by all Stainless processes: 1 Stainless, 1 StainlessManager and 1 StainlessClient per tab open.