Quick Shell Scripting Examples

If you learn how to use and combine various commands it can really speed up work. Today I was looking for a way to turn multiple Org Mode files into a single, readable HTML file. The filenames are in the format 2019-03-17-Sunday.org, and I wanted them in order of date. I was able to quickly do it like this:

$ cat $(ls -1 | sort | grep org$) | pandoc -f org -t html > output.html

Explanation:

  1. cat – concatenates multiple files to stdout
  2. $() – evaluates the commands inside the parentheses
  3. ls -1 – prints a list of files in one column
  4. | – pipes the output of one command into the next command
  5. sort – sorts the results (by date in this case)
  6. grep org$ – this filters out any files that don’t have org as the last part of the filename (excludes directories and backup files that end with a tilde, like filename.org~)
  7. pandoc -f org -t htmlpandoc is a tool to convert between formats, in this case from (-f) Org Mode to (-t) HTML
  8. > this sends the output to a file named output.html
1 Like