Search code examples
shellawkxargs

How can I use xargs properly?


I just want to delete all of my Vagrant boxes in my local machine. Here's the boxes that I have:

$ vagrant box list
centos/7                  (virtualbox, 1509.01)
coreos-alpha              (virtualbox, 1010.1.0)

First strings (e.g. "centos/7") in each line seems to be the box name, so I've tried following command:

$ vagrant box list | awk '{ print $1; }' | xargs vagrant box remove

I was just trying to get the first string and pass it to vagrant box remove using xargs because I want to have the string as an argument of the command. However, I got the following error somehow. What am I missing?

This command was not invoked properly. The help for this command is
available below.

Usage: vagrant box remove <name>

Options:

    -f, --force                      Remove without confirmation.
        --provider PROVIDER          The specific provider type for     the box to remove
        --box-version VERSION        The specific version of the box     to remove
        --all                        Remove all available versions     of the box
    -h, --help                       Print this help

Thanks,


Solution

  • I think xargs is merging more boxes into a single command, as this is the default behavior. Try using the -n switch to force one argument per command:

    $ vagrant box list | awk '{ print $1; }' | xargs -n 1 vagrant box remove