Pretty print XML on Unix command line

Needed to check some XML output from a CalDAV service so I used curl, which is nice and simple. Only problem was that all the XML came on a single long unreadable line. Figured out this was quite simple to fix.

$ curl --digest --user usr:pwd -X PROPFIND http://cal.example.com/cal.php/principals/usr/default | xmllint --format -

The key part here is of course the piping into xmllint. --format tells it to format the XML and the - tells it to read the XML from standard in. The dash can be swapped with the path to an XML file, if you need to format already downloaded XML.

$ xmllint --format file.xml

Simple pimple dimple :)

Unix HEAD check

Needed to check the HEAD of a URL on two Unix servers today. Goal was to check if routing, firewall and load balancer rules were all good. One server only had curl, and the other only had wget, so here are commands for both:

$ wget -S --spider http://geekality.net
$ curl -i -X HEAD http://geekality.net

Unix: Recursive search for text in files

Keep forgetting how to do this, so here it is. How to do a quick and simple, recursive search for text using grep. Hint: It’s very simple.

$ grep -rl "some text" .

That’s all there is to it! grep is the command, r makes the search recursive (i.e. in the target folder and all its sub-folders), l means it will list the name of all the files where it finds the text, "some text" is the string to search for, and finally, the dot at the end means to start the search in the current directory. And just to clarify, this searches for text inside of files, in the content of the files; not the filenames.

There, now I know where to find it when I forget it next time. And perhaps I have helped someone else too :)

Good night!

How to make the Bash command prompt more useful

Was on a Linux box today and I found the command prompt rather useless. It looked like this:

-bash-3.2$

I know I’m using bash, and I don’t really care about the version. Or that I’m using bash actually… But anyways, to make it more useful you can run this:

$ PS1="\u@\h:\w$ "

You will then get a prompt which contains your username, hostname and current working directory. Much more useful in my opinion. If you have a different opinion, please share :)

You can find more stuff to put in your prompt by reading the prompting section of the bash man page.

How to ignore “find: cannot read dir /foo: Permission denied”

If you run the following command on a Unix system you might see a lot of errors.

$ find / -name "tar"

In my case I got 8 regular findings and 63 lines of “find: cannot read dir /foo: Permission denied”. Quite a lot of noise I really don’t care about. Using my new knowledge about streams it is easy to get rid of all that though. Just pipe stderr into /dev/null.

Continue reading