Search code examples
bashfindsubshellcommand-substitution

Why basename with a suffix doesn't work in a subshell when used with find?


The following command doesn't do the subtitution, why?

find ./ -name "*.dng" -exec echo `basename \{\} .dng`  \;

but this command work:

find ./ -name "*.dng" -exec basename \{\} .dng  \;

What I'm actually trying to do is to find all the dng in my hard drive and do:

touch -c -r {basename}.RW2  {basename}.dng

Solution

  • The following command doesn't do the subtitution, why?

    find ./ -name "*.dng" -exec echo `basename \{\} .dng`  \;
    

    As Cyrus already said in his comment, bash expands `basename \{\} .dng` to {} before invoking find; so what find receives is just echo {}, it doesn't see `basename \{\} .dng` part.

    What I'm actually trying to do is to find all the dng in my hard drive and do:

    touch -c -r {basename}.RW2 {basename}.dng
    

    Assuming each reference file (*.RW2) is in the same directory as corresponding .dng file, I would do it like this:

    find . -name '*.dng' -exec sh -c '
    for dng do
      touch -c -r "${dng%.*}.RW2" "$dng"
    done' _ {} +