我想将多个文本文件连接到终端中的一个大文件。我知道我可以使用cat命令来做到这一点。但是,我希望每个文件的文件名在该文件的“数据转储”之前。有人知道怎么做吗?

我目前拥有的:

file1.txt = bluemoongoodbeer

file2.txt = awesomepossum

file3.txt = hownowbrowncow

cat file1.txt file2.txt file3.txt

期望的输出:

file1

bluemoongoodbeer

file2

awesomepossum

file3

hownowbrowncow

当前回答

这也可以做到:

$ find . -type f -print -exec cat {} \;
./file1.txt
Content of file1.txt
./file2.txt
Content of file2.txt

下面是对命令行参数的解释:

find    = linux `find` command finds filenames, see `man find` for more info
.       = in current directory
-type f = only files, not directories
-print  = show found file
-exec   = additionally execute another linux command
cat     = linux `cat` command, see `man cat`, displays file contents
{}      = placeholder for the currently found filename
\;      = tell `find` command that it ends now here

您还可以通过布尔运算符组合搜索,如-and或-or。找到-ls也很好。

其他回答

find . -type f -print0 | xargs -0 -I % sh -c 'echo %; cat %'

这将打印完整的文件名(包括路径),然后是文件的内容。它也非常灵活,因为您可以为find命令使用name“expr”,并在文件上运行任意多的命令。

这里有一个非常简单的方法。您说您想cat,这意味着您想查看整个文件。但是你还需要打印文件名。

试试这个

Head -n99999999 *或Head -n99999999 file1.txt file2.txt file3.txt

希望这能有所帮助

首先,我创建了每个文件:echo 'information' > bfile1 .txt为每个文件[123].txt。

然后我打印每个文件以确保信息是正确的: 尾文件? . txt

然后我做了这个:tail file?.txt >> Mainfile.txt。这就创建了Mainfile.txt来将每个文件中的信息存储到一个主文件中。

cat Mainfile.txt确认没有问题。

==> 文件1.txt <== 蓝月好啤酒

==> file2.txt <== awesomepossum

==> file3.txt <== hownowbrowncow

这应该可以达到目的:

for filename in file1.txt file2.txt file3.txt; do
    echo "$filename"
    cat "$filename"
done > output.txt

或者递归地对所有文本文件执行此操作:

find . -type f -name '*.txt' -print | while read filename; do
    echo "$filename"
    cat "$filename"
done > output.txt

如果所有文件都有相同的名称或可以通过find进行匹配,您可以这样做(例如):

find . -name create.sh | xargs tail -n +1

要查找,显示每个文件的路径并对其进行cat。