3

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.

1 Answer 1

4

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
2
  • 2
    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, 2012 at 22:37
  • I have set up using your method as it looks cleaner. Thanks again for your help.
    – Aeisor
    Nov 29, 2012 at 22:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .