How to check for file name of specific format using find?

I have to find the specific formatted file is present in the received list in the directory, for which I have written:

 
file_list=`ls -lrt /tmp/vinay/act/files |grep "$cdate"| awk '{print $9}'`
while read fileStr 
do
find $file_list $fileStr > /dev/null
 status=`echo $?`
 if [ $status != 0 ] ; then
 
  echo "File not found "
  exit 634
 else
  echo "File found "
 
 fi
done < files.txt

and my files.txt file contains some regular expr. of the file names to check for in the received list of files, like:

 
avc_cd_*.[txt|done]
asd_c_ds_cons_*.[txt|done]
.
.
.

In the above code, how to check whether file received is of the format as per files.txt file. Please help.

Thank you.

---------- Post updated at 07:24 AM ---------- Previous update was at 04:12 AM ----------

Hi.,

Is there any way to specify to the read command, that the file it is reading contains file names with regular expression for the above example?.

Kindly help.

Thank you.

Hi,

Try this...
I have change the RegExp in the input file.

Input file.
avc_cd_.*\.(done|txt)
asd_c_ds_cons_.*\.(txt|done)
 
Perl Script

#!/usr/bin/perl

$cdate="June 9";
$file_list=`ls -lrt /root/ |grep "$cdate"| cut -c 47- | tr "\n" " "`;

while (<>) {
chomp;
if ($file_list =~ /$_/) { print " found - $& \n"; }
}

perl perlscript inputfile

Actually I tried with perl script which you have given, but its not working out the way as it has to be, its showing - found once I execute the script and press enter key;

Can I get a solution using unix shell scripts?

Thanks.,

If you're determined to do this in the shell rather than in Perl (and I don't mean a thin Perl wrapper around a pipe of shell commands, I mean a proper Perl implementation), you might try something conceptually like the following. The key is remembering that you already have a perfectly good list of regexps:

ls -lrt /tmp/vinay/act/files |grep "$cdate"| awk '{print $9}' > file_list
for f in `cat files.txt`; do grep "$f" file_list; fi

Instead of running each filename in turn past all of the possible regexps, just dump all the filenames into a file, then grep that file for the "valid" regexps. The output of the above will be a list of all of the filenames matching one or more of the regexps in files.txt, then you can then pipe that list through sort -u to clean it up.

Now, all that said, your ls pipe method here is inefficient, and by using ls, you're throwing away the information on where the files you match actually are. There's a tool for doing this job in a single operation, and that tool is find. Half the secret of doing anything well on Unix is knowing the right tool to use.

I infer you have a large list of valid regexps in that 'files.txt' file, so you're probably going to want to generate the find command programmatically. That's something you could do either in Perl or in the shell. You're going to want to construct something that looks basically like this:

find /tmp/vinay/act/files -ctime [or possibly -cmin] [time period here] -a \( -name regexp1 -o -name regexp2 -o -name ... -o -name regexpn \) -print

You'll need to read find.1 and determine whether you need to use -cmin or -ctime. What this will do is, in a single operation, return to you the full pathnames of all files under /tmp/vinay/act/files created during the specified time period and matching one of more of your target regexps.

It's just like anything else really: Know what the available tools are, and use the right tool for the job.