Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does scp create the target folder if it does not exist [closed]

Tags:

linux

bash

scp

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 [email protected]:/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.

like image 846
Benoit Paquette Avatar asked Oct 16 '12 18:10

Benoit Paquette


People also ask

What is scp target?

With the scp command, you can specify the source (the file or directory to be copied) and the target (the location in which to copy the file or directory).

Does scp overwrite files by default?

By default, existing files are overwritten. To control overwrite behavior, use --overwrite. (If the files are identical no transfer occurs regardless of this setting value.) Because scp uses authentication and encryption provided by ssh, a Secure Shell server must be running on the remote computer.

Where does scp copy to by default?

Copy or Download a File From Remote to Local Using SCP Just invoke SCP followed by the remote username, @, the IP address or host, colon, and the path to the file. If not specified, the default path is the remote user's home directory.

Does scp work on folders?

The Unix command scp (which stands for "secure copy protocol") is a simple tool for uploading or downloading files (or directories) to/from a remote machine.


2 Answers

To achieve the task with ssh and scp (instead of rsync):

Solution

Lets break into 2 steps :

1. Create directory if missing:

ssh [email protected] "mkdir -p /data/install/somefolder" 

2. Copy to it:

scp -r /data/install/somefolder [email protected]:/data/install/somefolder 

Put them together

server="[email protected]" destiny="/data/install/somefolder" src="/data/install/somefolder" ssh "$server" "mkdir -p $destiny" && scp -r "$src" "$server:$destiny" 
like image 68
Thamme Gowda Avatar answered Oct 20 '22 14:10

Thamme Gowda


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.

like image 34
Jarmund Avatar answered Oct 20 '22 14:10

Jarmund