I am wondering how to to process files from some folder dir1 if they are piped through various tasks and in the end I'd like the output to be written in the same folder structure and file names, i.e. say I have 2 files; dir1/file1
and dir1/file2
. I want the following command:
task1 dir1/* | task2 | task3 --output dir2/${filename}
to output dir2/file1
and dir2/file2
which should represent the processed files.
Best Answer
Loop over the files in
dir1
, get the basename of each file, process it, write to output file:The variable substitution
${path##*/}
will take the value of thepath
variable and remove everything up to (and including) the last/
. This produces the basename of the file.task1
is then performed on the file indir1
, the result is passed on totask2
and the output of that is given totask3
. The last task usesdir2/$name
as the operand for its--output
switch, where$name
is the filename without path from the$path
valiable.