How Do You Get xargs to Execute Rather Than Print a Command?

Problem Scenario
You are writing a Bash command to craft AWS CLI commands. But the commands are not running with your xargs command. You created something like this:

aws s3 ls s3://bucketname/ | awk '{print "/usr/local/bin/aws s3 rm s3://bucketname/"$4}' | xargs -L 1

But it merely displays the well-crafted AWS CLI commands. You want the commands to execute.

You tried a variation but received "xargs: awscli: No such file or directory."

What should you do?

Solution

  1. Find where you AWS binary is. Run this: whereis aws
  2. Place the location before the "aws" command so it appears in the "print" statement of your awk command.

Rewrite the command. This example uses "tail" in case you are dealing with many files in an s3 bucket. You want to have all the command drafted after the xargs -L 1 portion. The piped output should be just the variable for the command (and flags) to run. Here is an example of a command that would work:

aws s3 ls s3://bucketname/ | tail -n 10 | awk '{print "s3://bucketname/"$4}' | xargs -L 1 aws s3 rm

Leave a comment

Your email address will not be published. Required fields are marked *