Bash: improper function/usage of command basename

What I want in the following command is to find particular files and move them to the other directories while appending ".log" to the destination filename.

find /src/dir/ -type f -mtime +3 -exec mv {} /dst/dir/`basename {}`.log \;

But it fails because the basename command enclosed in the backticks does not operate properly. $(basename {}) has similar result too.

mv: cannot move /src/dir/foo to /dst/dir//src/dir/foo.log: No such file or directory

Any idea would be appreciated.


That's because the shell sees the `basename {}` or $(basename {}) before it handles the arguments to find and processes them. Write a script that does what you want and run it with -exec instead.

find ... -exec myscript {} \;

where myscript is something like

#! /bin/sh
mv "$1" /dst/dir/$(basename "$1").log

You can invoke the shell for each file found, so the following is also possible:

find ... -exec bash -c 'mv "$1" "$(basename "$1").log"' -- {} \;

But test such a solution properly to be sure quoting and escaping works correctly.