Gluing RTF Files Chronologically via Bash Script – Tutorial

automatorbashscript

I like learning and creating small scripts for bash, and now I have finally the first "serious" problem I would like to solve through a script.

Here is the situation: I have a folder containing 45 rtf files. I want to prepend to each of them the name of the file (and its creation date) and then glue them all together in a unique rtf file, respecting chronological order.

How can I do that? For the moment I have created the following script: for every rtf file in the folder, it creates a new file (.txt… it is so difficult to use rtf with bash!) and writes into it the name and the creation date of the original file.

for f in *.rtf; do 
touch $f"_info_file.txt"
echo "File name:" $f >> $f"_info_file.txt";
printf "Date: " >> $f"_info_file.txt";
stat -f "%SB" "$f" >>$f"_info_file.txt";
done

(I guess this is very rough…)
Now I would like to glue them together in chronological order but I am a bit lost… Any ideas? Can Automator be useful? Thanks.

Best Answer

Following mod's suggestion, I post here my solution to the problem.

I created the following script for bash:

for f in *.rtf; do
   olddate=$(stat -f %SB -t %Y%m%d%H%M "$f")
   touch -mt $olddate "$f""_info_file.txt"
  echo "File name:" "$f" >> "$f""_info_file.txt";
  printf "Date: " >> "$f""_info_file.txt";
  stat -f "%SB" "$f" >>"$f""_info_file.txt";
done

I ran it into the bash and for each RTF file I had in folder a new .txt file associated to it was created. This txt file contained useful metadata (to me) of the file itself. Notice that, thanks to the script above, the new txt files were created in such a way that their creation date coincided with the files they are referring to.

Example: I had file01.rtf in the folder. After the script ran, in the folder I could find a file named file01_info_file.txt whose content was

Name file: file01.rtf 
Date: 20.12.2018 18.53 

The creation date of the file file01_info_file.txt is identical to the creation date of file01.rtf.

Now the conclusion easily follows: open Finder, sort by creation date the files in the folder, select them all and drag them in a new ... ... Microsoft Word document (:-D). The formatting of rtf was kept and the chronological order as well.