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.

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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags



Commands tagged sed from sorted by
Terminal - Commands tagged sed - 340 results
sed '4!d'
2011-09-05 17:54:57
User: totti
Functions: sed

d --> delete

!d ---> delete others

awk '{print $0/60;}' /proc/uptime
2011-08-31 10:41:21
User: ioggstream
Functions: awk

Get info directly from /proc/uptime

bc <<< `uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g'`
2011-08-29 10:24:43
User: totti
Functions: bc sed

Want to run scripts/programs in the system after starting X minute [ For letting the system to free ]? This will give uptime in minute.

seq 20 | sed '5,6 { w out.txt }' #Can't print correctly. See sample output
2011-08-29 10:17:47
User: totti
Functions: sed seq
Tags: sed

Can't print correctly in the command field.

There is a new line before } as follows

seq 20 | sed -n '5,6 { w out.txt


echo "abcde" | sed 's/./& /g'
shuf -n4 /usr/share/dict/words | sed -e ':a;N;$!ba;s/\n/ /g;s/'\''//g;s/\b\(.\)/\u\1/g;s/ //g'
2011-08-11 19:49:21
User: vasti
Functions: sed
Tags: sed regex xkcd shuf

This is what I came up to generate XKCD #936 style four-word password.

Since first letter of every word is capitalized it looks a bit more readable to my eyes.

Also strips single quotes.

And yes - regex is a bit of a kludge, but that's the bes i could think of.

TIME=$( { time redis-cli PING; } 2>&1 ) ; echo $TIME | awk '{print $3}' | sed 's/0m//; s/\.//; s/s//; s/^0.[^[1-9]*//g;'
2011-08-11 19:09:49
User: allrightname
Functions: awk echo sed time

Outputs the real time it takes a Redis ping to run in thousands of a second without any proceeding 0's. Useful for logging or scripted action.

ls -l | grep ^d | sed 's:.*\ ::g'
ls -l | grep ^d | sed 's:.*\ ::g'
2011-08-06 23:52:46
User: LinuxMan
Functions: grep ls sed
Tags: bash sed ls grep

Normally, if you just want to see directories you'd use brianmuckian's command 'ls -d *\', but I ran into problems trying to use that command in my script because there are often multiple directories per line. If you need to script something with directories and want to guarantee that there is only one entry per line, this is the fastest way i know

cat input.txt | sed 's/\\\033[^a-zA-Z]*.//g'
gpg --list-sigs | sed -rn '/User ID not found/s/^sig.+([a-FA-F0-9]{8}).*/\1/p' | xargs -i_ gpg --keyserver-options no-auto-key-retrieve --recv-keys _
2011-07-22 16:31:25
User: lingo
Functions: gpg sed xargs
Tags: GPG sed fetch

For instance, if people have signed your key, this will fetch the signers' keys.

curl -s --compressed http://funnyjunk.com | awk -F'"' '/ '"'"'mainpagetop24h'"'"'/ { print "http://funnyjunk.com"$4 }' | xargs curl -s | grep -o 'ht.*m/pictures/.*\.jpg\|ht.*m/gifs/.*\.gif' | grep "_......_" | uniq | xargs wget
2011-07-21 15:57:21
User: laniner
Functions: awk uniq xargs

If your version of curl does not support the --compressed option, use

curl -s http://funnyjunk.com | gunzip

instead of

curl -s --compressed http://funnyjunk.com
sed "s/\s\+/;/g;s/^ //;s/ $//" filename.csv
sed -i '1s/^/text to prepend\n/' file1
2011-06-25 12:02:11
User: xeor
Functions: sed
Tags: sed replace

Using the sed -i (inline), you can replace the beginning of the first line of a file without redirecting the output to a temporary location.

sed -i 's/\r\n//' file.txt
gpg --refresh-keys
history | tail -(n+1) | head -(n) | sed 's/^[0-9 ]\{7\}//' >> ~/script.sh
2011-06-08 13:40:58
Functions: head sed tail

Uses history to get the last n+1 commands (since this command will appear as the most recent), then strips out the line number and this command using sed, and appends the commands to a file.

sed -n -e 1186,1210p A-small-practice.in
2011-06-04 10:53:46
User: evandrix
Functions: sed
Tags: sed

Using sed to extract lines in a text file

If you write bash scripts a lot, you are bound to run into a situation where you want to extract some lines from a file. Yesterday, I needed to extract the first line of a file, say named somefile.txt.

cat somefile.txt

Line 1

Line 2

Line 3

Line 4

This specific task can be easily done with this:

head -1 somefile.txt

Line 1

For a more complicated task, like extract the second to third lines of a file. head is inadequate.

So, let's try extracting lines using sed: the stream editor.

My first attempt uses the p sed command (for print):

sed 1p somefile.txt

Line 1

Line 1

Line 2

Line 3

Line 4

Note that it prints the whole file, with the first line printed twice. Why? The default output behavior is to print every line of the input file stream.

The explicit 1p command just tells it to print the first line .... again.

To fix it, you need to suppress the default output (using -n), making explicit prints the only way to print to default output.

sed -n 1p somefile.txt

Line 1

Alternatively, you can tell sed to delete all but the first line.

sed '1!d' somefile.txt

Line 1

'1!d' means if a line is not(!) the first line, delete.

Note that the single quotes are necessary. Otherwise, the !d will bring back the last command you executed that starts with the letter d.

To extract a range of lines, say lines 2 to 4, you can execute either of the following:

sed -n 2,4p somefile.txt sed '2,4!d' somefile.txt

Note that the comma specifies a range (from the line before the comma to the line after).

What if the lines you want to extract are not in sequence, say lines 1 to 2, and line 4?

sed -n -e 1,2p -e 4p somefile.txt

Line 1

Line 2

Line 4

grep -E '<DT><A|<DT><H3' bookmarks.html | sed 's/<DT>//' | sed '/Bookmarks bar/d' | sed 's/ ADD_DATE=\".*\"//g' | sed 's/^[ \t]*//' | tr '<A HREF' '<a href'
2011-05-26 22:21:01
User: chrismccoy
Functions: grep sed tr
Tags: sed grep chrome

chrome only lets you export in html format, with a lot of table junk, this command will just export the titles of the links and the links without all that extra junk

ls * | while read fin;do fout=$(echo -n $fin | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e);if [ "$fout" != "$fin" ];then echo "mv '$fin' '$fout'";fi;done | bash -x
2011-05-18 07:24:54
User: pawelb1973
Functions: bash echo ls read sed xargs

urldecode files in current directrory

tcpdump -w "$(sed 's/-//gi; s/ /_/gi'<<<"-vvv -s0 -ieth1 -c10 icmp").pcap"
links -dump "http://spaceflight.nasa.gov/realdata/sightings/cities/view.cgi?country=United_States&region=Wisconsin&city=Portage" | sed -n '/--/,/--/p'
2011-05-03 12:15:56
User: eightmillion
Functions: sed
Tags: sed links iss

This command outputs a table of sighting opportunities for the International Space Station. Find the URL for your city here: http://spaceflight.nasa.gov/realdata/sightings/

sed 's/^#\(.*DEBUG\)/\1/' $FILE
ls -1 | sort -R | sed -n 's/^/Selected /;1p'
sed "s/^ *//;s/ *$//;s/ \{1,\}/ /g" filename.txt
2011-03-09 10:35:02
User: EBAH
Functions: sed
Tags: sed

The command removes all the spaces whithin a file and leaves only one space.