P. Geudens P. Geudens - 2 months ago 7
Bash Question

Linux shell: how to Iterate multiple file lists and take action on each line in the file?

I'm trying to iterate all files in a directory that have the FILELIST extension, which I manage to do. But then I would like to read the content of these files, containing paths and file names of other files. These files, I would like to move to another location.

FileA.FILELIST
/somepath/File1.csv
/somepath/File2.csv
FileB.FILELIST
/somepath/File3.csv
/somepath/File4.csv


What I have so far...

#!/bin/bash
# Iterate all file lists
for fl in /path/Inbox/*.FILELIST
do
#Iterate the content of the current file list
while read line;
do
#Move it to the Archive directory...
done < $fl
done


Thanks in advance !!

Answer

Your script looks good and with a few tweaks like below, should do the job for you. I have added conditions in read to handle special characters if available in the file you are reading from.

#/bin/bash

for file in /path/Inbox/*.FILELIST
do
    while IFS= read -r -d '' line;
    do
        echo "$line"

        # mv "$line" "$targetPath"
        # Do whatever else you want to do with the line here

    done < "$file"
done