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


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
Old 2 Weeks Ago
Quote:
Originally Posted by Jens885544
But maybe you can give me a hint: I now build in the working code into my script. The script does a foreach on 7000 files. Is it a recommended way to do
"if ssh 'ssh-user0815@example.com' stat $FILETOCHECK \> /dev/null 2\>\&1"
on every file? I ask because to me it looks like it builds then 7000 times a ssh connection.
Is there a way to build it once and then check the files?
I'd have a local file with 7,000 filenames in it:
Code:
/path/to/file1
/path/to/file2
/path/to/whatever
etc

And run ssh once:

Code:
ssh username@host 'while read -r FILE ; do [ -e "$FILE" ] && echo "$FILE exists" || echo "$FILE missing" ; done' < fileslist

# 9  
Old 2 Weeks Ago
For that many files, an ssh connection for every single one is pointless unless they all are on different servers. Should they be on one or few servers, and you just need to check their existence, why not use the sftp command, which is part of the ssh suite, to get a remote directory listing and evaluate that locally? Like
Code:
sftp user@host <<< "ls -1 $DIRTOCHECK" > filesonserver

If there are more than one remote directories to be searched, try using a "batchfile" containing the ls commands for the target directories, like
Code:
sftp -b batchfile user@host  > filesonserver

After that, on the local node, you can compare the results against the list of necessary files.

Last edited by RudiC; 2 Weeks Ago at 02:24 PM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Check if file exists via ssh in ssh (nested) say170 Shell Programming and Scripting 2 10-23-2014 04:31 PM
Except script fails to check file exists or not in remote node Sudhakar333 Shell Programming and Scripting 4 09-02-2014 05:13 PM
File Check in remote server ChandanN Shell Programming and Scripting 3 08-13-2014 02:55 PM
Unable to check if file exists on remote server using expect temp_user Shell Programming and Scripting 6 04-02-2014 04:47 AM
ssh to remote server and check if file exists jimbojames Shell Programming and Scripting 4 09-03-2012 08:09 PM
C shell scripting, check if link exists on remote servers CDi Shell Programming and Scripting 0 12-28-2011 03:49 PM
csh Check if file exists on remote system meteorologistks Shell Programming and Scripting 3 10-07-2011 11:40 AM
Check if remote folder exists funonnet Shell Programming and Scripting 0 03-10-2011 05:21 AM
Check file exists on remote machine. marpadga18 Shell Programming and Scripting 2 10-24-2010 02:41 PM
ftp - check if file on remote exists (skip overriding) spiriad UNIX for Dummies Questions & Answers 3 07-30-2010 05:25 AM
Problem with ftp- how to check if the remote directory exists.? anijan Shell Programming and Scripting 2 03-02-2010 11:50 PM
check if remote file exists hcclnoodles Shell Programming and Scripting 2 08-27-2008 05:53 PM
check if file exists on remote system ? hcclnoodles Shell Programming and Scripting 2 10-26-2006 04:08 AM
Need Script to check whether user exists in the remote machine Srini75 SCO 1 09-07-2005 11:23 AM
Check Remote Folder Exists borncrazy Shell Programming and Scripting 1 07-12-2004 04:15 PM