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 using sed from sorted by
Terminal - Commands using sed - 1,140 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 :-)

find <mydir> -type f -exec sed -i 's/<string1>/<string2>/g' {} \;
ls -1 | while read a; do mv "$a" `echo $a | sed -e 's/\ //g'`; done
echo ${IP} | sed "s/[0-9\.]//g"
2015-10-19 18:20:03
User: andregyn62
Functions: echo sed

This command validates if exist any character different in 0-255 and dot.

If any characters different is typed the error menssage is showing.

adb shell screencap -p | sed 's/\r$//' > FILENAME.PNG
curl -q -s http://www.hasthelhcdestroyedtheearth.com/ | sed -En '/span/s/.*>(.*)<.*/\1/p'
2015-09-23 23:26:30
User: Tatsh
Functions: sed

Use -q as first argument (as described in `man curl`) to ignore curlrc to ensure the output is always the same regardless of user's configuration.

curl -s http://www.hasthelhcdestroyedtheearth.com/ | sed -En '/span/s/.*>(.*)<.*/\1/p'
2015-09-23 12:30:31
User: BeniBela
Functions: sed

This says if the LHC has destroyed the world. Run it in a loop to monitor the state of Earth. Might not work reliable, if the world has actually been destroyed.

weather() { curl -s "http://www.wunderground.com/q/zmw:$1.1.99999" | grep "og:title" | cut -d\" -f4 | sed 's/&deg;/ degrees F/'; }
pidstat -t | sed 's/,/./4' | awk -v seuil='10.0' '{if (NR>3 && $8>seuil) print }'
last|grep `whoami`|grep -v logged|cut -c61-71|sed -e 's/[()]//g'|awk '{ sub("\\+", ":");split($1,a,":");if(a[3]){print a[1]*60*60+a[2]*60+a[3]} else {print a[1]*60+a[2] }; }'|paste -s -d+ -|bc|awk '{printf "%dh:%dm:%ds\n",$1/(60*60),$1%(60*60)/60,$1%60}'
2015-09-19 03:02:43
User: donjuanica
Functions: awk cut grep last paste sed

Add -n to last command to restrict to last num logins, otherwise it will pull all available history.

sudo apt-get remove --purge $(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d')
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'
curl $1 | grep -E "http.*\.mp3" | sed "s/.*\(http.*\.mp3\).*/\1/" | xargs wget
2015-09-17 13:19:53
User: theodric
Functions: grep sed xargs

The difference between the original version provided and this one is that this one works rather than outputting a wget error

sed -e "s/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/"
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

sed -i.$(date +%F@%T) 's/^LogLevel warn/LogLevel debug/g' httpd.conf
2015-07-22 14:47:26
User: zlemini
Functions: date sed

httpd.conf httpd.conf.2015-07-22@14:43:20

diff a.txt b.txt | grep -E '^(<|>)' | sed 's:^< \(.*\):<del style="color\:red; text-decoration\: none">- \1</del><br>:' | sed 's:^> \(.*\):<ins style="color\:green; text-decoration\: none">+ \1</ins><br>:'
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"

find /PATHNAME -type l | while read nullsymlink ; do wrongpath=$(readlink "$nullsymlink") ; right=$(echo "$wrongpath" | sed s'|OLD_STRING|NEW_STRING|') ; ln -fs "$right" "$nullsymlink" ; done
2015-04-14 14:58:41
User: iDudo
Functions: echo find ln read readlink sed

After you run this script, you can check status for broken symlink with this command:

find -L . -type l

curl -s http://host.net/url.html | grep magnet | sed -r 's/.*(magnet:[^"]*).*/\1/g'
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).

grep -xFf <(groups user1|cut -f3- -d\ |sed 's/ /\n/g') <(groups user2|cut -f3- -d\ |sed 's/ /\n/g')