P. Geudens P. Geudens - 1 year ago 90
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.


What I have so far...

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

Thanks in advance !!

Answer Source

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.


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

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

    done < "$file"