Копировать только файлы * .h со структурой подпапок bash

814
ShurupuS

Мне нужно написать скрипт для копирования только * .h файлов с сохранением структуры папок:

теперь это выглядит так:

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

но файлы находятся в одной папке, как я могу решить это?

1

2 ответа на вопрос

1
Hastur

In linux find can be really powerful.

You can use

OutDir="$/include/header/cocos/" # Linux is case sensitive, Check if mkdir -p "$" # 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 {} "$" ' \; # 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 "$" echo 'Copying Cocos Headers into Framework..' StartSearchDir="$../Frameworks/Cocos" BaseDestDir="$/include/header/cocos/" cd $StartSearchDir for H in `find . -name "*.h"`; do echo "$" PathFileDir=$(dirname $H) mkdir -p "$/$" # no error, make parents too cp -p "$H" "$/$/" # preserve ownership... # ditto -V "$" "$/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

1
ShurupuS

Resolved the issue this way:

cd "$" echo 'Copying Cocos Headers into Framework..' StartSearchDir="$/../Frameworks/Cocos" BaseDestDir="$/include/$/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

Творческий мир :) Hastur 9 лет назад 0