【问题标题】:unix command to copy all .h file with modified directory-structure用于复制所有具有修改目录结构的 .h 文件的 unix 命令
【发布时间】:2013-12-21 18:47:34
【问题描述】:

假设我的文件夹结构如下:

库\

  • UIToolkit\
    • 文件\
      • 工具包.h
      • toolkit.c
      • 工具包.资源
  • 网络层\
    • 文件\
      • network.h
      • 网络信息.txt

...

我需要一个命令,以便我可以输入库文件夹并指定一个输出文件夹,然后在我拥有的输出文件夹中:

输出\

  • UIToolkit\
    • 工具包.h
  • 网络层\
    • network.h

基本上是这样的:

  1. 复制所有.h文件并保留文件夹结构
  2. 还将所有标题移动到其子库的根文件夹中,无论它们在子库子文件夹中的深度如何。

我在使用 rsync 但它不执行第二步,所以我想我需要一些快速而肮脏的修改?

非常感谢!

【问题讨论】:

    标签: bash shell bsd


    【解决方案1】:

    基于 devnull 的一些修改答案:

    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")
    

    调用类似的东西

    ./thisscript.sh Libraries Output
    

    应该能够使用绝对或相对目录,恕我直言;但如果 .h 文件位于库下(必须至少低于一个子目录级别..),则不会处理。

    【讨论】:

      【解决方案2】:

      你可以说:

      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")
      

      这会根据所需的目录结构将所有*.h 文件复制到Output 文件夹。

      【讨论】:

      • 嗨,done 后面还有多少个 find 的结果只是包含所有文件名的一行,我该如何换行?
      • @hzxu 我相信有一个额外的右括号可能导致了这个问题。检查上面的编辑。
      猜你喜欢
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 2017-05-19
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      • 2011-10-20
      相关资源
      最近更新 更多