Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

learn linux and unix commands - unix shell scripting

check if remote file exists

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-29-2007   -   Original Discussion by hcclnoodles
hcclnoodles's Unix or Linux Image
hcclnoodles hcclnoodles is offline
Registered User
 
Join Date: Mar 2002
Last Activity: 20 January 2016, 4:16 AM EST
Posts: 303
Thanks: 1
Thanked 1 Time in 1 Post
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




Code:
#!/bin/sh

hostname=server56

if [ -f `ssh $hostname '/usr/local/file'` ]; then
echo file exists
else
echo file doesn't exist
fi

Any help on this would be greatly appreciated
Sponsored Links
    #2  
Old Unix and Linux 06-29-2007   -   Original Discussion by hcclnoodles
aigles's Unix or Linux Image
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 1 December 2014, 9:03 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 62 Times in 58 Posts
One way (not tested):



Code:
#!/bin/sh

hostname=server56

if [ -n `ssh $hostname 'ls /usr/local/file 2>/dev/null'` ]; then
echo file exists
else
echo file doesn't exist
fi

Sponsored Links
    #3  
Old Unix and Linux 08-27-2008   -   Original Discussion by hcclnoodles
mikemark's Unix or Linux Image
mikemark mikemark is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 25 October 2010, 10:34 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Here's another way:



Code:
#!/bin/sh

#
# Name:
#       testfile
#
# Usage:
#       ./testfile "/dir/and file to test for.txt"
#
# Notes:
#       Quotes needed when testing for names with whitespace, etc.
#       Be certain to:
#               > chmod 700 testfile
#       and perform any setup needed for ssh such as key generation and
#       proper placment on the remote computer
#

hostname=mike@ram

echo $hostname
if ssh $hostname 'ls "'$1'" >/dev/null'; then
        echo "file '"$1"' exists";
        echo "byebye";
        exit;
else
        echo "file '"$1"' doesn't exist";
fi
echo "finito!"

Sponsored Links
Closed


Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
csh Check if file exists on remote system meteorologistks Shell Programming and Scripting 3 10-07-2011 12:40 PM
Check file exists on remote machine. marpadga18 Shell Programming and Scripting 2 10-24-2010 03:41 PM
ftp - check if file on remote exists (skip overriding) spiriad UNIX for Dummies Questions & Answers 3 07-30-2010 06:25 AM
check if file exists on remote system ? hcclnoodles Shell Programming and Scripting 2 10-26-2006 05:08 AM
Check Remote Folder Exists borncrazy Shell Programming and Scripting 1 07-12-2004 05:15 PM



All times are GMT -4. The time now is 11:21 AM.