Test File Existance Remotely?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Test File Existance Remotely?
# 1  
Old 08-28-2009
Test File Existance Remotely?

Thanks in advance to anyone that can help me answer this:
I'm trying to write an if statement that will run test -f on whether a file exists on another server and if it does not then report that negative outcome to a log file.

I'm thinking it should look something like this:
if [ remsh server1 -f /tmp/file.dat ]; then
rcp server1:/tmp/file.dat local1:/tmp/
else
echo "File Not Found" >> table.log
fi


But when I run this I get: "ksh: server1: unknown test operator"

server1 is the name of the remote server and local1 is the server I'm running the script from and table.log is located on local1.
# 2  
Old 08-28-2009
Tools Can you use scp instead of rcp?

run "man test" to see what test flags you can use in your shell, -e may do what you want. Googling for "ksh test flags" may be quicker. Smilie

I use ssh for everyday administration tasks the syntax may be similar to rcp, I think I'm in the Bourne shell when I ssh in.

Code:
ssh user@server.domain.com 'hostname; if [ -f /tmp/file.dat ]; then echo "File Found"; else echo "File Not Found"; fi > /tmp/table.log'


Last edited by infosponge; 08-28-2009 at 02:40 PM..
# 3  
Old 08-28-2009
What does rcp do when the remote file doesn't exist? That may be good enough.

It's probably simpler - and definitely faster - to just do one remote command and just check the exit status of your rcp command.
# 4  
Old 08-28-2009
Code:
RET=`rsh <host> 'test -f <file>; echo $?'`     (Use single quotes)
if [ $RET -eq 0 ]
then
  rcp
else
  echo error
fi

# 5  
Old 08-28-2009
if the object of the exercise is to make sure the remote file exists AND the remote file is not that big (> 1MB or therabouts), it might be easier to just copy the file over every time just like what achenle suggested ...

if the object of the exercise is to make sure the process on the remote server is creating the remote file, then ...

you can try this one ....

assumes the remsh call to rem_server will always succeed ...

Code:
#! /bin/ksh
remsh rem_server "ls -l /dir/file" > /tmp/$$
if [ ! -s /tmp/$$ ]
then
     echo "rem_server" >> /dir/missing.log
else
     rcp /dir/file rem_server:/dir/file
fi


test and retest before putting into production ....
# 6  
Old 08-28-2009
I've been trying to do the same thing in a script that goes out to many servers to check on the existence of a particular file or directory. The problem I have is that I need to respond to the "enter password" prompt and using a "here document" to supply the password will not work for this. Is there anyway I can respond to the prompt in the script automatically?

Thanks....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking Multiple File existance in a UNIX folder(Note: File names are all different)

HI Guys, I have some 8 files with different name and extensions. I need to check if they are present in a specific folder or not and also want that script to show me which all are not present. I can write if condition for each file but from a developer perspective , i feel that is not a good... (3 Replies)
Discussion started by: shankarpanda003
3 Replies

2. Shell Programming and Scripting

Csh/tcsh : Check the file existance and run the script

Hi, I've to wait until a file generated and once its generated, source another script in Linux terminal. Please help me as this is very very urgent. The code should be something like if ( -e "/abc/xyz/a.txt ) source aaa.csh else sleep This should be repeated till the if... (4 Replies)
Discussion started by: kumar_eee
4 Replies

3. Shell Programming and Scripting

Error while checking file existance

Kindly help on below script: << i='find ...' if then echo 'File Exists' else echo 'File Does Not Exist' >> If i has some file name then it runs properly but if i has nothing ( blank value) then it throws an error. I dont exactly remember right now but error seems like:... (2 Replies)
Discussion started by: ravigupta2u
2 Replies

4. Shell Programming and Scripting

[Solved] Know the existance of a file on remote server

In a Korn shell, I want to test whether a file exists on a remote shell. If I use ssh <Remote server> ls abc It gives file proper result, but unable to capture in the script. Any help? Thanks Please use next time code tags for your code and data (3 Replies)
Discussion started by: Soham
3 Replies

5. Shell Programming and Scripting

Scripting to check the size of file and it's existance.

Hi, I am totaly new to create a script . Please help. I have file name retrived from SAP table into a internal table . Like :- /home/td_8d02_int_data_IPCL/ILLUSTRATIONS/CGM/l_pc_112138_01_0_01_00.cgm /home/td_8d02_int_data_IPC-L/ILLUSTRATIONS/CMP/l_pc_112138_01_0_01_00.cmp Objective... (1 Reply)
Discussion started by: amitkumar.b2
1 Replies

6. UNIX for Dummies Questions & Answers

Test existance of a file

Hi, I need to find out if a particular file exists and i am using if with -e option. Scenarion is like There is a possibility of two files having nomaincluture like below First file = abc20101028.somthing Second File = abc20101028.somthing.done I need to check abc20101028.somthing... (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

7. UNIX for Advanced & Expert Users

speed test +20,000 file existance checks too slow

Need to make a very fast file existence checker. Passing in 20-50K num of files In the code below ${file} is a file with a listing of +20,000 files. test_speed is the script. I am commenting out the results of <time test_speed try>. The normal "test -f" is much much too slow when a system... (2 Replies)
Discussion started by: nullwhat
2 Replies

8. Shell Programming and Scripting

check the file existance

Hello, I have two files .. 1. inventory_i.txt 2. inventory_b.txt I want to check if these two files exists. If exists, then do the process, otherwise, quite... if ; then echo "exists" else echo " does not exists" fi The above logic is not working... It is always, displaying Does... (2 Replies)
Discussion started by: govindts
2 Replies

9. UNIX for Dummies Questions & Answers

Checking file existance by extension

I'm sure this can't be too tough, but. I want to check the existance of file(s) by extension. There may be 0,1,N files. If any files exist, then remove them. Thanks for any insight. (3 Replies)
Discussion started by: niswonp
3 Replies

10. Shell Programming and Scripting

File existance

Guys Iam trying to do a script to find what files are missing in particular directory.I came up with this for i in file6 file7 file8 file9 file10 file11 file12; do if ; then echo " $file not in /mnt/Server/base/i386/ " else echo " $file ... " fi done Iam trying to print... (2 Replies)
Discussion started by: coolkid
2 Replies
Login or Register to Ask a Question