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,082 results
git ls-files | while read i; do git blame $i | sed -e 's/^[^(]*(//' -e 's/^\([^[:digit:]]*\)[[:space:]]\+[[:digit:]].*/\1/'; done | sort | uniq -ic | sort -nr
2009-10-25 09:40:01
User: pipping
Functions: read sed sort uniq
Tags: statistics git
-1

You'll run into trouble if you have files w/ missing newlines at the end. I tried to use

PAGER='sed \$q' git blame

and even

PAGER='sed \$q' git -p blame

to force a newline at the end, but as soon as the output is redirected, git seems to ignore the pager.

sed -n '/test/{/error\|critical\|warning/d;p}' somefile
2009-10-25 09:07:00
User: pipping
Functions: sed
2

That's what the sed command should've been, sorry.

sed -n /start_pattern/,/stop_pattern/p file.txt
declare -F | sed 's/^declare -f //'
sed '/test/{/error\|critical\|warning/d}' somefile
2009-10-24 15:59:59
User: pipping
Functions: sed
3

If you wanted to do all in one command, you could go w/ sed instead

pushd .> /dev/null; cd /; for d in `echo $OLDPWD | sed -e 's/\// /g'`; do cd $d; echo -n "$d "; ls -ld .; done; popd >/dev/null
2009-10-22 12:32:11
User: syladmin
Functions: cd echo ls sed
Tags: permissions
0

Can easily be scripted in order to show permission "tree" from any folder. Can also be formated with

column -t

{ pushd .> /dev/null; cd /; for d in `echo $OLDPWD | sed -e 's/\// /g'`; do cd $d; echo -n "$d "; ls -ld .; done; popd >/dev/null ; } | column -t

from http://www.commandlinefu.com/commands/view/3731/using-column-to-format-a-directory-listing

sed -ne '/^Package: \(.*\)/{s//\1/;h;};/^Installed-Size: \(.*\)/{s//\1/;G;s/\n/ /;p;}' /var/lib/dpkg/status | sort -rn
2009-10-19 19:01:17
User: arcege
Functions: sed sort
1

Use the hold space to preserve lines until data is needed.

uuencode -m $1 /dev/stdout | sed '1d' | sed '$d' | tr -d '\n' | xclip -selection clipboard
2009-10-19 09:03:09
User: caliburning
Functions: sed tr uuencode
Tags: xclip uuencode
1

I use it for embedding images in CSS for Stylish, the Firefox addon.

Thought it might be useful to others.

sed -n '/^function h\(\)/,/^}/p' script.sh
2009-10-19 07:55:35
User: haivu
Functions: sed
Tags: bash sed
8

I often need to extract a function from a bash script and this command will do it.

geoip(){curl -s "http://www.geody.com/geoip.php?ip=${1}" | sed '/^IP:/!d;s/<[^>][^>]*>//g' ;}
weather() { lynx -dump "http://mobile.weather.gov/port_zh.php?inputstring=$*" | sed 's/^ *//;/ror has occ/q;2h;/__/!{x;s/\n.*//;x;H;d};x;s/\n/ -- /;q';}
echo -n a | od -d | sed -n "s/^.* //gp"
2009-10-17 15:46:00
User: stf42
Functions: echo od sed
1

the shortest command for character 'a' i know

x=1 ; while [ $x -le 10 ] ; do lynx -dump http://www.alexa.com/siteinfo/http://[YOUR WEBSITE] | grep Global | sed 's/ \|Global\|\,//g' >> /var/log/alexa-stats.txt ; sleep 5h ; done &
2009-10-17 13:48:05
User: felix001
Functions: grep sed sleep
0

This will record the Alexa Traffic Stats to a file and run every 5 hours.

-- www.fir3net.com --

for i in ~/Desktop/Personal/Wallpapers/*.jpg ; { size=$((`identify -format "%wx%h" $i | sed 's/x/*/'`)) ; if [[ $size -lt 800001 ]] then ; rm -f "$i" ; fi; }
2009-10-16 00:21:21
User: cbrinker
Functions: rm sed
0

For all of the jpgs in a directory, determine their size and if below a threshold remove them forcefully.

seq -w 100 | sed 's/^/login/'
2009-10-15 13:56:56
User: lawrence
Functions: sed seq
-4

This command generates a sequential login list. Good to be used as a source of new logins.

sed -n 5p <file>
2009-10-15 11:00:48
User: Waldirio
Functions: sed
37

You can get one specific line during any procedure. Very interesting to be used when you know what line you want.

seq 4|xargs -n1 -i bash -c "echo -n 164.85.216.{} - ; nslookup 164.85.216.{} |grep name"|tr -s ' ' ' '|awk '{print $1" - "$5}'|sed 's/.$//'
sed -e "$ ! s/$/,/"
2009-10-13 10:13:52
User: jgc
Functions: sed
4

In this simple example the command will add a comma to the end of every line except the last. I found this really useful when programatically constructing sql scripts. See sample output for example.

dpkg --get-selections | cut -f1 | while read pkg; do dpkg -L $pkg | xargs -I'{}' bash -c 'if [ ! -d "{}" ]; then echo "{}"; fi' | tr '\n' '\000' | du -c --files0-from - | tail -1 | sed "s/total/$pkg/"; done
2009-10-12 14:57:54
User: pykler
Functions: bash cut du echo read sed tail tr xargs
Tags: Debian wajig
4

Calculates the size on disk for each package installed on the filesystem (or removed but not purged). This is missing the

| sort -rn

which would put the biggest packges on top. That was purposely left out as the command is slightly on the slow side

Also you may need to run this as root as some files can only be checked by du if you can read them ;)

for f in *.html; do sed '$d' -i "$f"; done
2009-10-12 14:46:43
User: alperyilmaz
Functions: sed
1

sed can be used deleting the last line and with -i option, there's no need to for temp files, the change is made on the actual file

ifs () { echo -n "${IFS}"|hexdump -e '"" 10/1 "'\''%_c'\''\t" "\n"' -e '"" 10/1 "0x%02x\t" "\n\n"'|sed "s/''\|\t0x[^0-9]//g; $,/^$/d"
2009-10-10 22:41:35
User: dennisw
Functions: echo hexdump sed
2

You can display, save and restore the value of $IFS using conventional Bash commands, but these functions, which you can add to your ~/.bashrc file make it really easy.

To display $IFS use the function ifs shown above. In the sample output, you can see that it displays the characters and their hexadecimal equivalent.

This function saves it in a variable called $saveIFS:

sifs () { saveIFS=$IFS; }

Use this function to restore it

rifs () { IFS=$saveIFS; }

Add this line in your ~/.bashrc file to save a readonly copy of $IFS:

declare -r roIFS=$IFS

Use this function to restore that one to $IFS

rrifs () { IFS=$roIFS; }
find . -type d -print | sed -e 's;[^/]*/;..........;g'|awk '{print $0"-("NR-1")"}'
(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \ ; ls -l | sed 1d) | column -t
head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'
sed -i '19375 s/^/#/' file
2009-10-07 17:50:40
User: TuxOtaku
Functions: sed
5

This will comment out a line, specified by line number, in a given file.