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

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`
    if [ ! -f path/$file ];
    then action item

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

while read -r file; do
    [ -f "path/$file" ] || echo "$file"
done < test.txt
| improve this answer | |
  • 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.