-1

i have some files in the directory:

ABC_file1_2020-10-01.txt
ABC_file1_2020-01-02.txt
ABC_file2_2020-11-01.txt
ABC_file2_2020-11-02.txt
CDE_file1_2020-11-01.txt
CDE_file1_2020-11-02.txt
CDE_file1_2020-11-03.txt ....

I want to use bash script in centos and find the files with prefix as ABC and date greater than 2020-11-01. i have written code like this: to find if prefix matches with file

#!/usr/bin/bash
read -p "enter prefix of a file : " prefix
for file in *; do
     if ["$file" = *"$prefix"*]; then
              echo $file
     fi
done

since I am new to this, I wanted to do it step by step and first wanted to find the file name that matches with the given prefix and later I thought of doing the condition for checking the date inside the if condition. but I don't understand where I have gone wrong. I am getting error in line 7. let me show the screen shot of error. here it is, error at line

I am new to this scripting. it would be an immense help if someone helps me with this error and finding the files with the prefix and the date greater than 2020-11-01.

  • Please edit your question and post the code and errors as text. There's no easy way to try code in an image ourselves, nor can we we copy it into our answers, and it won't show up in future Google searches. See: [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/q/285551/68587) – John Kugelman Nov 26 '20 at 06:48

1 Answers1

0

Do use the find command:

find . -name '^ABC'
TheSlater
  • 642
  • 1
  • 4
  • 11
  • if I use this inside the if statement like if [find . -name '^ABC'] which is checked while iterating through the files in directory using for loop. its popping an error saying find: command is not found. – Meghana avutu Nov 26 '20 at 07:26
  • Oh the find command search for all files starting at `.` in our case. No need for the loop anymore, the line itself works. Try it from the command line. – TheSlater Nov 26 '20 at 08:35