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,071 results
ls -1 | while read file; do new_file=$(echo $file | sed s/\ /_/g); mv "$file" "$new_file"; done
find -name '*.mp3' -exec mp3info {} -p "%F: %r kbps\n" \; | sort | sed '/320 kbps/d'
find . -type f -name filename.exe -exec sed -i "s/oldstring/oldstring/g" {} +;
curl -s http://www.reddit.com/user/$USER/about.json | tr "," "\n" | grep "link_karma" | tr ": " "\n" | grep -E "[0-9]+" | sed s/"^"/"Link Karma: "/
2013-05-13 15:36:59
User: iiAtlas
Functions: grep sed tr
Tags: curl
0

Replace $USER with the username of the Reddit user in question. To get comment karma instead run...

curl -s http://www.reddit.com/user/$USER/about.json | tr "," "\n" | grep "comment_karma" | tr ": " "\n" | grep -E "[0-9]+" | sed s/"^"/"Comment Karma: "/
cat domainlist.txt | while read line; do echo -ne $line; whois $line | grep Expiration ; done | sed 's:Expiration Date::'
sed -n '1!G;h;$p' techie.txt
pathrm() { PATH=`echo $PATH | sed -re 's#(:|^)cde($|:)#:#g;s#^:##g;s#:$##g'`; }
sudo sed -i 's/Exec=skype %U/Exec=skype --disable-cleanlooks -style GTK %U/' /usr/share/applications/skype.desktop
2013-04-17 08:43:00
User: FadeMind
Functions: sed sudo
0

Replace default command in skype.desktop file.

sudo sed -i 's/raspberrypi/pita1/' /etc/hosts /etc/hostname; sudo reboot
2013-04-16 07:27:41
User: egeoffray
Functions: sed sudo
0

Your computer's name is raspberrypi and you want to rename it to pita1.

This command will change both the hostname and the name used for netwrk communications.

uniq -c | sed -r 's/([0-9]+)\s(.*)/"\2": \1,/;$s/,/\n}/;1i{'
rm index.html | wget www.google.com;cat index.html | sed 's/<script>/\n\n\<script>\n\n/g' | sed 's/<\/script>/>\n\n/g'
2013-04-10 04:05:30
User: lbhack
Functions: cat rm sed wget
0

remove old index.html if you download it again and organiaz the java script tag on the file index.html

function google { Q="$@"; GOOG_URL='https://www.google.de/search?tbs=li:1&q='; AGENT="Mozilla/4.0"; stream=$(curl -A "$AGENT" -skLm 10 "${GOOG_URL}${Q//\ /+}" | grep -oP '\/url\?q=.+?&amp' | sed 's|/url?q=||; s|&amp||'); echo -e "${stream//\%/\x}"; }
2013-04-05 08:04:15
User: michelsberg
Functions: echo grep sed
Tags: google
12

Put it in your ~/.bashrc

usage:

google word1 word2 word3...

google '"this search gets quoted"'

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
Q="YOURSEARCH"; GOOG_URL="http://www.google.com/search?q="; AGENT="Mozilla/4.0"; stream=$(curl -A "$AGENT" -skLm 10 "${GOOG_URL}\"${Q/\ /+}\"" | grep -oP '\/url\?q=.+?&amp' | sed 's/\/url?q=//;s/&amp//'); echo -e "${stream//\%/\x}"
2013-04-03 09:56:41
User: techie
Functions: echo grep sed
Tags: google
8

I found this command on a different site and thought you guy might enjoy it. Just change "YOURSEARCH" to what ever you want to search. Example, "Linux Commands"

sed ?s/[sub_str]/[sub_str]\n/g? [text_file] | wc -l
sed "s,\x1B\[[0-9;]*[a-zA-Z],,g"
2013-03-18 14:49:30
User: Zhoul
Functions: sed
1

Remove ( color / special / escape / ANSI ) codes, from text, with sed

Credit to the original folks who I've copied this command from.

The diff here is:

Theirs: [m|K]

Theirs is supposed to remove \E[NUMBERS;NUMBERS[m OR K]

This statement is incorrect in 2 ways.

1. The letters m and K are two of more than 20+ possible letters that can end these sequences.

2. Inside []'s , OR is already assumed, so they are also looking for sequences ending with | which is not correct.

This : [a-zA-Z]

This resolves the "OR" issue noted above, and takes care of all sequences, as they all end with a lower or upper cased letter.

This ensures 100% of any escape code 'mess' is removed.

sed '/^$/d'
sed "s/\b\(.\)/\u\1/g"
wget -O - "[PICASA ALBUM RSS LINK]" |sed 's/</\n</g' | grep media:content |sed 's/.*url='"'"'\([^'"'"']*\)'"'"'.*$/\1/' |awk -F'/' '{gsub($NF,"d/"$NF); print $0}'|wget -i -
lynx --dump http://en.trending-topic.com/countries/Mexico/ | grep "62]#" | sed 's/\[62\]//g'
2013-03-12 16:25:14
User: xmuda
Functions: grep sed
0

In these command i use lynx to get the top trend topic of Mexico, if you replace Mexico with other country, you will get the #1 Trending topic

cat /var/lib/dpkg/info/*.md5sums|grep usr/sbin/sshd|sed 's,usr,/usr,'|md5sum -c
2013-03-12 11:20:48
User: Ztyx
Functions: cat grep md5sum sed
0

Replace "user/sbin/sshd" with the file you would like to check. If you are doing this due to intrusion, you obviously would want to check size, last modification date and md5 of the md5sum application itself. Also, note that "/var/lib/dpkg/info/*.md5sums" files might have been tampered with themselves. Neither to say, this is a useful command.

read -p 'Script: ' S && C=$S.crypt H='eval "$((dd if=$0 bs=1 skip=//|gpg -d)2>/dev/null)"; exit;' && gpg -c<$S|cat >$C <(echo $H|sed s://:$(echo "$H"|wc -c):) - <(chmod +x $C)
2013-03-10 08:59:45
User: rodolfoap
Functions: cat chmod echo gpg read sed wc
5

(Please see sample output for usage)

Use any script name (the read command gets it) and it will be encrypted with the extension .crypt, i.e.:

myscript --> myscript.crypt

You can execute myscript.crypt only if you know the password. If you die, your script dies with you.

If you modify the startup line, be careful with the offset calculation of the crypted block (the XX string).

Not difficult to make script editable (an offset-dd piped to a gpg -d piped to a vim - piped to a gpg -c directed to script.new ), but not enough space to do it on a one liner.

Sorry for the chmod on parentheses, I dont like "-" at the end.

Thanks flatcap for the subshell abbreviation to /dev/null

echo "eval \"\$(dd if=\$0 bs=1 skip=XX 2>/dev/null|gpg -d 2>/dev/null)\"; exit" > script.secure; sed -i s:XX:$(stat -c%s script.secure): script.secure; gpg -c < script.bash >> script.secure; chmod +x script.secure
2013-03-09 11:16:48
User: rodolfoap
Functions: chmod echo gpg sed stat
6

(Please see sample output for usage)

script.bash is your script, which will be crypted to script.secure

script.bash --> script.secure

You can execute script.secure only if you know the password. If you die, your script dies with you.

If you modify the startup line, be careful with the offset calculation of the crypted block (the XX string).

Not difficult to make script editable (an offset-dd piped to a gpg -d piped to a vim - piped to a gpg -c directed to script.new ), but not enough space to do it on a one liner.

sed -i.awkbak "s/\([:,]\)oldspiderman/\1newspiderman/" /etc/group
2013-03-01 23:48:20
User: iPaulo
Functions: sed
Tags: sed groups
0

Assuming a convention looking group file, this command will eject oldspiderman from the leagueofsuperfriends group and add newspiderman:

oldspiderman:x:551:

aquaman:x:552:

superman:x:553:

newspiderman:x:554:

leagueofsuperfriends:x:1000:superman,oldspiderman,superman,aquaman

sed -i -n '/%/p' *.php