1

I want to check via sftp only (not ssh) if a directory exists before creating it. The only solutions I have found so far are using ssh but I need to use sftp only due to permission-ing issues - does anyone know a way?

sftp_put () {
  sftp -oidentityfile=/home/user/.ssh/host_usersftp usersftp@$1 <<EOF
  -- Add a check here e.g -- IF exist $2 ( echo $2 exists ) ELSE ( echo create $2 )
  mkdir $2
  cd $2
  put $3
  EOF
}
3
  • You can do an ls of that folder and check the answer. And seeing your code, if the folder does not exist, you create it... just try to create it and ignore errors (in case it exists). Commented Jul 20, 2018 at 8:36
  • I'm not sure how you think you can implement this Commented Jul 20, 2018 at 10:40
  • 1
    This may not be an answer, but I find it puzzling that you want to use sftp and not ssh, since sftp uses ssh. Are you really wanting ftps? The -o option is sent to the ssh process, same goes for scp. Commented Jul 20, 2018 at 16:41

2 Answers 2

2
echo "chdir my_test_dir" | sftp -b - -oidentityfile=/home/user/.ssh/host_usersftp usersftp@$1
if [ $? -eq 0 ]
then
    echo "my_test_dir exists"
else
    echo "create my_test_dir"
fi
Sign up to request clarification or add additional context in comments.

Comments

2

sftp without -b will simply display a nonfatal warning message if you try to create a directory which already exists. (With -b you need -mkdir with a dash in front to make the script not abort if the command fails. For noninteractive use, -b is probably a good idea.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.