how to pipe ls into cat with xargs so that filenames are listed?

Viewed 1674

For using cat to output *.txt files within a specific directory, how can I insert the filename as a "break" before each file?

file #1
contents of file

file #2
contents of file

ideally as an easy to remember few bash commands.

see also:

Why not pipe list of file names into cat?

2 Answers

One more option to @0stone0 answer You can use tail insted of cat:

tail -n +1 *

Output:

==> file-1.txt <==
contents of file


==> file-2.txt <==
contents of file

Improved answer;

Instead of an separate -exec basename, using just -print will show the filename:

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

You could use a find command with multiple -exec;

find . -type f -iname '*.txt' -exec basename {} \; -exec cat {} \;
-> ls
a.txt  b.txt

-> find . -type f -iname '*.txt' -exec basename {} \; -exec cat {} \;
b.txt
b
a.txt
a

Related