# How to copy a directory from remote to home



## max21 (Sep 10, 2018)

From my FreeBSD mate-desktop I can copy directories and files to my VPS using cat.  I use user or root since it disconnect after the operation has completed.:

```
cat /root/MyFolder | ssh root@100.101.102.104 -p 2222 'cat >>MyFolder'
```
I don’t how to reverse this action.  I need to import a directory from my remote machine onto my desktop.  My IP is not static but can it still work?  My Firewall has good rules and I use a higher port number other then 22.  Would this be fairly safe?


----------



## SirDice (Sep 10, 2018)

I suggest you look into net/rsync, much easier to use and will be able to sync only the changes, not everything.

push: `rsync -avr /some/local/dir rome0@remote:/remote/dir`
pull: `rsync -avr rome0@remote:/remote/dir /some/local/dir`



max21 said:


> I don’t how to reverse this action. I need to import a directory from my remote machine onto my desktop.


`ssh rome0@100.101.102.104 -p 2222 'cat MyFolder' | cat << /root/MyFolder`


----------



## max21 (Sep 10, 2018)

Rsync, Thanks SirDice!, and thanks for the reverse too.


----------



## xtaz (Sep 10, 2018)

Seems a little convoluted. What's wrong with `scp -r -P 2222 MyFolder rome0@100.101.102.104:/root/MyFolder` ?


----------



## VladiBG (Sep 10, 2018)

Don't use `cat`to copy files. For simple file transfer use scp(1) as it's part of the OS and you don't have to install anything. If you need to mirror the content of the folders and copy only the changes then use rsync(1)


----------



## SirDice (Sep 10, 2018)

xtaz said:


> What's wrong with  scp -r -P 2222 MyFolder rome0@100.101.102.104:/root/MyFolder ?


Nothing wrong with that. Except it always copies everything and you'll need to start over if the connection drops halfway through. It also won't remove files on the other end if they've been removed locally.


----------



## max21 (Sep 20, 2018)

SirDice said:


> I suggest you look into net/rsync, much easier to use and will be able to sync only the changes, not everything.
> 
> push: `rsync -avr /some/local/dir rome0@remote:/remote/dir`
> pull: `rsync -avr rome0@remote:/remote/dir /some/local/dir`



Yeap, I notice for a long tiime everyone who use FreeBSD as a server vote rsync(1).  It's going to solve lots of problems before even getting started.  System backup and everything!

VladiBG, I see what you mean....  cat is ok for text-files (copying, appending and such) … I did lots of testing and I see it only works for regular files.  If there is a binary file in there you got problems.  So I'll keep the few below as a reminder.

I also got into some rsync(1) and now I know when rsync(1) is to be use.  For now it takes these tools to build for it, because when I’m finish hopefully nothing much will need changing until flying cars become common place.
The best I got to work:

```
scp -P 2222 -r ~/pfmod/* rome0@100.101.102.104:FILES/
```
Just saw xtaz format today, so I am on track.

```
scp -r -P 2222 ~/pfmod/* rome0@100.101.102.104:FILES/
```
The best of cat usage:

```
cat ~/.history_backup | ssh rome0@100.101.102.104 -p 2222 'cat >>.history_backup'
. . . .
cat /root/MyFolder | ssh root@100.101.102.104 -p 2222 'cat >>MyFolder'
```

That’s about all she wrote.  Also, I cross-up from the start because cat don't do directories, or can it?  I could swear I did a full directory with cat.  As far as files, that is a sure thing.  If I did it before I'll bump into it again.


----------



## max21 (Sep 22, 2018)

VladiBG, when you said *Don't use cat to copy files* I needed to know why because I got cat code that can copy files.  So now I’m looking for the diff other then cat limitations with binary files and directories.  Security should be the same because they both go thru the encrypted ssh tunnel I guest.  But I’m with you VladiBG, scp encrypt the data too.

I have 30 files on my desktop in a directory call .setup.

scp command did not copy one file but it did copy the other 29 files to the remote.  Notice how it acted up showing parts of the .cshrc regardless of what you use.
...............................................................

```
(~) scp -P 2002 -r /.setup/* rome0@100.101.102.104:SETUP/
rome0@100.101.102.104's password:
echotc: Unknown capability `md'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `me'.
```

I had to copy the dot-file by itself and scp verified the copy:

```
(~) scp -P 2002 -r /.setup/.cshrc rome0@100.101.102.104:SETUP/.cshrc
rome0@100.101.102.104's password:
echotc: Unknown capability `md'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `me'.
.cshrc                    100% 2835    98.4KB/s   00:00
```

cat worked perfectly well for copy-file but it did not output the name of file:

```
(~) cat /.setup/.cshrc | ssh rome0@100.101.102.104 -p 2002 'cat >SETUP/.cshrc'
rome0@100.101.102.104's password:
echotc: Unknown capability `md'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `AF'.
echotc: Unknown capability `me'.
```

Although I went off topic (seeking remote --> desktop <- from desktop ideas) what cause this limitation with scp?  Can scp copy all the files at once, if so, what am I’m missing?  Other then that scp is a wonderful tool.


----------



## VladiBG (Sep 22, 2018)

The above is from tcsh try with sh or csh.

scp -P 2002 .testdotfile rome@100.101.102.104:.testdotfile

`cat`doesn't copy the file. It read the sequentially from the given file and output it to the stdout where you can pipe that information to another process or write it to the file.


----------



## max21 (Sep 22, 2018)

VladiBG said:


> The above is from tcsh try with sh or csh.
> 
> scp -P 2002 .testdotfile rome@100.101.102.104:.testdotfile
> 
> `cat`doesn't copy the file. It read the sequentially from the given file and output it to the stdout where you can pipe that information to another process or write it to the file.




I got enough out of this to get some things done with _full_ confidents.  Before night falls, all buffers will be buffed and use properly. 

A File - from remote to me:

```
scp -P 2222 rome0@100.101.102.104:n.txt /n.txt
```

A Directory - from remote to me:

```
scp -r -P 2222 rome0@100.101.102.104:FOLDER /FOLDER
```
.
_bad a**_  FreeBSD

Thanks everybody!
.


----------

