How to find a file exists in particular through SSH

how to find a file exists in particular through ssh

for example: host1 and dir / home / tree / TEST

Host2: - ssh host1 - find the TEST file exists or not use bash

+6
bash ssh
source share
4 answers

ssh will return the exit code for the command you ask to execute:

if ssh host1 stat /home/tree/TEST \> /dev/null 2\>\&1 then echo File exists else echo Not found fi 

Of course, you will need key authentication, so you avoid the password hint.

+9
source share

This is what I ultimately do after reading and trying out the material here:

 FileExists=`ssh host "test -e /home/tree/TEST && echo 1 || echo 0"` if [ ${FileExists} = 0 ] #do something because the file doesn't exist fi 

Read more about the test: http://linux.die.net/man/1/test

+7
source share

The extension for Eric accepted the answer.

Here is my bash script to wait for an external process to download a file. This will block the current execution of the script indefinitely until the file exists.

Key-based access to SSH is required, although this can easily be changed to curl version for HTTP verification.

This is useful for downloading via external systems that use temporary file names:

  • rsync
  • transmission (torrent)

Script below:

 #!/bin/bash set -vx #AUTH=" user@server " AUTH="${1}" #FILE="/tmp/test.txt" FILE="${2}" while (sleep 60); do if ssh ${AUTH} stat "${FILE}" > /dev/null 2>&1; then echo "File found"; exit 0; fi; done; 
+1
source share

No echo needed. It could not be much easier than that :)

 ssh host "test -e /path/to/file" if [ $? -eq 0 ]; then # your file exists fi 
0
source share

All Articles