Couple of cool remote ssh commands for your UNIX arsenal

Here is a easy way to copy an entire directory tree from one Unix machine to another, while retaining the permisssions and ownership, using ssh as the middle man. Assuming that you want to copy everything under source_directory to destination_directory on another machine here is the command you would issue on the source machine (first cd to the directory containing source_directory):
tar -cf - source_directory/ | ssh userid@your.destination.machine.com "cd /somedir/destination_directory ; tar -xvlpf -"
or if you want to copy everything from the remote server’s source directory to the local machine’s destination directory:
ssh userid@your.source.machine.com "(cd /somedir/source_directory ; tar -cf - .)" |(cd /somedir/destination_directory ; tar -xvlf -)
Here is another similar command that allows you to backup a HD partition to another host via ssh:
dd bs=1M if=/dev/sdb | gzip | ssh userid@your.destination.machine.com "cd /destination/directory ; dd of=sdb.gz"
I have not bothered dissecting these commands, since I assume you are familiar with Unix and Shell commands. Please note that these commands will most likely not work in OSX, unless you’re working on datafiles only (ie: html files, txt files). Program files under OSX could potentially get corrupted if copied via the first command.

One last command which I came across the other day…..If you’re ever in need of a stop watch just use your shell to measure time. Issue the following command and wait a bit. Now interrupt it via Ctrl-C and it will show you how much time has passed. NOTE: Nothing happens when you issue the command, only when you stop it via ctrl-c.

time cat
Have Fun….

Leave a Reply