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 - 317 results
ps aux | grep $(echo $1 | sed "s/^\(.\)/[\1]/g")
2013-07-16 10:10:51
User: opexxx
Functions: echo grep ps sed
Tags: sed grep ps
1

grep по ps aux

find . -type d | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"
2013-07-16 10:08:34
User: opexxx
Functions: find sed
Tags: sed find
-4

show directory three

link=https://www.dropbox.com/login ; curl -b a -c cookie -d "t=$(curl -c a $link | sed -rn 's/.*TOKEN: "([^"]*).*/\1/p')&login_email=me%40yahoo.com&login_password=my_passwd" $link
2013-07-12 07:43:21
User: nixnax
Functions: link
1

Use the command line to log into Dropbox. You have to replace me@yahoo.com with your Dropbox email (note the URL-encoding of "@" as %40). Also replace my_passwd with your Dropbox password. (Note: special characters in your password (such as #) must be url-encoded. You will get a cookie (stored in file "cookie") that you can use for subsequent curl operations to dropbox, for example curl -b cookie https://www.dropbox.com/home. Debug note: If you want to see what data curl posts, use curl's --trace-ascii flag.

python -c "a='$(ps -u luke | grep Viber.exe)';b= a.split(' ')[1];import os;os.system('cpulimit -l 1 -p '+b)"
find . -type f -name filename.exe -exec sed -i "s/oldstring/oldstring/g" {} +;
sed -n '1!G;h;$p' techie.txt
pronounce(){ xidel "http://www.m-w.com/dictionary/$*" -f "replace(css('.au')[1]/@onclick,\".*'([^']+)', *'([^']+)'.*\", '/audio.php?file=\$1&word=\$2')" -f 'css("embed")[1]/@src' --download - | aplay -q;}
2013-04-18 13:03:16
User: BeniBela
Functions: aplay
0

Updated to the new version of the MW webpage (seems MW does not use cougar anymore, so the other commands do not work nowadays), and using Xidel to parse the page with a html parser instead regex.

Example usage:

pronounce onomatopoetic

I'm not sure how well Xidel works with binary streams (although it seems to work great in tests), so using wget to download the actual wav file might be safer, i.e.:

pronounce(){ wget -qO- $(xidel "http://www.m-w.com/dictionary/$*" -f "replace(css('.au')[1]/@onclick,\".*'([^']+)', *'([^']+)'.*\", '/audio.php?file=\$1&word=\$2')" -e 'css("embed")[1]/@src') | aplay -q;}

Xidel is not a standard cli tool and has to be downloaded from xidel.sourceforge.net

for i in `gpg --list-sigs | perl -ne 'if(/User ID not found/){s/^.+([a-fA-F0-9]{8}).*/\1/; print}' | sort | uniq`; do gpg --keyserver-options no-auto-key-retrieve --recv-keys $i; done
2013-03-10 09:15:15
User: hank
Functions: gpg perl sort
Tags: bash GPG sed fetch
0

The original command doesn't work for me - does something weird with sed (-r) and xargs (-i) with underscores all over...

This one works in OSX Lion. I haven't tested it anywhere else, but if you have bash, gpg and perl, it should work.

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

curl -s 'http://xkcd.com/rss.xml' | xpath '//item[1]/description/text()' 2>&1 | sed -n 's/.*title="\([^"]*\)".*/\1/p' | fold -s
2013-02-16 22:38:26
User: putnamhill
Functions: fold sed
Tags: sed curl fold xpath
0

I look at xkcd in my news reader, but it displays the image's title attribute only for a few seconds which makes reading the longer ones more challenging. So I use this to display it in my console.

find /path/ -type f -exec grep -l '<string of text>' {} \; | xargs sed -i -e 's%<string of text>%<new text string>%g'
sed -i 's/http:\/\/old\/new\///g' index.html
color()(set -o pipefail;"$@" 2>&1>&3|sed $'s,.*,\e[31m&\e[m,'>&2)3>&1
sed '/^\s*[^@%]/s=\([A-Z][A-Z]*\)\([^}A-Z]\|},$\)={\1}\2=g' literature.bib > output.bib
2013-01-15 22:24:17
User: michelsberg
Functions: sed
Tags: sed latex BibTeX
1

It is often recommended to enclose capital letters in a BibTeX file in braces, so the letters will not be transformed to lower case, when imported from LaTeX. This is an attempt to apply this rule to a BibTeX database file.

DO NOT USE sed '...' input.bib > input.bib as it will empty the file!

How it works:

/^\s*[^@%]/

Apply the search-and-replace rule to lines that start (^) with zero or more white spaces (\s*), followed by any character ([...]) that is *NOT* a "@" or a "%" (^@%).

s=<some stuff>=<other stuff>=g

Search (s) for some stuff and replace by other stuff. Do that globally (g) for all matches in each processed line.

\([A-Z][A-Z]*\)\([^}A-Z]\|},$\)

Matches at least one uppercase letter ([A-Z][A-Z]*) followed by a character that is EITHER not "}" and not a capital letter ([^}A-Z]) OR (|) it actually IS a "}", which is followed by "," at the end of the line ($).

Putting regular expressions in escaped parentheses (\( and \), respectively) allows to dereference the matched string later.

{\1}\2

Replace the matched string by "{", followed by part 1 of the matched string (\1), followed by "}", followed by the second part of the matched string (\2).

I tried this with GNU sed, only, version 4.2.1.

sed '/foo/ s/foo/foobar/g' <filename>
2013-01-02 08:52:44
User: totti
Functions: sed
Tags: sed file optimize
-6

Use optimized sed to big file/stream to reduce execution time

Use

sed '/foo/ s/foo/foobar/g' <filename>

insted of sed

's/foo/foobar/g' <filename>
for ARG in * ; do sudo -u USER 7z x -o"$(echo $ARG|sed 's/\(.*\)\..*/\1/')" "$ARG" ; done
2012-12-31 19:47:24
User: n158
Functions: sudo
0

Magic line will extract almost all possible archives from current folder in its own folders. Don't forget to change USER name in sudo command. sed is used to create names for folders from archive names w/o extension. You can test sed expression, used in this command:

arg='war.lan.net' ; x=$(echo $arg|sed 's/\(.*\)\..*/\1/') ; echo $x

If some archives can't be extracted, install packages:

apt-get install p7zip-full p7zip-rar

Hope this will save a lot of your time. Enjoy.

curl -s http://en.m.wikipedia.org/wiki/List_of_Internet_top-level_domains | sed -n '/<tr valign="top">/{s/<[^>]*>//g;p}'
2012-12-24 23:54:05
User: flatcap
Functions: sed
1

Quietly get a webpage from wikipedia: curl -s

By default, don't output anything: sed -n

Search for interesting lines: /<tr valign="top">/

With the matching lines: {}

Search and replace any html tags: s/<[^>]*>//g

Finally print the result: p

replace old new -- `find -type f`
2012-12-13 20:22:17
User: brian
Tags: sed find
1

Search and replace recursively. :-) Shorter and simpler than the others. And allows more terms:

replace old new [old new ...] -- `find -type f`

sed -r "s@\^\[{1,2}([0-9]{1,2}(;[0-9]{1,2})?(;[0-9])?(m|K){1,2})?@@g"
2012-11-29 18:42:32
User: malathion
Functions: sed
0

Catches some background colors missed by the highest rated alternative.

systemd-analyze plot | curl -sF file1=@- http://ompldr.org/upload | grep -P -o "(?<=File:).*(http://ompldr.org/.*)\<\/a\>" | sed -r 's@.*(http://ompldr.org/\w{1,7}).*@\1@'
cat dump.sql | sed -n -e '/Table structure for table .table1./,/Table structure for table .table2./p'
2012-11-22 23:54:04
User: infojunkie
Functions: cat sed
Tags: mysql sed
-1

Given a dump.sql file, extract table1 creation and data commands. table2 is the one following table1 in the dump file. You can also use the same idea to extract several consecutive tables.

curl -qsL http://checkip.dyn.com | sed -E "s/^.*Address: ([0-9\.]+).*$/\1/"
2012-10-28 22:20:50
Functions: sed
Tags: sed curl ipv4
0

Retrieves the current WAN ipv4 address via checkip.dyn.com.

git for-each-ref --sort='-authordate' --format='%(refname)%09%(authordate)' refs/heads | sed -e 's-refs/heads/--'
source <(egrep '^#define EX_.*' /usr/include/sysexits.h | sed -e 's/#define/declare -r/g' | sed 's/\//#/g' | sed -e 's/\s\{1,\}/ /g' | sed -e 's/ \([0-9]\)/\=\1/'g )
2012-09-21 13:30:37
User: fishdaemon
Functions: egrep sed
-1

Use meaningful exit codes

change "source" to "cat" to view output instead of assigning

sed 1d foo.txt