Check file exists from a shellscript


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check file exists from a shellscript
# 1  
Old 04-23-2009
Check file exists from a shellscript

Hi,

I have a list of files that I want to check to see if they exist and then count how many of these files exist, I also want to do the same for the files that arent found.

I have done this by creating temp files see below but want ot do this using variables instead:

Code:
for FILE in $FILELIST
do
ls -l $OKAYDIR/$FILE >>countload.txt 2>/dev/null
done

LOADCOUNT=`cat countload.txt|wc -l`
cat countload.txt >>results.txt

echo >>results.txt
echo "$LOADCOUNT files loaded">>results.txt

Thanks.
# 2  
Old 04-23-2009
I'd do something like this (in ksh):-


Code:
foundc=0
nfoundc=0

for file in $FILELIST
do
   if [[ -s "${file}" ]];then
       # file is found and is > 0 bytes.
       foundc=$(( found + 1 ))
       ...do something else you want...
   else
       # file is not found or is 0 bytes
       nfoundc=$(( nfounc + 1 ))
       ...do something else you want...
   fi
done

print "number of files in [$FILELIST] found = [${foundc}]\n"
print "number of files in [$FILELIST] NOT found = [${nfoundc}]\n"

You can change the -s test for -r (file is readble) -e (file exists) -d (file is directory etc) as you see fit.
# 3  
Old 04-23-2009
Thanks, how would I go about putting the found files and not found files into other variables which I could then echo out?
# 4  
Old 04-23-2009
Code:
foundc=0
nfoundc=0
fflist=""
nflist=""

for file in $FILELIST
do
   if [[ -s "${file}" ]];then
       # file is found and is > 0 bytes.
       foundc=$(( found + 1 ))
       fflist="${fflist}\n${file}"
       ...do something else you want...
   else
       # file is not found or is 0 bytes
       nfoundc=$(( nfounc + 1 ))
       nflist="${nflist}\n${file}"
       ...do something else you want...
   fi
done

print "List of found files:\n\n${fflist}\n---------"
print "List of NOT found files:\n\n${nflist}\n-----"

print "number of files in [$FILELIST] found = [${foundc}]\n"
print "number of files in [$FILELIST] NOT found = [${nfoundc}]\n"

I've not tested above so maybe couple of bugs.

You could of course save the outputs to a file instead of a variable.

NOTE: accessing the variable list of files will contain \n whereas printing it out should put each entry on new line.
# 5  
Old 04-23-2009
Thanks, how would I go about putting the found files and not found files into other variables which I could then echo out?
# 6  
Old 04-23-2009
I've just answered that with $fflist and $nflist. Smilie

They should contain a list of files found and a list of files not found.

Let me know if it doesn't work
# 7  
Old 05-28-2009
for i in $1
do
if [ ! -f $i ]; then
let countValue=1
fi
done
This is the code i am using and it is not working. where $1 is my txt file name

Thanks
Supriya
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if file exists

I need to check whether a file exists and has been changed. The file should contain a specific string. The file should also have been changed within the last ten seconds. How do I do that? (3 Replies)
Discussion started by: locoroco
3 Replies

2. Shell Programming and Scripting

To check if file exists

Hi, I have the below code written. However I am not getting the desired output I am checking if the particular path has file in it. #!/bin/bash ls -l /IRS2/IRS2_ODI/INFILE/*LS* 1>/dev/null 2>/dev/null if then echo $? echo "File Exists" fi ... (3 Replies)
Discussion started by: Shanmugapriya D
3 Replies

3. Shell Programming and Scripting

Check if file exists or not

Hi, I want to check if the file exists or not in the directory. i am trying below code but not working. File="/home/va59657/Account_20090213*.dat" echo "$File" if ]; then echo "file found" else echo "file not found" fi However i am getting file not found even if file exits as... (5 Replies)
Discussion started by: Vivekit82
5 Replies

4. Shell Programming and Scripting

How to check if the file exists in directory?

Hi Gurus, I have a requests to find if all the file in the filelist exist in certain directory. example: my filelist abc def ddd cde afg how can I find these 5 files exists at director /home/abc Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

5. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

6. Shell Programming and Scripting

how to check to see if a file exists?

I want to write a script to see if various files exist. What I want to do is have the script search in various directories if a file exist, and if not, then output something like "/path/file does not exist". I don't actually know of how to check and see if a file exists or not. What I have in mind... (2 Replies)
Discussion started by: astropi
2 Replies

7. Shell Programming and Scripting

Check to see if a file exists?

Hi. I'd like to have an IF-Then-Else statement where I can check to see if a file exists? We have the Bourne Shell by default. I'm looking for the syntax to do something like this: if myfile.txt exists then ...my code else ...my code end if Any help would be greatly... (5 Replies)
Discussion started by: buechler66
5 Replies

8. Shell Programming and Scripting

Script to check if file exists

guys, I am trying to write a script that does the following: it looks for a file in a specific directory and if the file is not there (NOT), it emails me. I have tried the following but its not working. It simply hangs up. Please help. if then mail -s 'blah blah blah' my email... (4 Replies)
Discussion started by: basisvasis
4 Replies

9. Programming

Check if file exists + ulp :S

Hi all! I know that this may not be the best forum to ask ulp and eagle related question. But the guys from eagle didn't know the answer and this is the BEST programming forum I know :D How can I check if a file exists while programming eagle's ulp's? There is not much information on-line.... (2 Replies)
Discussion started by: ruben.rodrigues
2 Replies

10. Shell Programming and Scripting

check if remote file exists

Hi Does anybody know how I can check if a file exists on a remote machine i.e. see bellow, this doesn't work by the way and if tried countless variations on this #!/bin/sh hostname=server56 if ; then echo file exists else echo file doesn't exist fi Any help on this would... (2 Replies)
Discussion started by: hcclnoodles
2 Replies
Login or Register to Ask a Question