0

I have a simple bash script that takes all .md files of the current folder and merge them into a new file:

for f in *.md; do cat "$f"; echo; done > output.txt;

It works well. Now I'd like to extend this script to takes a bunch of directory as parameters and combine all the md files of all these directories into one new file. How can I do that?

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
pimpampoum
  • 5,816
  • 6
  • 24
  • 27

2 Answers2

0

You can just add multiple folders:

for f in folder1/*.md folder2/*.md ... ; do cat "$f"; echo; done > output.txt;
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0

You could also try this,

find . -mindepth 1 -type f -name "*.md" -exec cat {} \; > output.txt

Searches for the .md files inside the current and the sub-directories and execute the cat command on all those founded files. Finally it stores the output to a text file.

For two separate folders, you may try this,

find /path/folder1 /path/folder2 -mindepth 1 -type f -name "*.md" -exec cat {} \; > output.txt
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274