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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check for file name of specific format using find?
# 1  
Old 06-09-2010
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:

Code:
 
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:

Code:
 
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.
# 2  
Old 06-09-2010
Hi,

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

Code:
Input file.
avc_cd_.*\.(done|txt)
asd_c_ds_cons_.*\.(txt|done)

Code:
 
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"; }
}

Code:
perl perlscript inputfile

# 3  
Old 06-09-2010
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.,
# 4  
Old 06-10-2010
Quote:
Originally Posted by IND123
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?
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.

Last edited by Phil Stracchino; 06-10-2010 at 02:04 PM.. Reason: More to add that I was too tired to add last night
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To find files having filename containing specific date format

Hi, I have a requirement to create a shell script(tcsh) that finds all the files in a directory having the file name containing date format "YYYYMMDDHHMM" and extract the date time part ""YYYYMMDDHHMM" for further processing. Could you please have any idea on this. trades_201604040000.out... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

2. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

3. Shell Programming and Scripting

Copy UNIX File into a DSN with a specific Format

Hi All , I am new to programming and here is what i am trying to achieve , i am taking a list of mounted filesystems (based on the HLQ) , passing it on to a txt file and then copying the file to a DSN .The code i am using : df | grep WAST.*WASCFG.*ZFS | awk '{print $2}' | sort -o log.txt |... (5 Replies)
Discussion started by: AnjanM
5 Replies

4. Shell Programming and Scripting

script to arrange file in specific format

Hi All, I am new to forum, I am looking to arrange a file in specific format but unable to get the formula to do it, already googled for the same, but didnt find the answer :(. hope to get help here :o:o:o:o:o I have to files : $ cat Dev_List2 0685 0686 0687 0688 0689 068A 068B 068C... (2 Replies)
Discussion started by: prasan_Aix
2 Replies

5. Shell Programming and Scripting

Extracting content from a file in specific format

Hi All, I have the file in this format **** Results Data **** Time or Step 1 2 20 0.000000000e+00 0s 0s 0s 1.024000000e+00 Us 0s 0s 1.100000000e+00 1s 0s 0s 1.100000001e+00 1s 0s 1s 2.024000000e+00 Us Us 1s 2.024000001e+00 ... (7 Replies)
Discussion started by: diehard
7 Replies

6. Shell Programming and Scripting

How to check file name format using shell script?

Hi, I am writting a script, which accepts input file as parameter. Input file name is aa_bb_cc_dd_ee.<ext> I need to check that input file name should be of 5 fileds. Please help me out. :confused: (7 Replies)
Discussion started by: Poonamol
7 Replies

7. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

8. UNIX for Dummies Questions & Answers

File format check

How to check if file is in a given format? For instance: if file records are delimeted with "|" ( pipes) and have exactly 26 fields? File is pretty big (~3 mil reccords), so not sure if I have to check all records or just head/tail records or smth. Any ideas are much much more than... (11 Replies)
Discussion started by: Leo_NN
11 Replies

9. Shell Programming and Scripting

extract specific data from xml format file.

Hi, I need to extract the start time value (bold, red font) under the '<LogEvent ID="Timer Start">' tag (black bold) from a file with the following pattern. There are other LogEventIDs listed in the file as well, making it harder for me to extract out the specific start time that I need. . .... (7 Replies)
Discussion started by: 60doses
7 Replies
Login or Register to Ask a Question