我想在bash中做一些特别的事情。我找到了for循环和find的部分解决方案,但我更喜欢制作一个脚本来完成更复杂的工作。
我有一个复杂的目录结构如下:
fiction/
book1/
chapter1/
page1.txt
page2.txt
..
chapter2/
page5.txt
page6.txt
..
book2/
chapter1/
chapter2/
..
non-fiction/
book5/
chapter1/
..
我希望你明白这一点。
简而言之,我想做以下事情:
我设法用简单的脚本做到了这一点:
for i in */; do cat "$i"*.txt > /newdir/"${i%%/}".txt; done
当我意识到书籍不仅在章节中,而且在包含章节的部分或子章节中都有分歧时,我的第一个问题出现了。唯一的解决方案是cd到这些文件夹并手动执行脚本。
但这不是专业方式。如何让脚本更智能地遍历子目录然后再返回?
答案 0 :(得分:0)
您可以尝试以下方式:
for b in */*
do
cat $(find "$b" -type f -name \*.txt| sort) >> "$b"/$(basename "$b").txtb
done
答案 1 :(得分:0)
这个答案......
基于这些假设:
#!/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