0

我想在 Ubuntu 中将一堆文件 (*.txt) 从一个目录复制到另一个目录。我想减小它们的大小,所以我使用 head 来获取每个的前 100 行。

我希望新文件保留其原始名称但位于子目录中small/。我努力了:

head -n 100 *.txt > small/*.txt

但这会创建一个名为*.txt. 我也试过:

head -n 100 *.txt > small/

但这会产生Is a directory错误。

它必须很容易,但我在 Linux 上很糟糕。任何帮助深表感谢。

4

2 回答 2

3

您必须改为创建一个循环:

for file in *.txt; do
    head -n 100 "$file" > small/"$file"
done

这将遍历所有.txt执行 ahead -n 100的所有文件并输出到small/目录中的新文件中。

于 2016-04-21T09:51:26.700 回答
1

尝试

for f in *.txt; do
  head -n 100 $f > small/$f
done
于 2016-04-21T09:51:27.747 回答