Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am confused on the -exec command. E.g. in this case:

find . -type f -name "*.c" -exec cat {} \;>all_c_files.txt

It seems that I get cat file1 file2 file3 ... fileN

While in this case:

find . -type f -name "*.txt" -exec cp {} OLD \;

I get:

`cp file1 OLD`  
`cp file2 OLD`  
`cp file3 OLD`  
...  
`cp fileN OLD`  

Similar case e.g. in:

find . -type f -name "*.txt" -exec printf "Text file: %s\n" {} \;

Seems to do:

printf "Text file: file1"  
printf "Text file: file2"  
printf "Text file: file3"  
...  
printf "Text file: fileN"  

So how does exec work? I mean am I right that it behaves differently in these example?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

I suppose that you are confused by cat command (and shell redirection), and not by find one.

find . -type f -name "*.c" -exec cat {} \; > all_c_files.txt is equivalent to:

(
  cat file1 ;
  cat file2 ;
  cat file3 ;
  ... 
  cat fileN
) > all_c_files.txt

obviously the previous command and the following one have the same identical result:

cat file1 file2 file3 ... fileN > all_c_files.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.