0

So I'm looking for a way to cat .html files in multiple subfolders, but by keeping them in their place.

Actual situation:

$ Folder1
.
├── Subfolder1
│   └── File1.html
    └── File2.html
├── Subfolder2
│   └── File1.html
    └── File2.html

Desired outcome:

$ Folder1
.
├── Subfolder1
│   └── Mergedfile1.html
    └── File1.html
    └── File2.html
├── Subfolder2
│   └── Mergedfile2.html
    └── File1.html
    └── File2.html

So far I've came up with this:

find . -type f -name *.html -exec cat {} + > Mergedfile.html

But this combines all the files of all the subfolders of Folder1, while I want to keep them separated.

Thanks a lot!

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Hugo
  • 3
  • 1

2 Answers2

1

You can loop on all subfolders with a for statement:

for i in Folder1/SubFolder*; do
   cat "$i"/File*.html > MergeFile$(echo "$i" | sed 's,.*\([0-9]\+\)$,\1,').html
done
oliv
  • 12,690
  • 25
  • 45
  • Unquoted variables will work in this limited case, but in order for your code to work in common corner cases, you should *always* quote variables which contain file names with double quotes. See also https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable – tripleee Dec 18 '17 at 10:11
0

Like told by AK_ , you can use find with exec.

find Folder1/ -mindepth 1 -maxdepth 1 -type d -exec sh -c "rep='{}';cat "'"$rep"'"/*.html > "'"$rep"'"/Mergedfile.html" \;
ctac_
  • 2,413
  • 2
  • 7
  • 17
  • You can't use `{}` like so! it's dangerous and will not work with all versions of `find`. Dangerous: what happens if there's a file named `'; rm -rf / #`? POSIX clearly states that [If a utility_name or argument string contains the two characters "{}", but not just the two characters "{}", it is implementation-defined whether find replaces those two characters or uses the string without change.](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html). So you're usage of `{}` is dangerous and not correct. – gniourf_gniourf Dec 18 '17 at 18:47
  • This worked perfectly ! Justed run another command to add the subfolder name afterwars. Thanks a lot @ctac_ ! – Hugo Dec 19 '17 at 08:23