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

我目前拥有的:

file1.txt = bluemoongoodbeer

file2.txt = awesomepossum

file3.txt = hownowbrowncow

cat file1.txt file2.txt file3.txt

期望的输出:

file1

bluemoongoodbeer

file2

awesomepossum

file3

hownowbrowncow

当前回答

这个方法将先打印文件名,再打印文件内容:

tail -f file1.txt file2.txt

输出:

==> file1.txt <==
contents of file1.txt ...
contents of file1.txt ...

==> file2.txt <==
contents of file2.txt ...
contents of file2.txt ...

其他回答

find . -type f -exec cat {} \; -print

你可以使用这个简单的命令来代替for循环,

ls -ltr | awk '{print $9}' | xargs head

我做了一个组合:

Cat /sharedpath/{unique1,unique2,unique3}/filename >新文件

and

Tail -n +1 file1 file2

到这个:

Tail -n +1 /sharedpath/{folder1,folder2,…,folder_n}/文件。扩展名| cat > /sharedpath/newfile

结果是一个newfile,其中包含{}括号中每个子文件夹(unique1,unique2..)的内容,以子文件夹名称分隔。

注意unique1 = folder1

在我的例子中是文件。扩展名在所有子文件夹中具有相同的名称。

我用grep来做类似的事情:

grep "" *.txt

它不会给你一个“标题”,而是给每一行加一个文件名前缀。

这个方法将先打印文件名,再打印文件内容:

tail -f file1.txt file2.txt

输出:

==> file1.txt <==
contents of file1.txt ...
contents of file1.txt ...

==> file2.txt <==
contents of file2.txt ...
contents of file2.txt ...