Leos Literak
Leos Literak

Reputation: 9406

Bash: basename inconsistency when run in find

Whole story: I am writing the script that will link all files from one directory to another. New file name will contain an original directory name. I use find at this moment with -execdir option.

This is how I want to use it:

./linkPictures.sh 2017_wien 2017/10

And it will create a symbolic link 2017_wien_picture.jpg in 2017/10 pointing to a file 2017_wien/picture.jpg.

This is my current script:

#!/bin/bash
UPLOAD="/var/www/wordpress/wp-content/uploads"
SOURCE="$UPLOAD/photo-gallery/$1/"
DEST="$UPLOAD/$2/"
find $SOURCE -type f -execdir echo {} ";"

When I run

find . -execdir echo `basename {}` ";"

I receive:

./.
./DSC03278.JPG

Which is strange. So I ran it from command line:

basename ./DSC03278.JPG

and it works as expected:

DSC03278.JPG

Why basename does not work when invoked from find command? I think that the rest of script will be easy.

Upvotes: 1

Views: 655

Answers (1)

melpomene
melpomene

Reputation: 85837

In a command like

foo bar `basename {}` baz

, the `...` part is expanded first. `basename {}` is {}, so this ends up running

foo bar {} baz

Similarly,

find . -execdir echo `basename {}` ";"

expands to

find . -execdir echo {} ";"

first and only then runs find.

Try

find . -execdir basename {} ";"

instead.

Upvotes: 4

Related Questions