Lists unambigously names of all xml elements used in files in current directory

grep -h -o '<[^/!?][^ >]*' * | sort -u | cut -c2-
This set of commands was very convenient for me when I was preparing some xml files for typesetting a book. I wanted to check what styles I had to prepare but coudn't remember all tags that I used. This one saved me from error-prone browsing of all my files. It should be also useful if one tries to process xml files with xsl, when using own xml application.

By: thebodzio
2009-06-17 00:22:18

1 Alternatives + Submit Alt

  • 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. Show Sample Output

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

What Others Think

Just a few small hints ... 1. I think the "-e" option is not needed. 2. Why "$(pwd)/*" and not simply "*"? 3. "sort | uniq" can be abbreviated: "sort -u"
inof · 627 weeks and 1 day ago
Thanks a lot for your advice! You're right in all pointed matters and thanks to you this one-liner became much shorter and elegant. I'm sending you a virtual can/bottle/pint/bathtub (whatever rows your boat) of your favorite beverage :).
thebodzio · 625 weeks and 1 day ago
Actually this will display not only the names but also append any attribute that exists on the element, so it might give you errors. Try running it on a folder with XSLT files. I'll try to submit a new version of the command if I've time.
inkel · 620 weeks and 1 day ago
True. I've got a TODO note about that with the script.
thebodzio · 617 weeks ago
Okee. I've reviewed my entry. It's still quite far from caring about many aspects of xml specification but does the job much better than previous version. First of all it shows only element name not element name _and_ attributes as inkel noticed. It also shows element names that contain characters with diacritic signs (like polish "książka" ? book or "tytuł" ? title). Finally I took "cut -c2-" from inkel's script. @inkel: thanks for your interrest and tip about "cut"! :)
thebodzio · 617 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. 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.

Share Your Commands

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.


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: