Bash – How to write output to a file of the same name as the input

bashcommand lineshell-script

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:

for path in dir1/*; do
    name="${path##*/}"
    task1 "$path" | task2 | task3 --output "dir2/$name"
done

The variable substitution ${path##*/} will take the value of the path variable and remove everything up to (and including) the last /. This produces the basename of the file.

task1 is then performed on the file in dir1, the result is passed on to task2 and the output of that is given to task3. The last task uses dir2/$name as the operand for its --output switch, where $name is the filename without path from the $path valiable.

Related Question