Copy only *.h files with subfolders structure bash

1

1

I need to write a script to copy only *.h files with saving the folder structure:

now it looks like this:

cd "${SRCROOT}"
echo 'Copying Cocos Headers into Framework..'
cd ..
for H in `find ./Frameworks/Cocos -name "*.h"`;  do
echo "${H}"  
ditto -V "${H}" "${BUILD_DIR}/include/header/cocos/"

done

but the files are in one folder, how can I solve this?

ShurupuS

Posted 2014-12-08T06:32:11.637

Reputation: 123

Answers

1

In linux find can be really powerful.

You can use

OutDir="${BUILD_DIR}/include/header/cocos/" # Linux is case sensitive, Check if
mkdir -p "${OutDir}"                        # it is needed Cocos or cocos...
cd ./Frameworks/Cocos                       # just to have clean path to create

# Here with only one line 
find . -name "*.h" -exec bash -c 'cp -p --parents {} "${OutDir}" ' \;
# cd - # Eventually to come back to the previous path 

Notes:
cp -p preserve ownership...
cp --parentscreate destination dir but needs that the base directory it exists.
mkdir -p Create the directory with all the parents' path without error if just exists
man find for all the options of find.


If you want to remain close to the precedent script

cd "${SRCROOT}"
echo 'Copying Cocos Headers into Framework..'
StartSearchDir="${SRCROOT}../Frameworks/Cocos"
BaseDestDir="${BUILD_DIR}/include/header/cocos/"
cd $StartSearchDir
for H in `find . -name "*.h"`;  do
  echo "${H}" 
  PathFileDir=$(dirname $H)
  mkdir -p "${BUILD_DIR}/${PathFileDir}"     # no error, make parents too
  cp -p "$H" "${BUILD_DIR}/${PathFileDir}/"    # preserve ownership...
  # ditto -V "${H}" "${BUILD_DIR}/include/header/cocos/" # commented line
done

Note with dirname you can extract from a full path+filename string only the path.
Check the help with man dirname and man basename

Hastur

Posted 2014-12-08T06:32:11.637

Reputation: 15 043

1

Resolved the issue this way:

cd "${SRCROOT}"
echo 'Copying Cocos Headers into Framework..'

StartSearchDir="${SRCROOT}/../Frameworks/Cocos"
BaseDestDir="${BUILD_DIR}/include/${PRODUCT_NAME}/header/cocos/"

echo 'STARTDIR:'$StartSearchDir
echo 'DESTDIR:'$BaseDestDir

cd $StartSearchDir

tar -cf - . | (cd $BaseDestDir ; tar -xpf - --include='*.h')

But Hastur solution is nice too - make his solution as the best one

ShurupuS

Posted 2014-12-08T06:32:11.637

Reputation: 123

Creative woraround :) – Hastur – 2014-12-08T13:45:17.953