|
|||||||||
| 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 |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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 | ||
|
|
![]() |
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 |
|
|