5

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?

Thufir
  • 8,216
  • 28
  • 125
  • 273

3 Answers3

7

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

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • 2
    `find` is quite useful for other reasons, getting familiar with the syntax could be quite useful. – 0stone0 Dec 09 '20 at 14:17
7

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
Shmuel
  • 1,568
  • 10
  • 20
0

if you want to search a string in list of files then this should be best option. This will print the filenames as well

grep "some string" *.log