Search code examples
bashfind

Find command is not sending one file at a time to my php command


I have a bunch of php files in a directory called blog, so it looks kind of like this:

./blog/hello.php
./blog/essay-for-english-class.php
./blog/research-for-south-american-reptiles.php

If I run a command like cd blog && php hello.php, I will see some json content, which is what I expect. I want to run a bash command that outputs each php file's content to their own separate json file in a separate directory while also keeping a variation of the original file name. So I tried something like this:

cd blog;
find ./ -iname "*.php" -exec php "{}" > "../build/blog/{}.json" \;

But the result is this:

cd ../build && ls;

{}.json 

Only the {}.json file appears in the directory. And it has ALL the php json content concatenated into one file. This is not what I was expecting.

I was expecting:


../build/blog/hello.php.json
../build/blog/essay-for-english-class.php.json
../build/blog/research-for-south-american-reptiles.php.json

What did I do wrong?

Ideally I also want to remove the .php from the json file names as well.


Solution

  • Based on comments from Gordon and HatLess, this was my final solution:

    cd blog;
    find ./ -name '*.php' -type f -exec bash -c 'for file do php "$file" > "$(pwd)/../build/blog/$(basename ${file/.php*/.json})"; done' sh {} +;