how to check files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to check files
# 1  
Old 12-12-2011
how to check files

Hi,

I have a dir in which I need to check for 3 files. and naming for three files are as below.

Code:
fileone_yyyy_mm_dd.dat
filetwo_yyyy_mm_dd.dat
filethree_yyyy_mm_dd.dat

and YYYY_mm_dd will change everyday as the date changes. I need to check everyday all these files are existing or not (irrespective of dates)

I am following the below approach, but some how I feel there could be much better way.

Code:
filecount=`ls dir1|grep -c "fileone"*`
if [ filecount -gt 0]; then
 echo "fileone ixists"
fi

like this I am writing for all 3 files.

Is there any other way something I can get the same result?
(may be using find command?)

please suggest

Last edited by methyl; 12-12-2011 at 06:33 PM.. Reason: added code tags
# 2  
Old 12-12-2011
You don't need to use find, ls, grep, or globbing at all. Your use of grep is wrong anyway since grep uses regular expressions, not globbing -- "fileone*" matches "fileon", then one or more e's. and does so anywhere in the line, so "aaaaaafileon" would match "fileone*".

The shell has many operators for use inside [ ] which can be used to check files, directories, or variables in many ways, see man test.

Code:
# Today's date
DATE=$(date +%Y_%m_%d)

for X in fileone filetwo filethree
do
        [ -f "${X}_${DATE}.dat" ] && echo "${X} exists"
done

# 3  
Old 12-12-2011
thanks for your inputs. I will have only today'sfiles only, in other words everday the old files will be archived and only current day files will be present in that dir.

so, as per your code, it should be

Code:
[ -f "${X}*.dat" ] && echo "${X} exists"

is it correct?

Last edited by methyl; 12-12-2011 at 06:34 PM.. Reason: added code tags
# 4  
Old 12-12-2011
No. The code as I gave it is what I meant. No more, no less.

Code:
# Today's date
DATE=$(date +%Y_%m_%d)

for X in fileone filetwo filethree
do
        [ -f "${X}_${DATE}.dat" ] && echo "${X} exists"
done

Your version won't work. * won't expand inside "", and if you remove the "", it will start complaining when * matches more than one file because -f doesn't take multiple files.

If you found a way to make it work, your error-checking code still wouldn't see any difference between files from previous days and files from the current day, making it fairly pointless.
# 5  
Old 12-12-2011
Another Shell way:

Code:
YMD=$(date +%Y_%m_%d)
#
if [ -f "fileone_${YMD}.dat" -a -f "filetwo_${YMD}.dat" -a -f "filethree_${YMD}.dat" ]
then
        echo "All three files exist
else
        echo "One of more files is missing"
fi


If I read the question another way, the match on today's date doesn't matter.
Code:
filecount=$(ls -1d fileone_????_??_??.dat filetwo_????_??_??.dat filethree_????_??_??.dat 2>/dev/null | wc -l)
if [ $filecount -ne 3 ]
then
        echo "one or more of the three files is missing"
fi


Last edited by methyl; 12-12-2011 at 02:09 PM.. Reason: tweak
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get CRC check sum of files in java EAR file without extracting .jar/.war files to disk.?

unzip -v gives CRC info of each file in a zip(in my case .EAR) file. # unzip -v my-application.ear Archive: my-application.ear Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 197981 Defl:N 183708 7%... (1 Reply)
Discussion started by: kchinnam
1 Replies

2. Shell Programming and Scripting

Check files and archive the files using sftp

Hi, I have to check the files in another server using sftp to do that, below is the code i am going with #!/bin/bash export SRC_FOLDER=$1 export ARC_FOLDER=$2 HOST=as07u3456 USER=relfag sftp ${USER}@${HOST} <<EOF cd $SRC_FOLDER/DSCOR ls bye EOF echo "done" whatever the files i... (8 Replies)
Discussion started by: ursrami
8 Replies

3. Shell Programming and Scripting

Check for data between two files

I have two files File1.txt 000199458 000199463 000200442 000200831 000200866 000201009 000201050 000201405 000201666 000201682 File2.txt (4 Replies)
Discussion started by: halfafringe
4 Replies

4. Shell Programming and Scripting

Kindly check it: Camparison of files only column1 of 2 files

Hi all, I have 2 files in which i have to find commom entries in column 1 an dif soemthing is common write other data of both files in front of it mentioned. Gene symbol and disease name column 1 column2 ARFGEF2 CAD DDEF2 CAD PSCD3 CAD PSCD4 CAD CAMK1... (15 Replies)
Discussion started by: manigrover
15 Replies

5. Shell Programming and Scripting

Perl code to check date and check files in particular dir

Hi Experts, I am checking how to get day in Perl. If it is “Monday” I need to process…below is the pseudo code. Can you please prove the code for below condition. if (today=="Monday" ) { while (current_time LESS THAN 9:01 AM) ... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

6. Shell Programming and Scripting

need a shell script to extract the files from source file and check whether those files existonserve

Hi, I am new to shell scripting.Please help me on this.I am using solaris 10 OS and shell i am using is # echo $0 -sh My requirement is i have source file say makefile.I need to extract files with extensions (.c |.cxx |.h |.hxx |.sc) from the makefile.after doing so i need to check whether... (13 Replies)
Discussion started by: muraliinfy04
13 Replies

7. Shell Programming and Scripting

How to check files and move the results to differents files?

Hi, I am a newbie to shell scripting. here is my objective: 1)The shell program should take 2 parameters - ie-> DestinationFolder, WebFolder 2)Destination folder contains few files that has to has be verified and deleted. 3)WebFolder is a folder containing a list of master files 4)It... (1 Reply)
Discussion started by: sandhyagupta
1 Replies

8. Shell Programming and Scripting

check whether 3 files are present

I'm trying to check whether 3 files are existing and send 3 files as attachements. If only two are there then send those two files as attachments. if ; then elif ; then I tired the above given syntax and then it is giving me an error line 11: ' I tried with -a instead of && and... (3 Replies)
Discussion started by: Celvin VK
3 Replies

9. Shell Programming and Scripting

Check 2 files ???

Hi all, I know how to count lines, count byte of a file, but really dont know how to compare line , I am newbie and hope you can help me to learn more about UNIX. Here is my problem. ==== How : 1. File A (Flatfile with NAME|DATE|ID1|ID2|ID3|ID4|ID5)... (3 Replies)
Discussion started by: sabercats
3 Replies

10. Solaris

How to check no. of files open currently

I'm getting an error "too many files open" # ulimit -a time(seconds) unlimited file(blocks) unlimited data(kbytes) unlimited stack(kbytes) 8192 coredump(blocks) unlimited nofiles(descriptors) 256 memory(kbytes) unlimited # hard limit shows 1024 I would like to know how many files... (1 Reply)
Discussion started by: max_min
1 Replies
Login or Register to Ask a Question