Recursively copy files from subfolder and add file extension

1

0

I have following folder structure:

/[randomChars]/icons/icon_[randomNumbersAndChars]

For example:

/awbwaidhawd/icons/icon_1241245
/awbwaidhawd/icons/icon_1552542
/awgawbvbbbb/icons/icon_1552542
/aawbbbbbbbb/icons/icon_1161266

I need to grab all icon files recursively, add the file extension ".png" to them and copy them to another folder.

That means I want to achieve following result:

/icon_1241245.png
/icon_1552542.png
/icon_1552542.png
/icon_1161266.png

For that I want to use standard bash linux scripting.

How could you achieve that?

EDIT:

Maybe I should add that there are more subfolders in the first layer that are not important. That means there exsists for example a folder like:

/awbwaidhawd/avatars/

Such folders should not be scanned.

Additionally, in a "icons" folder there can be files that are not named "icon_xxxxx". Those files should not be moved too.

Jonas Dedden

Posted 2019-01-29T22:09:45.567

Reputation: 11

What have you tried so far? Super User is not a script writing service; sole requests for code may not get much (positive) attention. If you [edit] the question and show your research effort (post the exact commands you tried, tell us where you are stuck) then you will increase your chances for a good answer. – Kamil Maciorowski – 2019-01-30T00:12:38.983

Answers

0

Maybe this is a simple solution:

find /source/directory -type f -execdir cp {} `pwd`/destination/directory/\{\}.png \;

Of course you can filter it by name (--name) if you don't want copy every file. Please note if there are files with same name they will be overwritten in /destination/directory/.

uzsolt

Posted 2019-01-29T22:09:45.567

Reputation: 1 017