1

I have a file test.txt which contains file names like below where some file names will have spaces and some will not.

Mon - Tue corrected item.csv
Sat -Sun incorrect item.csv
Wed_THU_corrected_item.csv

Now I have a script where I have a for loop that is intended to find these files which are listed in test.txt at a particular "path" where files will come daily just to make sure files exist. So please let me know how I can match such files that have spaces in their name.

for file in `cat test.txt`
do
    if [ ! -f path/$file ];
    then action item
    fi
done

marked as duplicate by G-Man, agc, peterh, Sebastian, roaima Dec 20 '17 at 11:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

Never use cat for loops! Use while read...:

while read -r file; do
    [ -f "path/$file" ] || echo "$file"
done < test.txt
  • can you explain the usage of done < test.txt and also path is the place where file will come in very same format which are contained in test.txt.Also when we use while read -r file , do we have to mention which file while loop is going to read – deepak Dec 20 '17 at 4:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.