How to check whether file is exist on remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check whether file is exist on remote server
# 1  
Old 07-14-2009
How to check whether file is exist on remote server

Hi all,

I am new to UNIX Scripting. I would like to know how to check whether file is exist in remote server. I have google, but cannot find any solution that works.

Currently my code is like this:
if [ -f "ssh userid@remotehost 'ls /home/batch/sample.txt'" ]; then
echo 'data file exist'
else
echo 'data file not exist'
fi


Thanks in advance... Smilie
# 2  
Old 07-14-2009
I'm not sure if you can do that, i might be wrong.
Another way to do this is to download the file through sftp and check the exit code
# 3  
Old 07-14-2009
Disclaimer: My point of reference is bash script.

As far as I know ssh doesn't send back return codes for scripts you execute remotely so your logic is a bit flawed there. Your test (if) is testing the ssh connection, I believe, so as long as you can ssh you'll test true. When you execute a command remotely with ssh you get back whatever is returned by the command so you need to execute a script on the remote server that looks for the file and returns what you want or just do it in a one-liner.

Code:
ssh user@remote 'if [ -f /home/batch/sample.txt ]; then echo "data file exists"; else echo "data file does not exist"; fi'

# 4  
Old 07-14-2009
Quote:
Originally Posted by mglenney
Disclaimer: My point of reference is bash script.

As far as I know ssh doesn't send back return codes for scripts you execute remotely so your logic is a bit flawed there. Your test (if) is testing the ssh connection, I believe, so as long as you can ssh you'll test true. When you execute a command remotely with ssh you get back whatever is returned by the command so you need to execute a script on the remote server that looks for the file and returns what you want or just do it in a one-liner.

Code:
ssh user@remote 'if [ -f /home/batch/sample.txt ]; then echo "data file exists"; else echo "data file does not exist"; fi'

Thanks mglenney... it works!! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to check via SSH and credentials if file on remote server exists?

Hi there, I am sorry to ask that kind of beginner thing, but all the code I found online didnt work for me. All I want to do is: Check via SSH if a File exists on my webserver. The SSH login has to be with username and password. So I would be very thankful if somebody could write the line.... (8 Replies)
Discussion started by: Jens885544
8 Replies

2. Shell Programming and Scripting

File Check in remote server

Hi Experts, I need a script in which I need to scp a file /home/chandan/abc.txt to a remote server using scp. Then I need to check whether scp is successful or not. How am I going to write this code? My Server Name: myserver File Name On My Server: /home/chandan/abc.txt Destination Server... (3 Replies)
Discussion started by: ChandanN
3 Replies

3. Shell Programming and Scripting

Urgent: File Check in remote server

Hi Experts, I need a script in which I need to scp a file /home/chandan/abc.txt to a remote server using scp. Then I need to check whether scp is successful or not. How am I going to write this code? My Server Name: myserver File Name On My Server: /home/chandan/abc.txt Destination Server... (1 Reply)
Discussion started by: ChandanN
1 Replies

4. Shell Programming and Scripting

Unable to check if file exists on remote server using expect

Hi, I need to check if a file exists on remote server using expect. #!/bin/bash ip_addr=10.10.10.10 user=root passwd=Help filename=/root/test expect -c " spawn ssh -n -T -o NumberOfPasswordPrompts=3 -o StrictHostKeyChecking=no $user@$ip_addr expect \"*?assword:*\" send --... (6 Replies)
Discussion started by: temp_user
6 Replies

5. Shell Programming and Scripting

ssh to remote server and check if file exists

Hi everyone, I am trying to figure out a way to ssh to remote server and check if file exists, and if it doesn't I want to leave the script with an exit status of 5. I have the following that I am attempting to use, but it is not returning anything: check() { ssh ${SOURCE_SERV} "ls -l... (4 Replies)
Discussion started by: jimbojames
4 Replies

6. 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

7. 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

8. Shell Programming and Scripting

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 then echo "Present" else echo "Not present" fi result : Not present (file is already present, eventhough its... (3 Replies)
Discussion started by: gksenthilkumar
3 Replies

9. Shell Programming and Scripting

Contantly check for file at the remote server and ftp when avaliable

Hello, I used the search engine but could not find the solution I am looking for. Probably its simple but I do not know the solution. My requirement is I ftp files every day in the morning from the remote server to the local machine. Now if the files are not there at the time I schedule the ftp... (2 Replies)
Discussion started by: kiran_418
2 Replies

10. Shell Programming and Scripting

How to check the file status in a remote server?

Hi All, Thanks in Advance. My requirement is there are some data files to be updated(some times new files get created) regularly in server A, these files are to be updated to the server B(backup server) by using SCP, I have to write a script for this purpose, before copying the files to server... (3 Replies)
Discussion started by: rajus19
3 Replies
Login or Register to Ask a Question