この答え...
- スペースを含むディレクトリを処理する
- 1 つの章に 9 ページを超えるページがある場合に、ページを正しく並べ替える
これは、次の仮定に基づいています。
- ページは常にチャプターのすぐ下にあります*
- ページは page[1 つ以上の数字].txt という名前です
- パスの「chapter*」に一致する部分は 1 つだけなので、「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