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,519 results
xmms2 mlib search NOT +rating | grep -r '^[0-9]' | sed -r 's/^([0-9]+).*/\1/' | sort -R | head | xargs -L 1 xmms2 addid
2009-04-16 20:27:30
Functions: grep head sed sort xargs
3

If you're like me and want to keep all your music rated, and you use xmms2, you might like this command.

I takes 10 random songs from your xmms2 library that don't have any rating, and adds them to your current playlist. You can then rate them in another xmms2 client that supports rating (I like kuechenstation).

I'm pretty sure there's a better way to do the grep ... | sed ... part, probably with awk, but I don't know awk, so I'd welcome any suggestions.

locate searchstring | xargs grep foo
2009-04-16 12:51:24
User: zimon
Functions: grep locate xargs
Tags: grep locate
-3

Greps located files for an expression.

Example greps all LaTeX files for 'foo':

locate *.tex | xargs grep foo

To avoid searching thousands of files with grep it could be usefull to test first how much files are returned by locate:

locate -c *.tex
echo "12345,12346" |sed -e's/ //'|tr "," "\n"| while read line; do echo -n $line" "; svn log -vr $line https://url/to/svn/repository/|grep "/"|head -1|cut -d"/" -f2; done
cd ~/.mozilla/firefox/ && sqlite3 `cat profiles.ini | grep Path | awk -F= '{print $2}'`/formhistory.sqlite "select * from moz_formhistory" && cd - > /dev/null
2009-04-13 20:23:37
User: klipz
Functions: awk cd grep
19

When you fill a formular with Firefox, you see things you entered in previous formulars with same field names. This command list everything Firefox has registered. Using a "delete from", you can remove anoying Google queries, for example ;-)

svn status | grep "^\?" | awk '{print $2}' | xargs svn add
yum erase `yum list installed | grep 'php'`
2009-04-13 12:58:16
Functions: grep
-4

This is useful for remove all packages that are part of a common suite.

alias lg='ls --color=always | grep --color=always -i'
2009-04-11 23:15:12
User: kFiddle
Functions: alias grep
Tags: ls alias color grep
6

This is a simple command, but extremely useful. It's a quick way to search the file names in the current directory for a substring. Normally people use "ls *term*" but that requires the stars and is not case insensitive. Color (for both ls and grep) is an added bonus.

svn status | grep '^?' | awk '{ print $2; }' | xargs svn add
2009-04-10 21:55:37
Functions: awk grep xargs
Tags: svn awk xargs
1

Lists the local files that are not present in the remote repository (lines beginning with ?)

and add them.

identify -verbose *.jpg|grep "\(Image:\|Resolution\)"
#!/bin/sh for dir in `ls -A | grep -v .sh`; do chown -R $dir:$dir $dir done
function jumpTo { xmms2 jump `xmms2 list | grep -i '$1' | head -n 1 | tail -n 1 | sed -re 's@.+\[(.+)/.+\] (.+)@\1@'`; }
2009-04-10 13:43:57
User: pyrho
Functions: grep head sed tail
Tags: xmms2
1

Usage:

Declare this function in your Shell, then use it like this:

> jumpTo foo

The script will search for the 'foo' pattern in your current xmms2 playlist (artist or songname), and play the first occurence of it !

svn info | grep '^URL:' | egrep -o '(tags|branches)/[^/]+|trunk' | egrep -o '[^/]+$'
2009-04-09 20:08:45
User: isaacs
Functions: egrep grep info
Tags: svn grep
1

Get the svn info, grep for the "URL" of the repository, pull out the tag/branch/trunk, and then just show the helpful/meaningful bit.

grep -r "mystring" . |uniq | cut -d: -f1 | xargs sed -i "s/mystring//"
2009-04-09 12:49:01
Functions: cut grep sed uniq xargs
-3

Linux : these script enable you to edit multiple files and remove exact phrase from multiple files

grep "install " /var/log/dpkg.log | awk '{print $4}' | xargs apt-get -y remove --purge
sudo tcpdump -i en1 -n -s 0 -w - | grep -a -o -E "Host\: .*|GET \/.*"
2009-04-04 01:41:48
User: peterc
Functions: grep sudo tcpdump
-2

Replace "en1" with your network interface (on OS X, usually en0, en1, eth0, etc..)

dmesg | grep cpu
2009-04-03 01:57:04
User: kingji
Functions: dmesg grep
Tags: cpus
-16

get cpu info from dmesg

history | grep ssh
2009-04-03 01:35:52
User: haivu
Functions: grep
Tags: bash csh
-9

After seeing the command you wish to repeat, just invoke it using the ! syntax.

ls -l | grep ^l
2009-04-02 17:47:36
User: haivu
Functions: grep ls
Tags: ls
-6

Shows all linked file and destinations. The 'ls -l' command lists the files in long (1 file per line) format, and the grep command displays only those lines that starts with an l (lower case L) -- a linked file.

Updated: Remove reference to hard links because this command does not apply to hard link as others kindly pointed out.

read -p 'Username: ' u;sudo -H -u $u xauth add $(xauth list|grep :$(echo ${DISPLAY: -4:2}));sudo su - $u
u=`curl -d 'dl.start=Free' $(curl $1|perl -wpi -e 's/^.*"(http:\/\/rs.*)" method.*$/$1/'|egrep '^http'|head -n1)|grep "Level(3) \#2"|perl -wpi -e 's/^.*(http:\/\/rs[^\\\\]*).*$/$1/'`;sleep 60;wget $u
while true; do ifconfig eth0 | grep "inet addr:"; sleep 60; done;
2009-04-01 19:29:28
User: bandit36
Functions: grep ifconfig sleep
Tags: script while loop
-5

You can use this to loop any command to periodically see the output.

while true; do [YOUR COMMAND HERE]; sleep [# of seconds]; done;

Alternatively, you can put it in a simple shell script to save typing!

#/!bin/bash

while true; do $1; sleep $2; done;

/path/to/script.sh "ifconfig eth0 | grep 'inet addr:'" 60
find . -name "*.py" -exec grep -n -H -E "^(import|from) math" {} \;
ssh -vN hostname 2>&1 | grep "remote software version"
2009-03-31 18:28:41
User: sud0er
Functions: grep hostname ssh
Tags: ssh
1

I used this to confirm an upgrade to an SSH daemon was successful

date +%m/%d/%y%X|tr -d 'n' >>datemp.log&& sensors|grep +5V|cut -d "(" -f1|tr -d 'n'>> datemp.log && sensors |grep Temp |cut -d "(" -f1|tr -d 'n'>>datemp.log
2009-03-31 18:13:23
User: f241vc15
Functions: cut date grep sensors tr
0
cat datemp.log

04/01/0902:11:42

Sys Temp: +11.0?C

CPU Temp: +35.5?C

AUX Temp: +3.0?C

find . -type f -exec grep StringToFind \{\} --with-filename \;|sed -e '/svn/d'|sed -e '/~/d'
2009-03-31 18:09:31
User: f241vc15
Functions: find grep sed
-3

Look for a string in one of your codes, excluding the files with svn and ~ (temp/back up files). This can be useful when you're looking for a particular string in one of your source codes for example, inside a directory which is under version control (e.g. svn), removing all the annoying files with ~ (tilde) from the search. you can even change the command after -exec to delete (rm) or view (cat) files found by 'find' for example