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 tagged sed from sorted by
Terminal - Commands tagged sed - 327 results
cmd=$(wget -qO- "http://www.m-w.com/dictionary/$(echo "$@"|tr '[A-Z]' '[a-z]')" | sed -rn "s#return au\('([^']+?)', '([^'])[^']*'\);.*#\nwget -qO- http://cougar.eb.com/soundc11/\2/\1 | aplay -q#; s/[^\n]*\n//p"); [ "$cmd" ] && eval "$cmd" || exit 1
2010-03-12 13:56:41
User: hackerb9
Functions: eval exit sed wget
3

Looks up a word on merriam-webster.com, does a screen scrape for the FIRST audio pronunciation and plays it.

USAGE: Put this one-liner into a shell script (e.g., ~/bin/pronounce) and run it from the command line giving it the word to say:

pronounce lek

If the word isn't found in merriam-webster, no audio is played and the script returns an error value. However, M-W is a fairly complete dictionary (better than howjsay.com which won't let you hear how to pronounce naughty words).

ASSUMPTIONS: GNU's sed (which supports -r for extended regular expressions) and Linux's aplay. Aplay can be replaced by any program that can play .WAV files from stdin.

KNOWN BUGS: only the FIRST pronunciation is played, which is problematic if you wanted a particular form (plural, adjectival, etc) of the word. For example, if you run this:

pronounce onomatopoetic

you'll hear a voice saying "onomatopoeia".

Playing the correct form of the word is possible, but doing so might make the screen scraper even more fragile than it already is. (The slightest change to the format of m-w.com could break it).

echo "ulimit `ulimit -a|sed -e 's/^.*\([a-z]\))\(.*\)$/-\1\2/'|tr "\n" ' '`"
2010-03-12 06:46:54
User: AskApache
Functions: echo
2

It is helpful to know the current limits placed on your account, and using this shortcut is a quick way to figuring out which values to change for optimization or security.

Alias is:

alias ulimith="command ulimit -a|sed 's/^.*\([a-z]\))\(.*\)$/-\1\2/;s/^/ulimit /'|tr '\n' ' ';echo"

Here's the result of this command:

ulimit -c 0 -d unlimited -e 0 -f unlimited -i 155648 -l 32 -m unlimited -n 8192 -p 8 -q 819200 -r 0 -s 10240 -t unlimited -u unlimited -v unlimited -x unlimited ulimit -a

core file size (blocks, -c) 0

data seg size (kbytes, -d) unlimited

scheduling priority (-e) 0

file size (blocks, -f) unlimited

pending signals (-i) 155648

max locked memory (kbytes, -l) 32

max memory size (kbytes, -m) unlimited

open files (-n) 8192

pipe size (512 bytes, -p) 8

POSIX message queues (bytes, -q) 819200

real-time priority (-r) 0

stack size (kbytes, -s) 10240

cpu time (seconds, -t) unlimited

max user processes (-u) unlimited

virtual memory (kbytes, -v) unlimited

file locks (-x) unlimited

grep current_state= /var/log/nagios/status.dat|sort|uniq -c|sed -e "s/[\t ]*\([0-9]*\).*current_state=\([0-9]*\)/\2:\1/"|tr "\n" " "
ls -d */* | sed -e 's/^/\"/g' -e 's/$/\"/g' | xargs mv -t $(pwd)
2010-03-01 23:43:26
User: leovailati
Functions: ls mv sed xargs
-1

You WILL have problems if the files have the same name.

Use cases: consolidate music library and unify photos (especially if your camera separates images by dates).

After running the command and verifying if there was no name issues, you can use

ls -d */ | sed -e 's/^/\"/g' -e 's/$/\"/g' | xargs rm -r

to remove now empty subdirectories.

find . -iname "FILENAME" -exec sed -i 's/SEARCH_STRING/REPLACE_STRING/g' {} \;
2010-02-24 19:52:22
User: nanopino
Functions: find sed
1

using find's exec option instead of a for loop and using sed's -i option for inplace replacement. no need to do the file swap.

sed -i -e 's/SEARCH_STRING/REPLACE_STRING/g' `find . -iname 'FILENAME'`
sed -i 's/oldname@example.com/newname@example.com/g' `grep oldname@example.com -rl .`
2010-02-18 18:26:09
User: and3k
Functions: sed
0

Do a recursive (-r) search with grep for all files where your old mail address is mentioned (-l shows only the file names) and use sed to replace it with your new address. Works with other search/replacement patterns too.

spellcheck(){ typeset y=$@;curl -sd "<spellrequest><text>$y</text></spellrequest>" https://www.google.com/tbproxy/spell|sed -n '/s="[0-9]"/{s/<[^>]*>/ /g;s/\t/ /g;s/ *\(.*\)/Suggestions: \1\n/g;p}'|tee >(grep -Eq '.*'||echo -e "OK");}
2010-02-17 08:20:48
User: eightmillion
Functions: echo grep sed tee
5

I took matthewbauer's cool one-liner and rewrote it as a shell function that returns all the suggestions or outputs "OK" if it doesn't find anything wrong. It should work on ksh, zsh, and bash. Users that don't have tee can leave that part off like this:

spellcheck(){ typeset y=$@;curl -sd "<spellrequest><text>$y</text></spellrequest>" https://google.com/tbproxy/spell|sed -n '/s="[1-9]"/{s/<[^>]*>/ /g;s/\t/ /g;s/ *\(.*\)/Suggestions: \1\n/g;p}';}
xmms2 list | sed -n -e '1i\0' -e 's/^.*(\([0-9]*\):\([0-9]*\))$/\1 60*\2++/gp' -e '$a\60op' | dc | sed -e 's/^ *//' -e 's/ /:/g'
xmms2 info $(xmms2 mlib search '<query>' | sed -ne 's/^00*\([1-9][0-9]*\).*$/\1/p') | awk -F' = ' '$1~/ url$/{print$2}'
sed 's/.*/ /'
2010-02-11 17:45:56
User: putnamhill
Functions: sed
Tags: sed
0

The first version printed:

tr: empty string2

The second version printed:

sed: -i may not be used with stdin

Maybe I misunderstood the orginal problem.

find . -type f |xargs -I% sed -i '/group name/s/>/ deleteMissing="true">/' %
2010-02-01 21:09:57
User: 4fthawaiian
Functions: find sed xargs
1

Changed out the for loop for an xargs. It's a tad shorter, and a tad cleaner.

for i in `find . -type f`; do sed -i '/group name/s/>/ deleteMissing="true">/' $i; done
2010-02-01 17:16:37
User: allrightname
Functions: sed
0

Recursively replace a string in files with lines matching string. Lines with the string "group name" will have the first > character replaced while other > characters on other lines will be ignored.

wget -q -U busybox -O- "http://www.google.com/search?ie=UTF8&q=define%3A$1" | tr '<' '\n' | sed -n 's/^li>\(.*\)/\1\n/p'
2010-02-01 13:01:47
User: hackerb9
Functions: sed tr wget
0

This is a minimalistic version of the ubiquitious Google definition screen scraper. This version was designed not only to run fast, but to work using BusyBox. BusyBox is a collection of basic Unix tools that have been compiled into a single binary to save space on tiny installations of Unix. For example, although my phone doesn't have perl or the GNU utilities, it does have BusyBox's stripped down versions of wget, tr, and sed. It turns out that those tools suffice for many tasks.

Known Bugs: This script does not handle HTML entities at all. I don't think there's an easy way to do that within BusyBox, but I'd love to see it if someone could do it. Also, this script can only define a single word, not phrases. (Well, you could if you typed in %20, but that'd be gross.) Lastly, this script does not show the URL where definitions were found. Given the randomness of the Net, that last bit of information is often key.

grep -rl oldstring . | parallel sed -i -e 's/oldstring/newstring/'
2010-01-28 08:44:16
Functions: grep sed
3

xargs deals badly with special characters (such as space, ' and "). To see the problem try this:

touch important_file

touch 'not important_file'

ls not* | xargs rm

Parallel https://savannah.nongnu.org/projects/parallel/ does not have this problem.

grep -E '^(cn|mail):' file.ldif | sed -e 's/^[a-z]*: //'
echo -e "swap=me\n1=2"|sed 's/\(.*\)=\(.*\)/\2=\1/g'
sed -i.bak 's/old/new/g' file
2010-01-06 17:04:05
User: deltaray
Functions: sed
Tags: bash sed
2

sed already has an option for editing files in place and making backup copies of the old file. -i will edit a file in place and if you give it an argument, it will make a backup file using that string as an extension.

command | sed '/regex/q'
2009-12-29 14:52:41
User: taliver
Functions: command sed
Tags: sed
1

Slightly simpler version of previous sed command that does the same thing. In this case, the output will stop at the command, and the entire command will be terminated as well, instead of proceeding through the whole file.

command | sed -n '1,/regex/p'
sed = <file> | sed 'N;s/\n/\t/'
2009-12-11 14:39:14
User: jgc
Functions: sed
Tags: sed
-1

Print out contents of file with line numbers.

This version will print a number for every line, and separates the numbering from the line with a tab.

grep -oP '"url":"\K[^"]+' $(ls -t ~/.mozilla/firefox/*/sessionstore.js | sed q)
2009-12-09 20:34:32
User: sputnick
Functions: grep ls sed
0

Require "grep -P" ( pcre ).

If you don't have grep -P, use that :

grep -Eo '"url":"[^"]+' $(ls -t ~/.mozilla/firefox/*/sessionstore.js | sed q) | cut -d'"' -f4
wget -q http://dynamic.xkcd.com/comic/random/ -O-| sed -n '/<img src="http:\/\/imgs.xkcd.com\/comics/{s/.*\(http:.*\)" t.*/\1/;p}' | awk '{system ("wget -q " $1 " -O- | display -title $(basename " $1") -write /tmp/$(basename " $1")");}'
2009-12-09 13:41:25
User: laugg
Functions: awk sed wget
Tags: sed awk wget comic
0

Only need to install Image Magick package.

Display a xkcd comic with its title and save it in /tmp directory

If you prefer to view the newest xkcd, use this command:

wget -q http://xkcd.com/ -O-| sed -n '/<img src="http:\/\/imgs.xkcd.com\/comics/{s/.*\(http:.*\)" t.*/\1/;p}' | awk '{system ("wget -q " $1 " -O- | display -title $(basename " $1") -write /tmp/$(basename " $1")");}'
find . -type f -exec sed -i s/oldstring/newstring/g {} +
2009-12-09 00:46:13
User: SlimG
Functions: find sed
Tags: sed find
14

This command find all files in the current dir and subdirs, and replace all occurances of "oldstring" in every file with "newstring".

printf $(echo -n $1 | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')
2009-11-25 04:27:39
User: infinull
Functions: echo printf sed
2

My version uses printf and command substitution ($()) instead of echo -e and xargs, this is a few less chars, but not real substantive difference.

Also supports lowercase hex letters and a backslash (\) will make it through unescaped