Run A Command Line Process On All Files In A Directory And Collect The Output Into A New File
July 2023
The Script
This was a test script for setting up a larger process. It reads all the files in a directory, passes them through a secondary process, then puts all the outputs in a single file:
#!/bin/bash
OUTPATH="results file.txt"
for
do
|
Details
-
Using quotes around
$OUTPATHallows spaces in the path -
The first `: > "$OUTPATH"
bashline clears out the file. From the bash manual, the `:bash"Do nothing beyond expanding arguments and performing redirections. The return status is zero." When you point that to a file with `>bashit clears it out -
This version uses a `for
bashloop. I've seen other stuff that uses `filebashbut haven't explored the differences yet -
The meat of the program runs `cat
bashon each file to output it then pipes that output to `trbashwith `PIPE_HERE`bash` before appending it to the output file via `>[NEO_TODO_ESCAPED_CHARACTER]`bash` -
The `tr
bashcommand changes all upper case letters to lowercase - Any command that works in the pipeline can be used in place of `tr`bash`. It's simply what's being used for the illustration
Example
Given these three input files:
Pitch The Straw
TacK iT dOWn
RAISE THE SAIL
The script will produce:
Output:
pitch the straw
tack it down
raise the sail
end of line