7

So I call this PHP script from the command line:

/usr/bin/php /var/www/bims/index.php "projects/output"

and its output is:

file1 file2 file3

What I would like to do is get this output and feed to the "rm" command but I think im not doing it right:

/usr/bin/php /var/www/bims/index.php "projects/output" | rm 

My goal is to delete whatever file names the PHP script outputs. What should be the proper way to do this?

Thanks!

1

4 Answers 4

12
/usr/bin/php /var/www/bims/index.php "projects/output" | xargs rm
Sign up to request clarification or add additional context in comments.

1 Comment

man xargs: "xargs - build and execute command lines from standard input". You should read that manual page ;)
4

Simplest solution:

rm `/usr/bin/php /var/www/bims/index.php "projects/output"`

What is between the backticks (`...`) is run as a command, and the output is passed as argument to rm.

6 Comments

xargs is slightly better: it can handle output that is too long for the maximum command line length by running rm more than once.
I didn't say better. I said simple :).
note: might not work if the Php script produce files with spaces
Wikipedia has some interesting examples, KISSly explained, regarding files with spaces, lines, etc: en.wikipedia.org/wiki/Xargs.
Even in 2010 the $(command substitution) syntax was recommended over backticks since many, many, many years.
|
3

you can try xargs

/usr/bin/php /var/www/bims/index.php "projects/output" | xargs rm 

or just simply use a loop

/usr/bin/php /var/www/bims/index.php "projects/output" | while read -r out
do
  rm $out
done

Comments

0

I guess this could help>>

grep -n "searchstring" filename | awk 'BEGIN { FS = " " };{print $1}'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.