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

Commands using sed from sorted by
Terminal - Commands using sed - 992 results
sed -n '10,20p' <filename>
2009-02-08 22:34:04
User: ergut
Functions: sed
47

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"
for i in *.bak ; do nuname=`echo $i | sed 's/\.[^\.]*$//'`; echo renaming $i to $nuname;mv $i $nuname; done
grep -o "\(new \(\w\+\)\|\w\+::\)" file.php | sed 's/new \|:://' | sort | uniq -c | sort
2009-01-26 12:08:47
User: root
Functions: grep sed sort uniq
-2

This grabs all lines that make an instantation or static call, then filters out the cruft and displays a summary of each class called and the frequency.

sed '1000000!d;q' < massive-log-file.log
2009-01-26 11:50:00
User: root
Functions: sed
17

Sed stops parsing at the match and so is much more effecient than piping head into tail or similar. Grab a line range using

sed '999995,1000005!d' < my_massive_file