0

I want to do search through a bunch of directories to find missing files. The format of the directories and files is YYYY/MM/file_name_YYYYMMDD.csv. So what I want to do is something like:

for date in `list_of_dates.txt`
do
 if [! -f /YYYY/MM/file_name_YYYYMMDD.csv]
   echo $date
 fi

What I can't figure out how to do is make each iteration look for the right file in the right directory. Is there a way to do this in a bash script?

  • The question is a bit uncertain to me. How do you define a "missing file"? Do you want to list all files that do not follow the pattern you described? – vatsug Jan 27 '17 at 15:52
  • Dose the file contain only the filenames on the format file_name_YYYYMMDD.csv or is there a path as well? – Kusalananda Jan 27 '17 at 16:09
  • The list_of_dates.txt file, what does it contain? – Kusalananda Jan 27 '17 at 16:11
  • list_of_dates.txt is a text file that just has a list of strings representing the dates for which I expect there to be a file. A file is "missing" if I expect there to be a file for that date, but can't find a file matching the pattern. – Gree Tree Python Jan 27 '17 at 16:17
  • @GreeTreePython It's difficult to write a solution when the data is unknown. A date can have many many different formats... – Kusalananda Jan 27 '17 at 16:18
2
for dir in ????/??; do
    year=${dir:0:4}
    month=${dir:5}
    days=$(cal "$month" "$year" | awk 'NF {DAYS = $NF}; END {print DAYS}') 
    for date in seq 1 "$days"; do
        filename="file_name_${year}${month}${date}.csv"
        if [[ ! -f /"$year"/"$month"/"$filename" ]]; then
            echo "$filename is missing."
        fi
    done
done
  • cool +1 or even days=$(($(cal $mon $year | wc -w) -9)) – JJoao Jan 28 '17 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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