I'm trying to figure out how to check if the name of a file exists in a .txt document.
I have the file list.txt which contains:
001 jane jane_profile_07272018.doc 002 kwood kwood_profile_04022017.doc 003 pchow pchow_profile_05152019.doc 004 janez janez_profile_11042019.doc 005 jane jane_pic_07282018.jpg 006 kwood kwood_pic_04032017.jpg 007 pchow pchow_pic_05162019.jpg 008 jane jane_contact_07292018.csv 009 kwood kwood_contact_04042017.csv 010 pchow pchow_contact_05172019.csv I need to check whether the files related to "jane" exist in the filesystem or not.
While trying to figure it out I wanted to just check if I could make it work by only searching for the .doc but I can't figure out the if expression to use.
files=$(grep "jane " list.txt | cut -d " " -f3) if [ -e *.doc "$files" ]; then echo "File exists" fi This gives the error: "main.sh: line 5: [: jane_profile_07272018.doc: binary operator expected"
I've spent an embarrassing amount of time trying to figure this out so any help is much appreciated.
EDIT: With some help from a friend we figured it out and this code works best for what I'm trying to do.
#!/bin/bash > oldFiles.txt files=$(grep " jane " list.txt | cut -d' ' -f3) for f in $files; do if [ -e $f ]; then echo $f >> oldFiles.txt; fi done