0

I'm trying to concatenate multiple files together that are in different directories using the following command:

~$ find . -name ‘*.text’ -exec cat {} >> combined.text \;

However it doesn't seem to be working as I am getting a response as:

find: missing argument to `-exec'

Is there something that I have may missed?

Thank you!

  • This is a typo. You are missing + (or \;, which would be less efficient) after {}. You also seem to have typographical single quotes ( rather than ') in the argument to -name. Unless you really want to append to your output file with the command, change the >> to >. – Kusalananda Jun 5 at 9:50
  • I have included the \; after the {} but it doesn't seem to work. How am I able to make if efficiently work? – Yen Deng Jun 5 at 9:53
  • Also fix the single quotes that I mentioned. Use + instead of \; to run cat as few times as possible. The redirection should still come last on the command line (it's the output of the find command you are redirecting, as a whole). – Kusalananda Jun 5 at 9:54
  • What's the advantage of find here over cat *.text >> combined.text? – Panki Jun 5 at 9:58
  • 1
    @Panki Well, they have files in multiple subdirectories, and cat *.text would furthermore fail if there are too many files. – Kusalananda Jun 5 at 9:59
1

You are using unicode quotes: ‘’ instead of normal quotes (''). Try this command instead:

find . -name '*.text' -exec cat {} +  >> combined.text

However, if combined.text already exists, that will print a warning since combined.text will be created before launching find so will be found by the find command:

$ find . -name '*.text' -exec cat {} +  >> combined.text
cat: ./combined.text: input file is output file

You can avoid that with:

find . -name '*.text' ! -name combined.text -exec cat {} + >> combined.text
  • Hi, an irrelevant question : I thought {} must be in the end before +, but {} in your command doesn't, while it still works, what am i missing? – dedowsdi Jun 5 at 10:57
  • 1
    @dedowsdi the + or the \; must be at the very end of the command you give to -exec: find ... -exec command +. So if the command includes a redirection, you need the + before: -exec cat {} >> file +. However, find ... -exec cat {} + > file.txt will also work, but because here you are redirecting the output of the entire find command. So find will cat the files and then print to standard output and you are using the normal shell redirection to save the output of find to a file. In this particular case there isn't much difference, really. – terdon Jun 5 at 11:26
  • 1
    The >> redirects find, not cat. The file will therefore be created before find is actually invoked (not when cat is run). This is as it should be. Just move the >>combined.txt to after the \; or + to avoid confusion. Would you want to redirect cat specifically, you would need to do so with -exec sh -c 'cat "$1" >>combined.text' sh {} \;, or -exec sh -c 'for file do cat "$file"; done >>combined.txt' sh {} +, or something similar (but that's a bit silly). – Kusalananda Jun 5 at 11:47
  • @Kusalananda yeah, I don't really know why I did it the other way originally. I blame a lack of coffee. – terdon Jun 5 at 11:57
  • @Kusalananda You are right, thanks for the explanation. – dedowsdi Jun 5 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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