Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I tried this bash command:

find /var/www/ -path '*wp-admin/index.php' -exec mv {} $(dirname {})/index_disabled

But the second {} is not executed.

It results in just ./index_disabled instead.

How can I use the found parameter twice in the execute command?

share|improve this question
    
the problem is in $(dirname {}) –  tigran Apr 17 '13 at 9:52

2 Answers 2

up vote 2 down vote accepted

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'
share|improve this answer
    
I get the error xargs: %: Datei oder Verzeichnis nicht gefunden –  rubo77 Apr 17 '13 at 12:04
    
You may have a typo. If not try: xargs -i sh -c 'mv {} $(dirname {})/index_disabled' in place. –  Spack Apr 17 '13 at 12:48
    
this works, you should update your answer, then this will be the solution to the question –  rubo77 Apr 17 '13 at 13:39

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.

share|improve this answer
    
thanks, that solved my problem. But how could I solve this without having to write an extra shell script? for the next time ;) –  rubo77 Apr 17 '13 at 12:00
1  
Beats me; I run into this problem seldom enough that I take the "noddy shell script" route when I do. It also has the handy advantage that, by using echo in place of the real function, you can check what it's going to do before running it for real, thus avoiding the sort of "I just blew away 375 important files, I have no backups, how do I undelete" question that gets asked depressingly often around these parts. –  MadHatter Apr 17 '13 at 12:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.