Saturday, February 18, 2012

Simple xargs example

Say I want to install an rpm package with dependencies. Without knowing about the dependencies I would try:

rpm -i foo.rpm

However, I would then get a warning that there are unresolved dependencies.

One solution is to use yum install with xargs. Here is an example:

ls | xargs yum install -y

You may also want to use grep or something similar to have a criteria passed to xargs

Thursday, February 16, 2012

AWK instead of AWK & grep

Say I want to use df -h to see my available disk space, however I would only like the output the space currently available for rootfs.

I could do:

df -h | grep rootfs | awk '{print $2}'

This however, is a waste of resources because you are piping unnecessarily many times and it is also not very elegant, the following way is more preferable:

df -h | awk '/rootfs/ {print $2}'