Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to cat all files in a given directory, but with a particular file at the beginning and end?

For example, say I have: file1.js, file2.js, file3.js, file4.js, file5.js -- Effectively I would like to cat file2.js file*.js file3.js > /var/www/output.js

I've tried a few variations of these

find ! -name "file2.js" ! -name "file3.js" -type f -exec cat file2.js {} file3.js > /var/www/js/output.js \;
find ! -name "file2.js" ! -name "file3.js" -type f | xargs -I files cat file2.js files file3.js > /var/www/output.js

but the best I can get out of it is file2.js added before and file3.js added after all other files (multiple times)

I know I could specify the files in the order I wanted manually, but this is not maintainable (I'm expecting, potentially >100 files).

I have looked through man cat, as well as a handful of websites devoted to xargs, find and cat to no avail.

Thanks in advance.

share|improve this question
up vote 4 down vote accepted

With the extglob option in bash you can do just that:

$ shopt -s extglob
$ cat file2.js !(file[23].js) file3.js > /var/www/output.js
share|improve this answer
I just found an answer that appears to work for myself: cat file2.js $(find ! -name "file2.js" ! -name "file3.js" -type f) file3.js > /var/www/output.js Thanks for answering! – Aeisor Nov 29 '12 at 22:37
I have set up using your method as it looks cleaner. Thanks again for your help. – Aeisor Nov 29 '12 at 22:48

Your Answer


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.