If I just use basename {} .txt, it will work:
find . -iname "*.txt" -exec basename {} .txt \;
It will just print xxx instead of ./xxx.txt
if I want use $(basename {} .txt) in the -exec option, it will fail:
find . -iname "*.txt" -exec echo "$(basename {} .txt)" \;
It will just print ./xxx.txt
How can I solve this problem? I hope I can use $(basename {} .txt)
as parameters for other cmd. Do I have to do sh -c
or pipe -exec basename {} \;
with xargs?
Best Answer
Try:
Your first command failed, because
$(...)
run in subshell, which treat{}
as literal. sobasename {} .txt
return{}
, yourfind
became:which print file name matched.