Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if dir exists over ssh and return results to host machine [duplicate]

Tags:

bash

shell

ssh

I'm trying to establish an SSH connection and see if a directory exist, and if that directory exists I want to run commands on the local machine that made the SSH call.

Here is what I've attempted:

if [ ssh -t username@ssh_server -d /directory ] then {     commands.... } fi 

Is something like this possible?

like image 875
hax0r_n_code Avatar asked Apr 10 '13 13:04

hax0r_n_code


People also ask

How do you check if a directory exists or not in bash?

Checking If a Directory Exists In a Bash Shell Script-h "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir exists but point to $(readlink -f /path/to/dir)." The cmd2 is executed if, and only if, cmd1 returns a non-zero exit status.

How do you check if any file exists in a directory in shell script?

In order to check if a file exists in Bash using shorter forms, specify the “-f” option in brackets and append the command that you want to run if it succeeds. [[ -f <file> ]] && echo "This file exists!" [ -f <file> ] && echo "This file exists!" [[ -f /etc/passwd ]] && echo "This file exists!"

How do I view a file in SSH?

SSH provides two different commands, which can be used to accomplish this. In order to search for a file location, you can use the find command. Find is a very powerful tool and accepts various arguments allowing you to specify the exact search term (i.e search by name, by type or even by modified time).


1 Answers

You are very close:

Change if statement to

if ssh username@ssh_server '[ -d /directory ]' 

I am assuming that you have setup key-based authentication.

like image 160
anishsane Avatar answered Oct 02 '22 09:10

anishsane