iLemming
iLemming

Reputation: 36176

Append a file to the beginning of other files

I need to query all javascript files in a folder (except 'pre.js') and prepend each file with the content of pre.js

it seems fairly easy to do for a single file:

cat pre.js |cat - foo.js  > /tmp/out && mv /tmp/out foo.js

but I have multiple files in the folder

Upvotes: 1

Views: 59

Answers (4)

glenn jackman
glenn jackman

Reputation: 246847

ed is the standard editor:

shopt -s extglob nullglob

for f in !(pre).js; do ed -s "$f" << END
0r pre.js
w
q
END
done

Upvotes: 1

gniourf_gniourf
gniourf_gniourf

Reputation: 46833

This should do:

find folder/ -name '*.js' \! -name 'pre.js' -exec sh -c 'cat pre.js "$1" > /tmp/out && mv /tmp/out "$1"' _ {} \;

Upvotes: 0

pobrelkey
pobrelkey

Reputation: 5973

A slightly more general form of the other answer:

for i in *.js ; do if [[ "${i}" != pre.js ]] ; then cat pre.js "${i}" > $$ ; mv $$ "${i}" ; fi ; done

Upvotes: 1

janos
janos

Reputation: 124656

That calls for a simple one-liner loop:

for f in foo.js bar.js baz.js; do cat pre.js $f > /tmp/out && mv /tmp/out $f; done

Upvotes: 1

Related Questions