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,078 results
for f in $(ls *.xml.skippy); do mv $f `echo $f | sed 's|.skippy||'`; done
2009-11-19 21:36:26
User: argherna
Functions: ls mv sed
Tags: sed ls mv for
-2

For this example, all files in the current directory that end in '.xml.skippy' will have the '.skippy' removed from their names.

mkpasswd() { head -c $(($1)) /dev/urandom | uuencode - | sed -n 's/.//;2s/\(.\{'$1'\}\).*/\1/p' ;}
2009-11-19 14:27:52
User: taliver
Functions: head sed uuencode
-3

This uses urandom to produce a random password. The random values are uuencoded to ensure only printable characters. This only works for a number of characters between 1 and 60.

modinfo $(cut -d' ' -f1 /proc/modules) | sed '/^dep/s/$/\n/; /^file\|^desc\|^dep/!d'
2009-11-18 23:40:46
User: sputnick
Functions: cut modinfo sed
6

I modify 4077 and marssi commandline to simplify it and skip an error when parsing the first line of lsmod (4077). Also, it's more concise and small now. I skip using xargs ( not required here ). This is only for GNU sed.

For thoses without GNU sed, use that :

modinfo $(lsmod | awk 'NR>1 {print $1}') | sed -e '/^dep/s/$/\n/g' -e '/^file/b' -e '/^desc/b' -e '/^dep/b' -e d
lsmod | sed -e '1d' -e 's/\(\([^ ]*\) \)\{1\}.*/\2/' | xargs modinfo | sed -e '/^dep/s/$/\n/g' -e '/^file/b' -e '/^desc/b' -e '/^dep/b' -e d
2009-11-17 22:51:08
User: marssi
Functions: lsmod modinfo sed xargs
1

Liked command 4077 so I improved it, by doing all text manipulation with sed.

"Run this as root, it will be helpful to quickly get information about the loaded kernel modules." THX mohan43u

lsmod | cut -d' ' -f1 | xargs modinfo | egrep '^file|^desc|^dep' | sed -e'/^dep/s/$/\n/g'
2009-11-17 02:13:34
User: mohan43u
2

Run this as root, it will be helpful to quickly get information about the loaded kernel modules.

geo(){ curl -s "http://www.geody.com/geoip.php?ip=$(dig +short $1)"| sed '/^IP:/!d;s/<[^>][^>]*>//g'; }
2009-11-12 17:14:09
User: dennisw
Functions: sed
1

A function that takes a domain name as an argument

sed -e '/^[[:blank:]]*#/d; s/[[:blank:]][[:blank:]]*#.*//' -e '/^$/d' -e '/^\/\/.*/d' -e '/^\/\*/d;/^ \* /d;/^ \*\//d' /a/file/with/comments
2009-11-10 17:47:22
Functions: sed
Tags: sed PHP
-3

Strips comments from at least bash and php scripts. Normal # and // as well as php block comments

removes all of the:

empty/blank lines

lines beginning with #

lines beginning with //

lines beginning with /*

lines beginning with a space and then *

lines beginning with */

It also deletes the lines if there's whitespace before any of the above.

Add an alias to use in .bashrc like this:

alias stripcomments="sed -e '/^[[:blank:]]*#/d; s/[[:blank:]][[:blank:]]*#.*//' -e '/^$/d' -e '/^\/\/.*/d' -e '/^\/\*/d;/^ \* /d;/^ \*\//d'"
find -name '*.avi' | while read i ; do echo $(mplayer -identify -frames 0 -vo null -nosound "$i" 2>&1 | grep ID_LENGTH | cut -d= -f2)" ""$i" ;done | sort -k1 -r -n | sed 's/^\([^\ ]*\)\ \(.*\)$/\2:\1/g'
2009-11-09 17:14:59
User: ZungBang
Functions: cut echo find grep read sed sort
2

handles file names with spaces and colons, fixes sort (numeric!), uses mplayer, same output format as other alternatives

sed -i '/myexpression/d' /path/to/file.txt
2009-11-09 11:40:45
User: jgc
Functions: sed
Tags: sed regex
9

The -i option in sed allows in-place editing of the input file.

Replace myexpression with any regular expression.

/expr/d syntax means if the expression matches then delete the line.

You can reverse the functionality to keep matching lines only by using:

sed -i -n '/myexpression/p' /path/to/file.txt
mpg123 `curl -s http://blip.fm/all | sed -e 's#"#\n#g' | grep mp3$ | xargs`
wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
sed -n 's/^model name[ \t]*: *//p' /proc/cpuinfo
2009-11-05 10:59:31
User: jgc
Functions: sed
Tags: sed cpuinfo
0

Alternative command to retrieve the CPU model name and strip off the "model name : " labels.

lynx --dump http://ip.boa.nu|sed -e 's/^[[:space:]]*//' -e 's/*[[:space:]]$//'|grep -v ^$
2009-11-04 22:23:45
User: xeor
Functions: grep sed
-5

For those of us that still uses lynx :)

eval $(curl -s http://www.commandlinefu.com/commands/matching/external/ZXh0ZXJuYWw=/sort-by-votes/plaintext|sed -n '/^# Get your external IP address$/{n;p;q}')
2009-11-04 16:58:31
User: jgc
Functions: eval sed
-1

This command uses the top voted "Get your external IP" command from commandlinefu.com to get your external IP address.

Use this and you will always be using the communities favourite command.

This is a tongue-in-cheek entry and not recommended for actual usage.

IFS=$'\n';cl=($(curl -s http://www.commandlinefu.com/commands/matching/external/ZXh0ZXJuYWw=/sort-by-votes/plaintext|sed -n '/^# Get your external IP address$/{n;p}'));c=${cl[$(( $RANDOM % ${#cl[@]} ))]};eval $c;echo "Command used: $c"
2009-11-04 16:55:44
User: jgc
Functions: sed
3

There's been so many ways submitted to get your external IP address that I decided we all need a command that will just go pick a random one from the list and run it. This gets a list of "Get your external IP" commands from commanlinefu.com and selects a random one to run. It will run the command and print out which command it used.

This is not a serious entry, but it was a learning exercise for me writing it. My personal favourite is "curl icanhazip.com". I really don't think we need any other ways to do this, but if more come you can make use of them with this command ;o).

Here's a more useful command that always gets the top voted "External IP" command, but it's not so much fun:

eval $(curl -s http://www.commandlinefu.com/commands/matching/external/ZXh0ZXJuYWw=/sort-by-votes/plaintext|sed -n '/^# Get your external IP address$/{n;p;q}')
curl -s "http://www.geody.com/geoip.php?ip=$(curl -s icanhazip.com)" | sed '/^IP:/!d;s/<[^>][^>]*>//g'
2009-11-04 07:15:02
User: getkaizer
Functions: sed
Tags: sed curl
11

Not my script. Belongs to mathewbauer. Used without his permission.

This script gives a single line as shown in the sample output.

NOTE: I have blanked out the IP address for obvious security reasons. But you will get whatever is your IP if you run the script.

Tested working in bash.

ifconfig eth1 | grep inet\ addr | awk '{print $2}' | cut -d: -f2 | sed s/^/eth1:\ /g
2009-11-03 19:26:40
User: TuxOtaku
Functions: awk cut grep ifconfig sed
2

Sometimes, you don't really care about all the other information that ifconfig spits at you (however useful it may otherwise be). You just want an IP. This strips out all the crap and gives you exactly what you want.

sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g
2009-11-03 00:34:06
User: vaejovis
Functions: sed
4

Removes ANSI color and end of line codes to the [{attr1};...;{attrn}m format.

mpc play $(sed -n "s@^[ >]\([0-9]\+\)) $(mpc playlist|cut -d' ' -f3-|dmenu -i -p 'song name'||echo void)@\1@p" < <(mpc playlist))
2009-10-30 16:22:10
User: gibboris
Functions: sed
1

Add a [fluxbox] binding in your key file then this

command provides a dmenu selector for the next track to play

mysql -u <user> --password=<password> -e "SHOW COLUMNS FROM <table>" <database> | awk '{print $1}' | tr "\n" "," | sed 's/,$//g'
2009-10-29 13:42:17
User: maxmanders
Functions: awk sed tr
-1

Useful when you need to write e.g. an INSERT for a table with a large number of columns. This command will retrieve the column names and comma-separate them ready for INSERT INTO(...), removing the last comma.

getdji (){local url sedcmd;url='http://finance.yahoo.com/q?d=t&s=^DJI';sedcmd='/(DJI:.*)/,/Day.*/!d;s/^ *//g;';sedcmd="$sedcmd/Change:/s/Down / -/;/Change:/s/Up / +/;";sedcmd="$sedcmd/Open:/s//& /";lynx -dump "$url" | sed "$sedcmd"; }
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 //'