Ramy
Ramy

Reputation: 21271

How to `rm` files as awk action?

This one-liner:

sudo df /tmp \
  | grep '/tmp' \
  | expand - \
  | cut -d " " -f 12 \
  | sed 's/%//' \
  | awk '{ if ($1<50)
             $("sudo rm -rf /path/to/trash/files/*")
         }'

seems to have no effect, while this:

sudo df /tmp \
  | grep '/tmp' \
  | expand - \
  | cut -d " " -f 12 \
  | sed 's/%//' \
  | awk '{ if ($1<50)
             print $1
         }'

prints the percentage of disk used for tmp.

(The end goal is to flip the comparison around to ($1>50), but for testing I'm trying <.)

Upvotes: 2

Views: 396

Answers (1)

anubhava
anubhava

Reputation: 785761

You don't really need so many commands in pipeline with awk.

You can just use:

df /tmp | awk 'NR>1 && $5+0 > 50 {system ("date")}'

Here change date command to something else that you need to run there.

Upvotes: 4

Related Questions