SFTP Does directory exist?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SFTP Does directory exist?
# 1  
Old 09-20-2011
SFTP Does directory exist?

Hi,

Im trying to add some validation into my shell script code that basically checks whether a directory exists before SFTP'ing a file to it. If the directory exists then it will add the file, if not then it should return some kind of message. This is the code I have written so far but with no success:
Code:
sftp ${SFTP_ACCOUNT}@${SFTP_ADDR} >${ERROUT} 2>&1 <<EOF
#echo $SFTP_DESTINATION
if [-d $SFTP_DESTINATION && echo "directory exists"];
then
   put ${SOURCE_FILE_NAME} ${SFTP_DESTINATION}
else
  echo "Remote Directory ${SFTP_DESTINATION} does not exist"
fi
bye
EOF

Any ideas on how i do this would be greatly appreciated!

Thanks,
Jack

Last edited by Scott; 09-20-2011 at 07:59 AM.. Reason: Added code tags
# 2  
Old 09-20-2011
I don't think we can do that within sftp. Only basic commands like ls works.
But since you have sftp set up, you can make use of ssh to check the directory existence and then proceed with sftp
Code:
ret=$( ssh ${SFTP_ACCOUNT}@${SFTP_ADDR} "test -d $SFTP_DESTINATION >/dev/null 2>&1; echo \$?" )
 
if [ $ret -eq 0 ]
then
  echo "Now go ahead with sftp"
fi

--ahamed

Last edited by ahamed101; 09-20-2011 at 09:17 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Linux sftp — how to add new user to access exist directory with write permission?

I have built a website and I can access and edit the website'files on server via the root user. The current file and directory structures are not changeable. Now I am hiring a webpage designer to help me re-design some pages, I am going to let the designer edit the files directly on the server. So... (5 Replies)
Discussion started by: uwo-g-xw
5 Replies

2. UNIX for Beginners Questions & Answers

A file or directory in the path does not exist

I'm brand new to AIX and I looked up how to print this file and it was working but now I'm not able to do it all of a sudden. the file name is rom1.txt so this is what i wrote in the command line and I know I'm in the right directory. In bold is what I seem to be messing up with. prod @ root... (3 Replies)
Discussion started by: Dark0Prince
3 Replies

3. Solaris

User directory doesn't exist

Hii all, i create the user useradd -d /home/kk kk passwd kk when i tried to login to kk i get a error user directory doesn't exist then i tried useradd kkk passwd kkkwhen i tried to login to kkk i get the same error user directory doesn't exist. (4 Replies)
Discussion started by: vipinkumarr89
4 Replies

4. Shell Programming and Scripting

how to check file exist in a directory or not

HI folks, can any one tell me how to check whether the file is existed in a directory or not . let me tell you my requirement : if the file is existed i should display a one message or else i have to send a mail .. i have the mail logic .. but I'm failed to check file existence .. please... (5 Replies)
Discussion started by: sravan008
5 Replies

5. UNIX for Advanced & Expert Users

Directory is invisible in listing but it is exist.

Hi ALL. Can anyone could help me. Have you had a chance to experienced that when you list (ls) a directory from ordinary execution of command, you couldn't see the directory. However, when you list it from the directory filename itself or even changing to directory (cd), it will show to you... (9 Replies)
Discussion started by: BCJapan
9 Replies

6. Shell Programming and Scripting

sftp mget where file doesn't exist BASH

I have a script that is working: #!/bin/bash sftp user@domain.com <<EOF cd somedir mget *.csv quit EOF but on a crontab I want to only pull newer files, so I want to do something like: while read ls current dir local file != true do mget that new file but I'm not sure the syntax... (2 Replies)
Discussion started by: unclecameron
2 Replies

7. Shell Programming and Scripting

check the directory exist

I have the below script to check whether directory is exist or not , now I sure the directory /abc NOT exist , but when run the script , it still pop the result is "the directory exist" , could suggest what is wrong ? thx ll -d /abc > /dev/null 2>&1 if then echo "the directory exist !!" ... (7 Replies)
Discussion started by: ust
7 Replies

8. Shell Programming and Scripting

To check whether a directory is exist and if it is not, create it

Hi, I want to write a shell script to check whether a directory (say A) is existing in a given location and if it is not, create it. (3 Replies)
Discussion started by: sabya
3 Replies

9. Shell Programming and Scripting

How to check directory exist on servers

There are many servers and their directory structer should be exactly the same. To check the directory path for all servers, I wrote a script. #! /bin/ksh ARRAY_DIRECTORIES="/c/dev/custom/bin" ARRAY_DIRECTORIES="/c/dev/db/custom/src" ARRAY_ENV="remoteName200" ARRAY_ENV="remoteName201"... (2 Replies)
Discussion started by: weonpc
2 Replies

10. Programming

how to check if directory/file exist using c/c++

Hi there, how to check if directory/file exist using c/c++ under linux/unix. Thanks. Steven (2 Replies)
Discussion started by: steven88
2 Replies
Login or Register to Ask a Question