从所有子文件夹打印到文件的前两行



我有以下Linux命令:

head -2 * > output.txt

我想对所有子文件夹运行此命令,并将输出放入同一文件中。

输出示例:

path_file1:
first two lines of file1
path_file2:
first two lines of file2
..
..
..

这可以作为 Linux 命令吗?如果是这样,如何?

您可以使用 find 命令上的 -exec 标志来执行 head -2

find . -type f -exec head -2 {} ; > output.txt

这可能对你有用(GNU sed(:

sed -sn '1,2p' * > /anotherDirectory/output.txt
output=""
for f in $(find .)
do
    output=$output"nn${f}:n"$(head -2 $f)
    echo -e $output
done
echo -e $output > somefile.txt

它遍历通过"find ."找到的所有文件,在输出中添加两个换行符,后跟文件名和冒号,然后是 head -2 命令。最后,所有内容都写入某个文件.txt

findhead结合使用(-v打印文件名(

find . -type f -exec head -vn2 {} ; >print.txt

最新更新