unix/macos - how to find all files and duplicate in place with a different extension

unix/macos - how to find all files and duplicate in place with a different extension



So I want to grab all certain files then duplicate them in the same folder/location but with a different extension. So far I have this from another question Copy all files with a certain extension from all subdirectories:


find . -name *.js -exec cp newDir ;



I want to duplicate all those .js files into .ts e.g. duplicate functions.js to functions.ts wherever it may be.


.js


.ts


functions.js


functions.ts



more examples:


a/functions.js
b/test.js
c/another.js
index.js



to


a/functions.ts
b/test.ts
c/another.ts
index.ts




2 Answers
2


find . -name *.js | while read jsfile; do cp "$jsfile" "$jsfile%.js.ts"; done


find . -name *.js


.js


read


fine


$jsfile%.js


.js


jsfile


a/functions.js


a/functions






Could you write an explanation about what each part is doing? The answer is correct though.

– A. Lau
Sep 13 '18 at 3:10






@A.Lau Yes, I updated my answer.

– Feng
Sep 13 '18 at 3:14



Here is how to assign variables using find and xargs and open up all sort of command-line options,


find


xargs


$ find . -name '*.js' | xargs -I bash -c 'p=""; cp $p newDir/$(basename $p%.js.ts)'



Use xargs -I to get the output of find as input to xargs. Use bash -c to execute a command.


xargs -I


find


xargs


bash -c



Here is a demo:


$ mkdir -p a b c d newDir
$ touch a/1.js b/2.js c/three.js d/something.js
$ find . -name '*.js' | xargs -I bash -c 'p=""; cp $p newDir/$(basename $p%.js.ts)'
$ ls newDir/
1.ts 2.ts something.ts three.ts



EDIT (Question changed after hours of initial post). To keep a duplicate in the same directory use the same cp command and remove newDir and basename:


cp


newDir


basename


$ find . -name '*.js' | xargs -I bash -c 'p=""; cp $p $p%.js.ts'






To clarify, that command was an example from the other question, I want the output to remain in their respective folders. I have added a more detailed example to my question, thanks.

– A. Lau
Sep 13 '18 at 3:05






see my updated comment. Fairly simple modification to my original answer would get you the result you want.

– iamauser
Sep 13 '18 at 13:52



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)