开发者

How does command substitution work with find?

开发者 https://www.devze.com 2023-03-06 10:38 出处:网络
I have the following开发者_Go百科 command find . -name \"*.tiff\" -exec echo `basename -s .tiff {}` \\;

I have the following开发者_Go百科 command

find . -name "*.tiff" -exec echo `basename -s .tiff {}` \;

I expect this to print all my .tiff-files without their file extensions. What I get is

./file1.tiff
./file2.tiff
...

The command,

find . -name "*.tiff" -exec basename -s .tiff {} \;

does yield

file1
file2
...

Is this not supposed to be the input of echo?


The content of the backticks is executed before the find command - yielding just the placeholder {}, which is used in the find command line - hence your result. You can always use set -x to examine what the shell is up to.


Use single-quote characters (') instead of backticks (`) - putting a command in backticks causes it to be executed and replaced by its output in your command. Also, modify the command to get rid of the echo, like this:

find . -name "*.tiff" -exec 'basename -s .tiff {}' \;

This will execute basename on each found file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号