Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想从文件夹中选择并复制只有 N 行的文件。
如何在 Bash 中做到这一点?
铝。
您可以使用 bash 中的循环来执行此操作:
for f in *; do [ -f "$f" ] && [ $(wc -l < "$f") = 8 ] && cp "$f" "$dest" done
这将遍历您目录中的所有文件和文件夹。第一个测试检查目标是一个文件。第二个检查行数是否为 8。如果两者都为真,cp则文件为"$dest".
cp
"$dest"
编辑:如果您还想包含隐藏文件,您可以将循环更改为for f in .* *. 感谢@chepner 让我注意到这一点。
for f in .* *