46

I am wondering if scp will create the target folder if it does not exist on the remote server. For example, would this work?

scp -r /data/install/somefolder user@ftpserver.com:/data/install/somefolder

Here the folder /data/install/somefolder doesn't exist on ftp server, so would this command create it?

N.B. I have read about rsync but am not really sure how it works or how to use it yet.

doubleDown
  • 8,048
  • 1
  • 32
  • 48
Benoit Paquette
  • 491
  • 2
  • 5
  • 4

2 Answers2

53

To achieve the task with ssh and scp (instead of rsync): lets break the task into 2 steps:

1. Create directory if missing:

ssh user@ftpserver.com "mkdir -p /data/install/somefolder"

2. Copy to it:

scp -r /data/install/somefolder user@ftpserver.com:/data/install/somefolder

Put them together

server="user@ftpserver.com"
destiny="/data/install/somefolder"
src="/data/install/somefolder"
ssh "$server" "mkdir -p $destiny" && scp -r "$src" "$server:$destiny"
Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57
25

Short answer: no.

...but rsync does, which is why I have aliased scp to rsync -Pravdtze ssh on my box. Yes, that's a lot of switches, which in combination produces my preferred rsync behavior. As rsync does provide a very extensive set of switches and options, I suggest you do some research on it to see what fits your needs best. Man-page is a good place to start, but there are a lot of info easily available. Here's a decent list of examples.

Edit: Actually, in that particular case you posted, the folder will be created, as that's the folder you're copying. However, if you're trying to copy it to user@remotehost:somenonexistentfolder/somefolder, then it will fail.

Lii
  • 11,553
  • 8
  • 64
  • 88
Jarmund
  • 3,003
  • 4
  • 22
  • 45