2

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
oguz ismail
  • 1
  • 16
  • 47
  • 69
Fractale
  • 1,503
  • 3
  • 19
  • 34
  • 2
    Because bash first runs `basename \{\} .dng` and with its output it then runs your `find` command. – Cyrus Jun 13 '20 at 04:21
  • 1
    This needs more details. Are RW2 files in the same directory as dng files, or are they all in the directory where you issue find from? What exactly do you need `basename` for? – oguz ismail Jun 13 '20 at 05:20
  • 1
    @oguz Ismail, RW2 files are in the same directory as dng files. I use basename to remove the prefix. – Fractale Jun 13 '20 at 06:53

1 Answers1

3

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' _ {} +
oguz ismail
  • 1
  • 16
  • 47
  • 69