How to process several files with a bash for loop

Posted on February 10, 2017
Tags: howto, bash

A quick example of a bash for loop to process files.

Here is a stupid artificial example on how to run a bash for loop on all .tex files and output their filenames with changed file extensions .pdf. The lines starting with echo show three different ways to change the filename string.

for i in $(ls *.tex);
do
    echo "${i%.tex}.pdf"
    echo "${i/.tex/.pdf}"
    echo $(echo "$i" | sed s/.tex/.pdf/)
done

It might be handy to rename and/or convert certain file types. For recursive listing one could use the glob star ‘**/*.tex’ (in case of the installed package or zsh) or l

find . -name "*.tex"