find . -type f > a.txt find . -type f > a.txt cat a.txt | while read FILENAMES; do touch "$FILENAMES"; done cat a.txt | while read FILENAMES; do touch "$FILENAMES"; done 结果:第2步“创build文件”(我的意思是只有相同的文件名,但是有0字节大小),但如果在“A”目录中有子目录,则步骤2不能创build子目录中的文件,因为里面没有目录。
问:有没有办法,“触摸”可以创build目录?
快速拍摄:
while read FILENAME do mkdir -p $(dirname "$FILENAME") && touch "$FILENAME" done < a.txt
请注意文件/path名中的特殊字符(空格,…)等…
我最近在一个GNU平行踢上。 以下是使用该工具在一行中执行此操作的一种方法:
find A -type d | parallel 'mkdir -p B/$(dirname {})' && find . -type f | parallel 'touch B/{}'
请注意,这是低效的,因为它为中间目录运行了大量额外的不需要的mkdir -p 。 如果你正在处理真正巨大的目录结构,这应该被优化。
在原来的“A”目录下:
find . -type f > a.txt
“B”目录:
while read file; do if [[ "$file" = */* ]]; then mkdir -p "${file%/*}"; fi; touch "$file"; done < a.txt
即使A包含文件,这也是有效的:
A /我兄弟的12个“logging目录/我兄弟的12”logging
(cd A; find . -type f) | (cd B; parallel 'mkdir -p {//}; touch {}')
处理用户创build“创造性”文件名我总是testing这样的脚本
My brother's 12" records
如果这样做,那么机会是好的,它不会失败。
感谢Phil H提供的基本构build块。
在做之前:
cat a.txt | while read FILENAMES; do touch "$FILENAMES"; done
做:
cat a.txt | sed -e `s|/[^/]*$||` | uniq | while read DIRNAMES; do mkdir -p "$DIRNAMES"; done
正如已经指出的,小心特殊字符在dirnames /文件名,尤其是斜杠(我讨厌有一个名为“aaa / ccc”的文件,但它总是可能的)。