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.

Universal configuration monitoring and system of record for IT.

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 - 338 results
git log --oneline | nl -v0 | sed 's/^ \+/&HEAD~/'
2015-11-23 21:35:57
User: flatcap
Functions: nl sed
Tags: git sed nl git-log

Print a git log (in reverse order) giving a reference relative to HEAD.

HEAD (the current revision) can also be referred to as HEAD~0

The previous revision is HEAD~1 then HEAD~2 etc.


Add line numbers to the git output, starting at zero:

... | nl -v0 | ...


Insert the string 'HEAD~' before the number using sed:

... | sed 's/^ \+/&HEAD~/'


Thanks to bartonski for the idea :-)

sed 's/,/\n/g;q' file.csv | nl
2015-08-26 11:38:56
User: flatcap
Functions: sed
Tags: sed nl

Take the header line from a comma-delimited CSV file and enumerate the fields.


First sed replaces all commas with newlines


Then sed quits (q) after the first line.

Finally, nl numbers all the lines

xxd -p source | fold -w2 | paste -sd' ' | sed "s/A/B/g" | xxd -p -r > destination
2015-05-26 18:29:48
User: hincor
Functions: fold paste sed
Tags: sed xxd fold paste

Replace all instances of "A" with "B" in file "source" saved as file "destination".

!! IF A/B is multi-byte, then separate bytes with spaces like so: "s/20\ 0A/00/g".

for f in `ls`; do sed -i '/MATCHING STRING/ { s/ORIGINAL/REPLACEMENT/; }' ${f} ; done
2015-05-21 19:37:42
User: krizzo
Functions: sed

Find and replace specific characters in a single line in multiple files with sed.

fold -sw 20 <(echo "Long Text to be wrapped with \"\n\"") |sed ':a;N;$!ba;s/ *\n/\\n/g'
2015-04-16 21:06:53
User: alecthegeek
Functions: echo fold sed

I used this fragment with Imagemagick convert so that I can place long text strings in pictures. The "\n" gets converted to a true newline in the image.

So this fragment uses fold command to wrap the line and then sed to convert newlines (and any trailing spaces on the line) to the text "\n"

function every() { sed -n -e "${2}q" -e "0~${1}p" ${3:-/dev/stdin}; }
2015-04-03 01:30:36
User: flatcap
Functions: sed

Thanks to knoppix5 for the idea :-)

Print selected lines from a file or the output of a command.


every NTH MAX [FILE]

Print every NTH line (from the first MAX lines) of FILE.

If FILE is omitted, stdin is used.

The command simply passes the input to a sed script:

sed -n -e "${2}q" -e "0~${1}p" ${3:-/dev/stdin}

print no output

sed -n

quit after this many lines (controlled by the second parameter)

-e "${2}q"

print every NTH line (controlled by the first parameter)

-e "0~${1}p"

take input from $3 (if it exists) otherwise use /dev/stdin

function every() { N=$1; S=1; [ "${N:0:1}" = '-' ] && N="${N:1}" || S=0; sed -n "$S~${N}p"; }
2015-03-21 23:44:59
User: flatcap
Functions: sed

Sometimes commands give you too much feedback.

Perhaps 1/100th might be enough. If so, every() is for you.

my_verbose_command | every 100

will print every 100th line of output.

Specifically, it will print lines 100, 200, 300, etc

If you use a negative argument it will print the *first* of a block,

my_verbose_command | every -100

It will print lines 1, 101, 201, 301, etc

The function wraps up this useful sed snippet:

... | sed -n '0~100p'

don't print anything by default

sed -n

starting at line 0, then every hundred lines ( ~100 ) print.


There's also some bash magic to test if the number is negative:

we want character 0, length 1, of variable N.


If it *is* negative, strip off the first character ${N:1} is character 1 onwards (second actual character).

nmap -sP | grep -v "Host" | tail -n +3 | tr '\n' ' ' | sed 's|Nmap|\nNmap|g' | grep "MAC Address" | cut -d " " -f5,8-15
2014-12-26 18:31:53
User: jaimerosario
Functions: cut grep sed tail tr

In the field, I needed to script a process to scan a specific vendor devices in the network. With the help of nmap, I got all the devices of that particular vendor, and started a scripted netcat session to download configuration files from a tftp server.

This is the nmap loop (part of the script). You can however, add another pipe with grep to filter the vendor/manufacturer devices only. If want to check the whole script, check in http://pastebin.com/ju7h4Xf4

sed -i 's/oldname/newname/' /etc/hosts /etc/hostname
2014-11-02 22:03:48
User: adria
Functions: sed
Tags: sed hostname host

With sed you can replace strings on the fly.

ip a s eth0 | sed -nr 's!.*inet ([^/]+)/.*!\1!p'
gcloud components list | grep "^| Not" | sed "s/|\(.*\)|\(.*\)|\(.*\)|/\2/" | xargs echo gcloud components update
2014-10-13 20:52:25
User: wires
Functions: echo grep sed xargs

Google Cloud SDK comes with a package manager `gcloud components` but it needs a bit of `sed` to work. Modify the "^| Not" bit to change the package selection. (The gcloud --format option is currently broken)

sed -e '/ s/^;//' -i test.txt
2014-10-13 13:37:53
User: suyashjain
Functions: sed
Tags: sed

This sed command will search for in all lines of test.txt and replace comment symbol ";" . You can use it for other purpose also.

/bin/ls -lF "$@" | sed -r ': top; s/. ([0-9]+)([0-9]{3}[,0-9]* \w{3} )/ \1,\2/ ; t top'
2014-09-29 14:33:23
User: hackerb9
Functions: sed

This modifies the output of ls so that the file size has commas every three digits. It makes room for the commas by destructively eating any characters to the left of the size, which is probably okay since that's just the "group".

Note that I did not write this, I merely cleaned it up and shortened it with extended regular expressions. The original shell script, entitled "sl", came with this description:

 : '

 : For tired eyes (sigh), do an ls -lF plus whatever other flags you give

 : but expand the file size with commas every 3 digits. Really helps me

 : distinguish megabytes from hundreds of kbytes...


 : Corey Satten, corey@cac.washington.edu, 11/8/89

 : '

Of course, some may suggest that fancy new "human friendly" options, like "ls -Shrl", have made Corey's script obsolete. They are probably right. Yet, at times, still I find it handy. The new-fangled "human-readable" numbers can be annoying when I have to glance at the letter at the end to figure out what order of magnitude is even being talked about. (There's a big difference between 386M and 386P!). But with this nifty script, the number itself acts like a histogram, a quick visual indicator of "bigness" for tired eyes. :-)

sed -e "s/^ $(hostname).novalocal/" /etc/hosts
2014-09-25 15:38:43
User: renoirb
Functions: sed

When booting a VM through OpenStack and managed through cloudinit, the hosts file gets to write a line simiar to ns0.novalocal ns0

This command proven useful while installing a configuration manager such as Salt Stack (or Puppet, or Ansible) and getting node name

zcat error.log.gz | sed 's^\[.*\]^^g' | sed 's^\, referer: [^\n]*^^g' | sort | uniq -c | sort -n
2014-09-24 05:26:24
User: zanhsieh
Functions: sed sort uniq zcat
Tags: sort sed uniq zcat

credit shall fall to this for non-gzipped version:


ifconfig | egrep -A2 "eth|wlan" | tr -d "\n"| sed 's/\-\-/\n/g'|awk '{print "mac: "$5 " " $7}' | sed 's/addr:/addr: /g'
ifconfig | head -n 2 | tr -d '\n' | sed -n 's/.*\(00:[^ ]*\).*\(adr:[^ ]*\).*/mac:\1 - \2/p'
YEAR=2015; ncal 7 $YEAR | sed -n 's/^Fr.* \([^ ]\+\) *$/Jul \1/p'
2014-08-17 11:04:02
User: andreasS
Functions: sed
Tags: sed date

Calculate the date of Sysadmin day (last Friday of July) of any given year

man -Pcat urxvt | \ # see description for full command
2014-07-16 23:14:14
User: malathion
Functions: man

man -Pcat urxvt | sed -n '/th: b/,/^B/p'|sed '$d'|sed '/^ \{7\}[a-z]/s/^ */^/g' | sed -e :a -e 'N;s/\n/@@/g;ta;P;D' | sed 's,\^\([^@]\+\)@*[\t ]*\([^\^]\+\),! \2\n! URxvt*\1\n\n,g' | sed 's,@@\( \+\),\n\1,g' | sed 's,@*$,,g' | sed '/^[^!]/d' | tr -d "'\`"

Prints a precompiled list of options to add to your Xresources.

Provided by Charles Torvalds here: http://www.askapache.com/linux/rxvt-xresources.html#Output_RXVT_Resources

echo -e '\x2Hello, folks\t!\r' | sed "y/\x2\x9\xD\x20/&#9218;&#9225;&#9229;&#9248;/"
2014-06-30 14:42:15
User: hermannsw
Functions: echo sed

Not better, but more lightweight (sed instead of perl).

Above command is reduced due to length restriction of less than 256 characters and entity encoding of "Save" command on this page. This is complete command (best without entity encoding):

echo -e '\x2Helo folks\t!\r' | sed "y/\x0\x1\x2\x3\x4\x5\x6\x7\x8\x9\xA\xB\xC\xD\xE\xF\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20/&#9216;&#9217;&#9218;&#9219;&#9220;&#9221;&#9222;&#9223;&#9224;&#9225;&#9226;&#9227;&#9228;&#9229;&#9230;&#9231;&#9232;&#9233;&#9234;&#9235;&#9236;&#9237;&#9238;&#9239;&#9240;&#9241;&#9242;&#9243;&#9244;&#9245;&#9246;&#9247;&#9248;/"


wget -q -O "quote" https://www.goodreads.com/quotes_of_the_day;notify-send "$(echo "Quote of the Day";cat quote | grep '&ldquo;\|/author/show' | sed -e 's/<[a-zA-Z\/][^>]*>//g' | sed 's/&ldquo;//g' | sed 's/&rdquo;//g')"; rm -f quote
2014-06-15 03:17:19
User: nowhereman88
Functions: rm wget

Just pulls a quote for each day and displays it in a notification bubble...

or you can change it a bit and just have it run in the terminal

wget -q -O "quote" https://www.goodreads.com/quotes_of_the_day;echo "Quote of the Day";cat quote | grep '&ldquo;\|/author/show' | sed -e 's/<[a-zA-Z\/][^>]*>//g' | sed 's/&ldquo;//g' | sed 's/&rdquo;//g'; rm -f quote
sed -i 's/^.*?].*?:\s//g' skype-chat-log.txt
2014-05-26 07:48:36
Functions: sed

Regular expression search pattern to remove the Datetime and Name when you paste from skype chat into your text editor

From this Gist:


echo thisIsATest | sed -E 's/([A-Z])/_\L\1/g'
sed -e 's/ .*//' -e 's/\.//' -e 's/^0*//' /proc/loadavg
2014-04-18 19:12:05
User: flatcap
Functions: sed

Show the current load of the CPU as a percentage.

Read the load from /proc/loadavg and convert it using sed:

Strip everything after the first whitespace:

sed -e 's/ .*//'

Delete the decimal point:

sed -e 's/\.//'

Remove leading zeroes:

sed -e 's/^0*//'
echo thisIsATest | sed -r 's/([A-Z])/_\L\1/g'
2014-04-11 13:36:08
User: flatcap
Functions: echo sed
Tags: sed

Convert a camelCase string into snake_case.

To complement senorpedro's command.