Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCP is creating subdirectory... but I just want it to copy directly

Tags:

scp

I'm trying to use scp to copy recursively from a local directory to a remote directory.... I have created the folders on the remote side:

Remote Location (already created):

/usr/local/www/foosite

I am running scp from the local machine in directory:

/usr/local/web/www/foosite

But it's copying the "foosite" directory as a subdirectory... I just want the contents of the folder, not the folder itself...

Here is the command I'm using:

scp -r /usr/local/web/www/foosite [email protected]:/usr/local/www/foosite
like image 339
user1801932 Avatar asked Nov 24 '14 12:11

user1801932


People also ask

Which option for SCP allows you to copy a directory and all its sub directories?

To copy a directory (and all the files it contains), use scp with the -r option. This tells scp to recursively copy the source directory and its contents.

How do I copy a file in subdirectories?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option.

What command do you use to copy files from all subdirectories to another directory?

To copy a directory with all subdirectories and files, use the cp command.

Does SCP copy folder?

The scp command copies files or directories between a local and a remote system or between two remote systems. You can use this command from a remote system (after logging in with the ssh command) or from the local system. The scp command uses ssh for data transfer.


2 Answers

The problem is that if you don't use the asterisk (*) in the local part of the call, scp will create a new top level directory in the remote server. It should look like this:

scp -r /usr/local/web/www/foosite/* [email protected]:/usr/local/www/foosite

This says "Copy the CONTENTS" (but not the directory itself) to the remote location.

Hope this helps... Took me an hour or so to figure this out!!!

like image 138
user1801932 Avatar answered Oct 02 '22 19:10

user1801932


Old question, but I think there is a better answer. The trick is to leave the foosite directory off of the destination:

scp -r /usr/local/web/www/foosite [email protected]:/usr/local/www

This will create the foosite directory on the destination if it does not exist, but will just copy files into foosite if the directory already exists. Basically the -r option will copy the last directory in the path and anything under it. If that last directory already exists on the destination, it just doesn't do the mkdir.

like image 26
nortoon Avatar answered Oct 02 '22 20:10

nortoon