unix command to copy all .h file with modified directory-structure

梦想与她 提交于 2019-12-11 18:19:39

问题


Suppose I have a folder structure like:

Libraries\

  • UIToolkit\
    • files\
      • toolkit.h
      • toolkit.c
      • toolkit.resource
  • NetworkLayer\
    • files\
      • network.h
      • network-info.txt

...

I need a command so that I can input the Libraries folder and specify a Output folder then in the Output folder I have:

Output\

  • UIToolkit\
    • toolkit.h
  • NetworkLayer\
    • network.h

Basically it:

  1. copies all .h file and preserves the folder structure
  2. also move all the headers to its sub-libraries' root folders no matter how deep they are in the sub-libraries sub-folders.

I was using rsync but it does not do 2nd step, so I guess I need some quick and dirty modification?

Thanks a lot!


回答1:


A bit modified answer based on devnull's:

idir=$1
odir=$2

while read -r f; do
    subdir=${f#$idir}
    subdir=${subdir%%/*}
    mkdir -p $odir/$subdir
    cp -a $f $odir/$subdir
done < <(find $idir -type f -name "*.h")

call something like

./thisscript.sh Libraries Output

shall be able to work with absolute or relative directories, IMHO; but won't handle if .h file is right under Libraries (must be at least one subdir level down..).




回答2:


You can say:

cd /path/to/Libraries
while read -r file; do
  odir=$(cut -d'/' -f2 <<< ${file});
  fname=$(basename ${file});
  cp "$file" "/path/to/Output/${odir}/${fname}";
done < <(find . -type f -name "*.h")

This would copy all the *.h files to the Output folder as per the desired directory structure.



来源:https://stackoverflow.com/questions/20416524/unix-command-to-copy-all-h-file-with-modified-directory-structure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!