输入子目录(带空格)并将文件合并到另一个目录

时间:2012-09-02 20:04:12

标签: bash directory-traversal

我想在bash中做一些特别的事情。我找到了for循环和find的部分解决方案,但我更喜欢制作一个脚本来完成更复杂的工作。

我有一个复杂的目录结构如下:

fiction/
 book1/
  chapter1/
   page1.txt
   page2.txt
   ..
  chapter2/
   page5.txt
   page6.txt
  ..
 book2/
  chapter1/
  chapter2/
 ..
non-fiction/
 book5/
  chapter1/
..

我希望你明白这一点。

简而言之,我想做以下事情:

  1. 输入每本书和章节,例如第1章
  2. 在/mybooks/book1/chapter1.txt
  3. 中查看所有page1,page2等
  4. 进入下一章,并重复/mybooks/book1/chapter2.txt
  5. 继续阅读下一本书
  6. 我设法用简单的脚本做到了这一点:

    for i in */; do cat "$i"*.txt > /newdir/"${i%%/}".txt; done
    

    当我意识到书籍不仅在章节中,而且在包含章节的部分或子章节中都有分歧时,我的第一个问题出现了。唯一的解决方案是cd到这些文件夹并手动执行脚本。

    但这不是专业方式。如何让脚本更智能地遍历子目录然后再​​返回?

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方式:

for b in */*
do 
  cat $(find "$b" -type f -name \*.txt| sort) >> "$b"/$(basename "$b").txtb
done

答案 1 :(得分:0)

这个答案......

  • 处理带空格的目录
  • 在页面
  • 中有超过9页的情况下对页面进行排序

基于这些假设:

  • 页面总是紧跟章节* <​​/ li>
  • 页面被命名为页面[一个或多个数字] .txt
  • 只有一个部分与路径中的“chapter *”匹配,所以没有什么比“chapter2 / subchapter1”

#!/bin/bash

while read -r -d $'\0' path; do
    output="$path".txt
    ls -1 "$path"/page*.txt | \
     sed 's/^.*\([0-9][0-9]*\).txt/\1/' | \
     sort -n | \
     while read n; do
         echo "cat ${path}/page${n}.txt >> $output"
         # cat "${path}/page${n}.txt" >> "$output"
     done
done < <(find fiction/ -type d -name "chapter*" -print0)

如果对结果感到满意,请删除echo - 行并取消注释以下内容。

实施例

find f
f
f/book2
f/book2/chapter1
f/book2/chapter1/page10.txt
f/book2/chapter1/page2.txt
f/book2/chapter1/page1.txt
f/book2/chapter3
f/book2/chapter3/page2.txt
f/book2/chapter3/page1.txt
f/book2/chapter2
f/book2/chapter2/page2.txt
f/book2/chapter2/page1.txt
f/book2/part1
f/book2/part1/subsection1
f/book2/part1/subsection1/chapter1
f/book2/part1/subsection1/chapter1/page2.txt
f/book2/part1/subsection1/chapter1/page3.txt
f/book2/part1/subsection1/chapter1/page1.txt
f/book1
f/book1/chapter1
f/book1/chapter1/page2.txt
f/book1/chapter1/page1.txt
f/book1/chapter3
f/book1/chapter3/page2.txt
f/book1/chapter3/page1.txt
f/book1/chapter2
f/book1/chapter2/page2.txt
f/book1/chapter2/page1.txt
f/book with space
f/book with space/chapter1
f/book with space/chapter1/page2.txt
f/book with space/chapter1/page1.txt

输出:

cat f/book2/chapter1/page1.txt >> f/book2/chapter1.txt
cat f/book2/chapter1/page2.txt >> f/book2/chapter1.txt
cat f/book2/chapter1/page10.txt >> f/book2/chapter1.txt
cat f/book2/chapter3/page1.txt >> f/book2/chapter3.txt
cat f/book2/chapter3/page2.txt >> f/book2/chapter3.txt
cat f/book2/chapter2/page1.txt >> f/book2/chapter2.txt
cat f/book2/chapter2/page2.txt >> f/book2/chapter2.txt
cat f/book2/part1/subsection1/chapter1/page1.txt >> f/book2/part1/subsection1/chapter1.txt
cat f/book2/part1/subsection1/chapter1/page2.txt >> f/book2/part1/subsection1/chapter1.txt
cat f/book2/part1/subsection1/chapter1/page3.txt >> f/book2/part1/subsection1/chapter1.txt
cat f/book1/chapter1/page1.txt >> f/book1/chapter1.txt
cat f/book1/chapter1/page2.txt >> f/book1/chapter1.txt
cat f/book1/chapter3/page1.txt >> f/book1/chapter3.txt
cat f/book1/chapter3/page2.txt >> f/book1/chapter3.txt
cat f/book1/chapter2/page1.txt >> f/book1/chapter2.txt
cat f/book1/chapter2/page2.txt >> f/book1/chapter2.txt
cat f/book with space/chapter1/page1.txt >> f/book with space/chapter1.txt
cat f/book with space/chapter1/page2.txt >> f/book with space/chapter1.txt