How to test if remote dir exist?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to test if remote dir exist?
# 1  
Old 11-21-2016
How to test if remote dir exist?

Hello, Smilie

I'm trying to test if a remote directory exist per ssh, I have already made a ssh key and it works :
Code:
#!/bin/bash

HOST="10.10.1.22"
FILE_PATH="/var/wwww/html"

ssh -q $HOST [[ -d $FILE_PATH ]] && echo "Directory exists" || echo "Directory does not exist";

Output :
Code:
rsync-user@vmtest1:/home/scripts$ ./test.sh
Directory does not exist

That the directory exist or not, that's always the same output Smilie
Thanks by advance Smilie
# 2  
Old 11-21-2016
The && and || are evaluated locally.
Enclose them in quotes
Code:
ssh -qnx $HOST "test -d $FILE_PATH && echo 'Directory exists' || echo 'Directory does not exist'"

I have chosen the "quotes" because $FILE_PATH should be substituted, and therefore the 'ticks' for the echo.
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 11-21-2016
Thanks ! It works also, I just found my mistake..
Code:
FILE_PATH="/var/wwww/html"

# 4  
Old 11-21-2016
Quote:
Originally Posted by Arnaudh78
It works also,
You will find, that ssh, when executed as a securified rexec-replacement, returns the error code of the remotely executed program.

You could, therefore, equally try the likes of:

Code:
if ! ssh "${user}@${host}" "ls /dir/in/question >/dev/null 2>&1" ; then
     echo "dir does not exist"
else
     echo "dir does exist"
fi

Which would not be of much use in your context (it would i.e. produce false positives if "/dir/in/question" would be a file) but you can use this mechanism to generally execute commands remotely while testing for their return code - UNIX convention is to return 0 on success and something else for the various reasons of being unsuccessful.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 11-21-2016
Quote:
Originally Posted by bakunin
Code:
if ! ssh "${user}@${host}" "ls /dir/in/question >/dev/null 2>&1" ; then
     echo "dir does not exist"
else
     echo "dir does exist"
fi

bakunin
I had no think, why not, it's an easy way, thanks for your reply Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

scp from remote dir

Hi Admins, I want to scp some files from remote sub directories.So i used below scripts to accomplish the same. find `ssh testsftp@10.60.5.120/QSYS.LIB/EDWVPINTER.LIB` -name *.MBR -exec scp {} . \; It fails. I can not place copying script in remote server for various reasons. ... (1 Reply)
Discussion started by: newaix
1 Replies

2. Shell Programming and Scripting

Test variables exist using a for loop

I am setting a number of variables in my script and I would like to test they exist by using a for loop. e.g. VAR1=abc VAR2=xyz for i in VAR1 VAR2; do if ; then echo Alert fi done But of course the $i doesn't refer to the variable itself, rather to the strings VAR1 & VAR2. Does... (2 Replies)
Discussion started by: Catullus
2 Replies

3. Programming

Kernel module - How to test if file doesn't exist

Hey, I'm currently getting into some kernel module progamming. As a little exercise I want to read the headers out of an ELF file. My code is very simple, here is the important part: struct file *fp; /* ... */ fp = filp_open("some/file/on/my/pc", O_RDONLY, 0); if(fp == NULL) { ... (15 Replies)
Discussion started by: disaster
15 Replies

4. Shell Programming and Scripting

FNG Question: Test if remote server has booted

Hello again, I have a script on my media server that wakes up my backup server, performs an 'rsync' backup, then shuts the backup server down. Currently, I have it send the Wake on LAN packet, and sleep for 5 minutes, just to give the backup server time to boot (of course it doesn't take that long,... (11 Replies)
Discussion started by: vwgtiturbo
11 Replies

5. Shell Programming and Scripting

How to check whether file is exist on remote server

Hi all, I am new to UNIX Scripting. I would like to know how to check whether file is exist in remote server. I have google, but cannot find any solution that works. Currently my code is like this: if ; then echo 'data file exist' else echo 'data file not exist' fi Thanks in... (3 Replies)
Discussion started by: suigion
3 Replies

6. Shell Programming and Scripting

File exist test

Can someone please shed light on why this may not be working, file does exist, but I get an error if ] then echo "No ${source_path}/${file_mask} found - ">> ${logfile} result=1 check_result ${result} "Failed to find file... (4 Replies)
Discussion started by: Pokermad
4 Replies

7. Shell Programming and Scripting

To get the dir list of a remote server

Dear all; I am new to UNIX scripting and I want to download remote server's dir listing every hour, to check whether it is updating. How can I do this. can I use ftp command to down load this dir listing. Pls help me in this regards. tks (1 Reply)
Discussion started by: HMS.Chandrasiri
1 Replies

8. Shell Programming and Scripting

Rsh: test $? on remote system.

Hi, a little help. I need to test the return code of a list file command on a remote system (Unix) using the rsh command. More exactly, to test is a directory exists, I try the following command: rsh $remoteHost "ls -la " $DirRemote Now, if the $DirRemote is not correct and I test... (3 Replies)
Discussion started by: gio123bg
3 Replies

9. UNIX for Advanced & Expert Users

Printer Error(the Object Instance Test Does Not Exist)

Hello, i need some help about how to set up a high velocity impact printer in UNIX SCO 5.05, this printer is attached with a parallel port in a PC(host), the host use tunemul to access unix.(this reference is just to ask you if this is a local or remote connection, just to be sure), so, i... (2 Replies)
Discussion started by: jav_v
2 Replies
Login or Register to Ask a Question