How do i use the {} operator in find execution inside exec

Solution 1:

You could use a simple for loop to solve this.

for f in $(find /var/www/ -path '*wp-admin/index.php'); do mv $f $(dirname $f)/index_disabled; done

Solution 2:

Your problem is not that it's not interpreted twice, as doing

find . -type f -exec echo {} {} \;

will show. The problem is that {} can't be used as an argument to a function, as you're trying to. In my (limited) experience, if you want to get clever with find and the contents of {}, you'll need to write a shell script that is invoked from find, that takes {} as its one and only argument, and that does the clever things inside that script.

Here's an example clever script:

[me@risby tmp]$ cat /tmp/clever.sh 
#!/bin/bash
echo $1 $(dirname $1)/index_disabled

Here's me using it with find, and the last few lines of the results:

[me@risby tmp]$ find . -type f -exec /tmp/clever.sh {} \;
[...]
./YubiPAM-1.1-beta1/stamp-h1 ./YubiPAM-1.1-beta1/index_disabled
./YubiPAM-1.1-beta1/depcomp ./YubiPAM-1.1-beta1/index_disabled
./YubiPAM-1.1-beta1/INSTALL ./YubiPAM-1.1-beta1/index_disabled

As you can see, if I replaced echo in the shellscript with mv, I'd get the desired result.

Solution 3:

You'll have to use the xargs command and a little trick:

$ find /var/www/ -path '*wp-admin/index.php' | xargs -i sh -c 'mv {} $(dirname {})/index_disabled'