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 sed from sorted by
Terminal - Commands using sed - 1,078 results
find "$DIR" -regex "$FILENAME" -type f -print0 | xargs -0 sed -i _`date "+%y%m%d%H%M%S"` -E "s/$TEXT1/$TEXT2/g"
netstat -anp |grep 'tcp\|udp' | awk '{print $5}' | sed s/::ffff:// | cut -d: -f1 | sort | uniq -c | sort -n
sed -e "/$USER/d;s/:.*//g" /etc/group | sed -e :a -e '/$/N;s/\n/ /;ta'
sed -e :a -e '/$/N;s/\n/ /;ta' <filename>
curl -s http://myip.dk | grep '<title>' | sed -e 's/<[^>]*>//g'
2009-02-13 01:44:12
User: uzi
Functions: grep sed
3

add this alias in .bashrc to fast check the ip address of your modem router

alias myip="curl -s http://myip.dk | grep '' | sed -e 's/]*>//g'"

find . -name "*.php" -exec php -l {} \; | sed -e "/^No syntax/d"
2009-02-10 09:14:25
User: TomNomNom
Functions: find sed
1

Uses the PHP binary to check the syntax of all .php files in or below the current working directory. Really handy for doing that last minute check before you commit code to the repository.

ls -R | grep : | sed -e '\''s/:$//'\'' -e '\''s/[^-][^\/]*\//--/g'\'' -e '\''s/^/ /'\'' -e '\''s/-/|/'\''
2009-02-09 22:32:01
User: scw
Functions: grep ls sed
-3

Shows the current directory and those below it in a simple tree structure. Recommended use:

alias lt='$command_above'

grep -r "sampleString" . |uniq | cut -d: -f1 | xargs sed -i "/sampleString/d"
2009-02-09 20:21:30
User: sk1418
Functions: cut grep sed xargs
-2

checking files in current and sub directories, finding out the files containing "sampleString" and removing the containing lines from the file.

* Beware that The command will update the original file [no backup].

The command can be extended if play with 'find' command together,

e.g. it is possible to execute on certain type of files: *.xml, *.txt... (find -name "*.xml" | grep....)

if anybody knows a better solution on that, please drop a comment. thx.

sed -n 's/[ :]/_/g; s/^\(.\{1,\}\)_5_\($1$[$./0-9A-Za-z]\{27,31\}\)_*$/\1:\2/p' < cisco-device-config > passwd
svn st | cut -c 8- | sed 's/^/\"/;s/$/\"/' | xargs tar -czvf ../backup.tgz
2009-02-09 11:24:31
User: chrisdrew
Functions: cut sed tar xargs
12

Create a tgz archive of all the files containing local changes relative to a subversion repository.

Add the '-q' option to only include files under version control:

svn st -q | cut -c 8- | sed 's/^/\"/;s/$/\"/' | xargs tar -czvf ../backup.tgz

Useful if you are not able to commit yet but want to create a quick backup of your work. Of course if you find yourself needing this it's probably a sign you should be using a branch, patches or distributed version control (git, mercurial, etc..)

sed -e '/^[<space><tab>]*$/d' somefile
sed -n '10,20p' <filename>
2009-02-08 22:34:04
User: ergut
Functions: sed
54

Similarly, if you want to print from 10 to the end of line you can use: sed -n '10,$p' filename

This is especially useful if you are dealing with a large file. Sometimes you just want to extract a sample without opening the entire file.

Credit goes to wbx & robert at the comments section of http://www.commandlinefu.com/commands/view/348/get-line1000-from-text.#comment

sed -e 's/ *$//' ~/.bash_history | sort | uniq -cd | sort -nr | head
kill -HUP ` ps -aef | grep -i firefox | sort -k 2 -r | sed 1d | awk ' { print $2 } ' `
cflow file.c | grep ':$' | sed 's/ <.*//'
gsed -e :a -e 's/\(<\/[^>]*>\)/\1\n/g;s/\(<br>\)/\1\n/g' page2.txt | sed -n '/<cite>/p;s/<cite>\(.*\)<\/cite>/\1/g' >> output
2009-02-06 12:45:20
User: shrimphead
Functions: sed
-1

From a saved page of google search results, split out all of the links for the results. Useful for creating apache rewrite rules from.

sed "s/^ABC/+ABC/" <file | sed "s/DEF$/DEF+/" | tr "\n" "~" | tr "+" "\n" | grep "^ABC" | tr "~" "\n"
2009-02-06 11:29:29
User: poboxy
Functions: grep sed tr
2

If the file content is :

-

Blah blah blah

ABC

hello blah blah blah

bloh bloh bloh

DEF

Bah bah bah

-

You'll get:

-

ABC

hello blah blah blah

bloh bloh bloh

DEF

(sed 's/^/x+=/' [yourfile] ; echo x) | bc
2009-02-06 08:25:43
User: michelem
Functions: echo sed
7

If you have a file full of numbers written line by line, you can sum every line to get the total.

With a file like this:

3443535

9878977

67554

987798

232324

you will got:

14610188

cleartool co -nc `cleartool ls -recurse | grep "hijacked" | sed s/\@\@.*// | xargs`
xxd < orig | sed 's/A/B/' | sed 's/HEXA/HEXB/' | xxd -r > new
2009-02-05 20:25:04
User: wwest4
Functions: sed
3

Replaces A with B in binary file "orig" and saves the result to "new". You must have the hex representations of A & B. Try od: echo -e "A\c" | od -An -x

sed -i.`date +%Y%m%d` -e 's/pattern/replace' [filename]
2009-02-05 18:20:54
User: wwest4
Functions: sed
1

Does an in situ search-replace but leaves a datestamped backup. A variation with more precision:

sed -i.`date +%Y%m%d%H%M%S 's/pattern/replace' [filename]

sed -i 's/OLD/NEW/g' FILE
2009-02-05 18:07:41
User: nanexcool
Functions: sed
2

Very quick way to change a word in a file. I use it all the time to change variable names in my PHP scripts (sed -i 's/$oldvar/$newvar/g' index.php)

echo -n search\>\ ; read SEARCH_STRING && sed -n "/$SEARCH_STRING/{n;p;n;p;n;p;q}" [file-to-search]
2009-02-05 18:07:23
User: wwest4
Functions: echo read sed
0

customizable context searches - if you know sed, this is a basis for more complex context control than grep --context offers

pathrm() { PATH=`echo $PATH | sed -e "s=^${1}:==;s=:${1}$==;s=:${1}:=:="`; }
2009-02-05 16:25:12
User: wam
Functions: sed
2

Function to remove a specified path from your PATH environment variable.

NAME=$(nslookup $IP | sed -n 's/.*arpa.*name = \(.*\)/\1/p'); test -z "$NAME" && NAME="NO_NAME"; echo "$NAME"