Check if file exist


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if file exist
# 1  
Old 05-11-2009
Check if file exist

Hi

Does anybody know how I can check if a file exists

i.e. see bellow, this doesn't work by the way and if tried countless variations on this

file1=$one/file111.txt
if [ -f $file1 ]
then
echo "Present"
else
echo "Not present"
fi

result :
Not present
(file is already present, eventhough its showing Not present....)

also have tried with -d & -z option not working....
# 2  
Old 05-11-2009
Worked for me.

Code:
one="/home/myuser"
file1=$one/file1.txt
if [ -f $file1 ]
then
echo "Present"
else
echo "Not present"
fi

Code:
# sh test.sh
Not present
# touch /home/myuser/file1.txt
# sh test.sh
Present



EDIT: Oh my bad on a remote machine??? Are you using nfs to mount the directory? You will need to connect to the remove machine some how, nfs, samba, ssh, etc.....
# 3  
Old 05-11-2009
below is my detailed issue about the query...

Last edited by gksenthilkumar; 05-11-2009 at 08:15 PM..
# 4  
Old 05-11-2009
I have created two scripts, one with hardcoded and another one with extract from file instead of hardcoded,

script:1
--------
#!/bin/ksh
filename="$one/file1.dat"
if [ -f $filename ]
then
echo "$filename has arrived." >> $logfile
else
echo "$filename has NOT yet arrived." >> $logfile
fi
:>

Result:
$one/file1.dat has arrived.

script:2
--------
#!/bin/ksh
outfile=$HOME/outputfile.lst
splitfile=$HOME/splitfile.lst
infile=$HOME/inputfile.lst
rm -f $infile
rm -f $splitfile
rm -f $outfile
sqlplus -s << sqlEOF
$user/$pass
whenever sqlerror exit 1
set head off pages 0
set feedback off
spool $infile
SELECT files
FROM tab1;
spool off
sqlEOF
cat $infile | while read filename time
do
checkfile=$filename
echo $checkfile,$filename >> $splitfile
if [ -f $checkfile ]
then
echo "$checkfile has arrived." >> $logfile
else
echo "$checkfile has NOT yet arrived." >> $logfile
fi
done

Here after execute the script sh script2.sh

1. $infile contains,
$one/file1.dat 101010
$one/file2.dat 110011
$two/file1.lst 090909
$two/file2.lst 110011

2. splitfile shows,
$one/file1.dat,$one/file1.dat
$one/file2.dat,$one/file2.dat
$two/file1.lst,$two/file1.lst
$two/file2.lst,$two/file2.lst

3. outfile shows,
$one/file1.dat has NOT yet arrived.
$one/file2.dat has NOT yet arrived.
$two/file1.lst has NOT yet arrived.
$two/file2.lst has NOT yet arrived.

Here those above files are presents in UNIX directory still it shows NOT yet received and the same is working for script 1 (hardcoded with one single file) and script 2 is NOT working.

Please help me...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check for a file and touch if not exist

Hi, I've a situation where i need to check for the file existence and create a zero byte file based on the parameter,in some cases i need to touch and in some case i dont need to touch with zero byte file please help me on parameterizing this touch command?? Regards. San (2 Replies)
Discussion started by: sandeep karna
2 Replies

2. Shell Programming and Scripting

check file exist before execution

Hi , I have a scripts which run at every 1 min and do some job. this scripts look for the file in the directory and move in the other directory. I want to write a line which forst check if the *.LOG file exist in the directory if *.LOG exist then do for i in *.LOG load ... (7 Replies)
Discussion started by: guddu_12
7 Replies

3. Shell Programming and Scripting

how to check file exist in a directory or not

HI folks, can any one tell me how to check whether the file is existed in a directory or not . let me tell you my requirement : if the file is existed i should display a one message or else i have to send a mail .. i have the mail logic .. but I'm failed to check file existence .. please... (5 Replies)
Discussion started by: sravan008
5 Replies

4. Shell Programming and Scripting

Check if file exist

Hi, I created following script to check if file exist: #!/bin/bash SrcDir=$1 SrcFileName=$2 SrcTimePeriod=$3 if ;then echo 1 else echo 0 fi I ran it like: /apps/Scripts/FileExist.sh /apps/Inbox file1 2nd_period_2010 Even file exist at that location, my above command is... (4 Replies)
Discussion started by: palak08
4 Replies

5. Shell Programming and Scripting

Check if file exist

Hi, I am trying to create a bash script which will check if file exist then remove that file else do nothing. I have to do same process for three files in same script. I have written code for one file and trying to run it. if then rm -r /user1/abc/File1 fi When I run this code it... (1 Reply)
Discussion started by: palak08
1 Replies

6. Shell Programming and Scripting

Check file exist issue

I have created two scripts, one with hardcoded and another one with extract from file instead of hardcoded, script:1 -------- #!/bin/ksh filename="$one/file1.dat" if then echo "$filename has arrived." >> $logfile else echo "$filename has NOT yet arrived." >> $logfile fi :> Result:... (4 Replies)
Discussion started by: gksenthilkumar
4 Replies

7. Shell Programming and Scripting

Check Word if exist on file or not

Hello, I want to check if some word exist or not on some file By Example : word is : nixcraft file called : /root/shell.txt and i want to check if nixcraft word exist on /root/shell.txt file with if statement or another tool Any Ideas (5 Replies)
Discussion started by: LinuxCommandos
5 Replies

8. Programming

how to check if directory/file exist using c/c++

Hi there, how to check if directory/file exist using c/c++ under linux/unix. Thanks. Steven (2 Replies)
Discussion started by: steven88
2 Replies

9. Shell Programming and Scripting

how to check if directory/file exist using c/c++

Hi there,, how to check if directory/file exist using c/c++ under unix/linux? I can use access() under Window MFC. Thanks. Steven (1 Reply)
Discussion started by: steven88
1 Replies

10. UNIX for Dummies Questions & Answers

how to check if the file exist or not?

say i would like to check if the file is existed before i use rm command. How can i do it? i know if i can use find, but i would like to have a good interface (in a shell script) thks (3 Replies)
Discussion started by: gusla
3 Replies
Login or Register to Ask a Question