Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Processing a List of Files from an Input File

August 14, 2023 — Gregg Szumowski

Suppose we have a list of files stored in a text file and we want to perform an operation on each of them within a bash script. How would we go about doing that? Well, there are several options, here are a few.

$ cat input-files.txt
file-1.txt
file-2.txt
file-3.txt
file-4.txt
file-5.txt
file-6.txt
file-7.txt
file-8.txt
file-9.txt

Now, if we want to operate on each file in this list we can do something like this:

while IFS= read -r filename
do
echo "Do something on ${filename} here..."
done < "input-files.txt"

or alternatively,

input="input-files.txt"
while IFS= read -r filename
do
printf '%s\n' "${filename}"
done < "${input}"

Tags: cli, bash, shell-scripting, motd