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
deepak
  • 21

1 Answers1

3

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

while read -r file; do
    [ -f "path/$file" ] || echo "$file"
done < test.txt
Ipor Sircer
  • 14,546
  • 1
  • 27
  • 39
  • 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 04:32