如何将文件夹中的所有 .txt 文件和所有包含的文件夹移动到目标目录中。
最好将它们重命名为它们所在的文件夹,尽管这并不那么重要。我对 bash 不太熟悉。
要递归移动文件,请组合find
with mv
.
find src/dir/ -name '*.txt' -exec mv -t target/dir/ -- {} +
或者如果在没有 GNU 版本的 UNIX 系统上find
,例如 macOS,使用:
find src/dir/ -name '*.txt' -exec mv -- {} target/dir/ ';'
要在移动文件时重命名它们,这会比较棘手。一种方法是使用一个循环"${var//from/to}"
替换所有出现的from
with to
in $var
.
find src/dir/ -name '*.txt' -print0 | while IFS= read -rd $'\0' file; do
mv -- "$file" target/dir/"${file//\//_}"
done
这很丑,因为from
是一个斜杠,需要转义为\/
.
也可以看看:
- Unix.SE:理解IFS= read -r line https://unix.stackexchange.com/questions/209123/understanding-ifs-read-r-line
- BashFAQ:如何逐行(和/或逐字段)读取文件(数据流、变量)? https://mywiki.wooledge.org/BashFAQ/001
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)