I have lots of files eg.

a.xxx.001 a.xxx.002

b.yyy.001 b.yyy.002 b.yyy.003

How can I merge then into: a.xxx and b.yyy? In Ubuntu


Assuming the original file was straightforwardly split into chunks (so the chunks don't have headers or anything like that), use the concatenate command:

cat a.xxx.??? >a.xxx

Make sure that you have all the pieces, since cat will just concatenate what it's given. If your shell is bash (the default shell) or zsh, you can use

cat a.xxx.{001..002} >a.xxx
cat b.yyy.{001..003} >b.yyy

This way, cat is given the names of the expected pieces in order, so you will get an error message if any of the pieces is missing.

  • Assuming that the current working directory contains only those pieces, he can also use cat a.xxx.* > cat a.xxx. – Cristian Ciupitu Sep 18 '10 at 12:45
  • 1
    @Cristian: I didn't use * because it's more failure-prone. If the pieces are called …, a.xxx.9, a.xxx.10, …, it's obvious that .??? won't work, whereas .* would silently concatenate the pieces in the wrong order. .??? is also a little more robust against stray files being caught, though .[0-9][0-9][0-9] would be safer (but would require significantly more typing). – Gilles 'SO- stop being evil' Sep 18 '10 at 13:48
  • @Giles: a good split program would use proper file names for the pieces to avoid this issue. – Cristian Ciupitu Sep 18 '10 at 20:01
$ cat a.xxx.001 a.xxx.002 > a.xxx
$ cat b.yyy.001 b.yyy.002 b.yyy.003 > b.xxx
  • Your idea is good, but he said that he has lots of files, not only a couple of files. – Cristian Ciupitu Sep 18 '10 at 12:43
  • Not sure how the previous comment relates; both answers do it well, but you should definitely be using wild cards instead of explicitly listing them. – Andrew M. Sep 18 '10 at 13:11
  • True. The answer below is better. Btw, to do it on modification date, something like "cat ls -tr a.xxx.* > a.xxx" would work. – Wouter de Bie Sep 18 '10 at 14:28
  • @Redmumba: yeah, I was referring to wildcards. – Cristian Ciupitu Sep 18 '10 at 15:43

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.