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 xml from sorted by
Terminal - Commands tagged xml - 21 results
wget -q -O- http://example-podcast-feed.com/rss | grep -o "<enclosure[ -~][^>]*" | grep -o "http://[ -~][^\"]*" | xargs wget -c
2013-09-24 12:38:08
User: talha131
Functions: grep wget xargs
0

This script can be used to download enclosed files from a RSS feed. For example, it can be used to download mp3 files from a podcasts RSS feed.

curl ${URL} 2>/dev/null|grep "<${BLOCK}>"|sed -e "s/.*\<${BLOCK}\>\(.*\)\<\/${BLOCK}\>.*/\1/g"
2013-08-31 14:53:54
User: c3w
Functions: grep sed
0

set BLOCK to "title" or any other HTML / RSS / XML tag and curl URL to get everything in-between e.g. some text

unzip document.odt content.xml && xmlindent -w content.xml && nano content.xml
2012-12-01 17:05:28
User: arthurdent
0

OpenDocument documents from OpenOffice.org,LibreOffice

and other applications, are actually ZIP archives.

Useful informations in these archives are in XML format.

Here we like it or do not. Anyway, the XML files have the unfortunate tendency to

not be indented, and for good reason: they consist of only one line!

To solve the problem and to use a proper editor on the content,

I proceed as follows.

Required xmlindent

You can also use :

zip document.odt content.xml

And it works with vi instead of nano !

xmllint --format --xmlout --nsclean <file>
2012-11-27 06:13:23
User: seb1245
1

Like `tidy`, `xmllint` can be used to prettify XML files.

The --nsclean option is also useful to remove redundant namespaces.

xsltproc --stringparam name value <xsl_stylesheet> <xml_document>
2012-11-09 15:54:46
User: seb1245
Tags: xml XSLT
0

The XML document can be transformed to text, XML, HTML or anything else. The --stringparam option allows to set XSL variables externally.

tidy -i -xml <inputfile>
2012-11-03 18:10:58
User: Testuser_01
8

This will indent the input to be more readable. Warnings and messages are not send to STDOUT so you can just use a pipe to create the formatted outputfile, like:

tidy -i -xml in.xml > out.xml
echo '<foo><bar/></foo>' | xmllint --format -
xpath () { xmllint --format --shell "$2" <<< "cat $1" | sed '/^\/ >/d' }
2011-12-20 08:34:11
User: sharfah
Functions: sed
Tags: xml xmllint xpath
-1

This function uses xmllint to evaluate xpaths.

Usage: xpath /path/to/element XMLfile

xml2 < file.xml | grep ^/path/to/element | cut -f2- -d=
2011-12-19 18:51:17
User: bandie91
Functions: cut grep
Tags: cut xml
2

poor man's xml parser :)

diffxml() { diff -wb <(xmllint --format "$1") <(xmllint --format "$2"); }
2011-10-06 07:36:13
User: sharfah
Functions: diff
Tags: diff xml xmllint
13

Diffs two xml files by formatting them first using xmllint and then invoking diff.

Usage: diffxml XMLFile1 XMLFile2

xpath () { xmllint --format --shell "$2" <<< "cat $1" | sed '/^\/ >/d' }
2011-10-05 07:45:16
User: sharfah
Functions: sed
0

This function uses xmllint to evaluate xpaths.

Usage: xpath /some/xpath XMLfile

awk "/<xsd:annotation>/{h=1};!h;/<\/xsd:annotation>/{h=0}" annotatedSchema.xsd
2011-07-15 07:17:17
User: 2chg
Functions: awk
Tags: xml filter
-1

Removes all lines between the lines containing "" and "", including these lines itself

Backdrop:

Sometimes when working with XML files without an graphical editor, large comment-/annotation-blocks taper the readability to walk through the file. I like to create a copy of such documents without these annotations. As the documentation itself is in documentation tags inside the annotation tags an therefore graphical editors tend to put the annotation tags in their own lines, this command removes all documentations within annotation-tags.

find -type f -name "*.xml" -exec xmllint --noout {} \;
2011-01-25 18:26:57
User: bradbeattie
Functions: find
Tags: xml xmllint
1

If everything validates, there's no output. Can be handy to run on a cron job set up to email output.

atomtitles () { curl --silent $1 | xmlstarlet sel -N atom="http://www.w3.org/2005/Atom" -t -m /atom:feed/atom:entry -v atom:title -n}
2010-12-15 11:03:31
User: Seebi
0

this simply curls the feed and runs a xpath query on it ...

wget -O - 'https://USERNAMEHERE:PASSWORDHERE@mail.google.com/mail/feed/atom' --no-check-certificate
2010-09-26 14:47:13
User: PLA
Functions: wget
-3

I use this command in my Conky script to display the number of messages in my Gmail inbox and to list the from: and subject: fields.

curl -L -s `curl -s [http://podcast.com/show.rss]` | xmlstarlet sel -t -m "//enclosure[1]" -v "@url" -n | head -n 1` | ssh -t [user]@[host] "mpg123 -"
2010-07-31 00:17:47
User: denzuko
Functions: head ssh
0

Gets the latest podcast show from from your favorite Podcast. Uses curl and xmlstarlet.

Make sure you change out the items between brackets.

curl -L -s `curl -s http://www.2600.com/oth-broadband.xml` | xmlstarlet sel -t -m "//enclosure[1]" -v "@url" -n | head -n 1` | ssh -t [user]@[host] "mpg123 -"
0

Ever wanted to stream your favorite podcast across the network, well now you can.

This command will parse the iTunes enabled podcast and stream the latest episode across the network through ssh encryption.

curl -u <username> http://app.boxee.tv/api/get_queue | xml2 | grep /boxeefeed/message/description | awk -F= '{print $2}'
2010-01-20 16:17:19
User: Strawp
Functions: awk grep
Tags: curl xml boxee
0

Might be able to do it in less steps with xmlstarlet, although whether that would end up being shorter overall I don't know - xmlstarlet syntax confuses the heck out of me.

Prompts for your password, or if you're a bit mental you can add your password into the command itself in the format "-u user:password".

xmlproc_parse.python-xml &>/dev/null <FILE> || exit 1
2009-12-11 17:30:03
User: sputnick
Functions: exit
Tags: python xml
-1

For debian likes, that's in python-xml package.

grep -Eho '<[a-ZA-Z_][a-zA-Z0-9_-:]*' * | sort -u | cut -c2-
2009-08-05 21:54:29
User: inkel
Functions: cut grep sort
Tags: sort grep cut xml
0

This one will work a little better, the regular expressions it is not 100% accurate for XML parsing but it will suffice any XML valid document for sure.

sed -n 's/.*<foo>\([^<]*\)<\/foo>.*/\1/p'
2009-07-23 07:59:30
User: recursiverse
Functions: sed
4

Limited, but useful construct to extract text embedded in XML tags. This will only work if bar is all on one line.

If nobody posts an alternative for the multiline sed version, I'll figure it out later...