Network File Copy using SSH

Network File Copy using SSH

   

This post was last updated on May 26th, 2020 at 02:45 pm

Please note that &&, ||, and -, are documented at the bottom of this page.


PUSH:

  • ssh target_address cat <localfile ">" remotefile
  • ssh target_address cat <localfile - ">" remotefile
  • cat localfile | ssh target_address cat ">" remotefile
  • cat localfile | ssh target_address cat - ">" remotefile
  • dd if=localfile | ssh target_address dd of=remotefile
  • ssh target_address cat <localfile "|" dd of=remotefile
  • ssh target_address cat - <localfile "|" dd of=remotefile
  • ( cd SOURCEDIR && tar cf - . ) | ssh target_address "(cd DESTDIR && tar xvpf - )"
  • ( cd SOURCEDIR && tar cvf - . ) | ssh target_address "(cd DESTDIR && cat - > remotefile.tar )"
  • ( cd SOURCEDIR && tar czvf - . ) | ssh target_address "(cd DESTDIR && cat - > remotefile.tgz )"
  • ( cd SOURCEDIR && tar cvf - . | gzip -1 -) | ssh target_address "(cd DESTDIR && cat - > remotefile.tgz )"
  • ssh target_address "( nc -l -p 9210 > remotefile & )" && cat source-file | gzip -1 - | nc target_address 9210
  • cat localfile | gzip -1 - | ssh target_address cat ">" remotefile.gz


PULL:

  • ssh target_address cat remotefile > localfile
  • ssh target_address dd if=remotefile | dd of=localfile
  • ssh target_address cat "<" remotefile >localfile
  • ssh target_address cat "<" remotefile.gz | gunzip >localfile

  • COMPARE:

  • ###This one uses CPU cycles on the remote server to compare the files:
  • ssh target_address cat remotefile | diff - localfile
  • cat localfile | ssh target_address diff - remotefile
  • ###This one uses CPU cycles on the local server to compare the files:
  • ssh target_address cat <localfile "|" diff - remotefile

  • Push: Push local file to remote server.
    Pull: Pull remote file from remote server to local machine.

    Of course there is always ftp, scp2, nfs, smb and other methods as well.

    The above methods make a great Ghost replacement.
    One can boot a system using standalone linux on a floppy, such as tomsrtbt and can then proceed to:

    1. backup the local hard drive to a remote server or
    2. download an image from the remote server and place it on the local hard drive.

    RSH works just the same as SSH I’m sure, it’s jut that ssh or ssh should give you better security.

    Note: Compressing and then transferring data is faster than transferring uncompressed data. Use compression before sending data over the wire to achieve faster data transfer speeds.

    localfile and remotefile can be files, directories, images, hard drive partitions, or hard drives.


    Moving files around on local filesystem:

    • ( cd SOURCEDIR && tar cf - . ) | (cd DESTDIR && tar xvpf - )


    FTP VIEW:

    • ftp> get file.gif "| xv -"
    • ftp> get README "| more"


    FTP PUSH:

    • ftp> put "| tar cvf - ." myfile.tar
    • ftp> put "| tar cvf - . | gzip " myfile.tar.gz


    FTP PULL:

    • ftp> get myfile.tar "| tar xvf -"


    Pipes and Redirects:

    • zcat Fig.ps.Z | gv -
    • gunzip -c Fig.ps.gz | gv -
    • tar xvf mydir.tar
    • tar xvf - < mydir.tar
    • cat mydir.tar | tar xvf -
    • tar cvf mydir.tar .
    • tar cvf - . > mydir.tar
    • tar cf - . | (cd ~/newdir; tar xf -)
    • gunzip -c foo.gz > bar
    • cat foo.gz | gunzip > bar
    • zcat foo.gz > bar
    • gzip -c foo > bar.gz
    • cat foo | gzip > bar.gz
    • cat foo | gzip > bar.gz


    SSH Keys

    see
    http://www.cpqlinux.com/sshkeys.html


    Explanation of &&, ||, and –

    && is shorthand for “if true then do”
    || is shorthand for “if false then do”
    These can be used separately or together as needed. The following examples will attempt
    to change directory to “/tmp/mydir”; you will get different results based on whether
    “/tmp/mydir” exists or not.
    cd /tmp/mydir && echo was able to change directory
    cd /tmp/mydir || echo was not able to change directory
    cd /tmp/mydir && echo was able to change directory || echo was not able to change to directory
    cd /tmp/mydir && echo success || echo failure
    cd /tmp/mydir && echo success || { echo failure; exit; }

    The dash "-" is used to reference either standard input or standard output. The context in which the dash is used is what determines whether it references standard input or standard output.


    Netcat:

    Also be aware of nc (netcat). It is good for transferring files and a lot of other TCP/UDP usages. Examples can be found in the nc man page. Here is an example to demonstrate transferring a file.

    ( On receiver: 192.168.0.10 )
    nc -l -p 3000 >file.txt

    ( On sender )
    cat file.txt | nc 192.168.0.10 3000


    prev next index


    Previous Installing Linux on the Compaq Original Smart Array Controller
    Next RedHat Serial Port Console Install

    About author

    Sibananda Sahu
    Sibananda Sahu 149 posts

    A Linux Kernel Developer and a Firmware Developer by profession. Have worked with few big companies: BROADCOM Corporation, Cypress Semiconductor, LSI Corporation, TOSHIBA Corporation, Western Digital; on various cutting edge technologies and product lines, such as: RAID storage Driver, SSD Firmware, WLAN Firmware etc. Having more than 9 years of experience in Software Engineering domain. Now, took a pledge to educate all aspirant students to teach about Linux Kernel Development.

    View all posts by this author →

    You might also like

    Administration 0 Comments

    How To Download Docker Images From Docker Hub In Linux?

    This post was last updated on August 1st, 2020 at 08:53 amDocker image is an immutable file that is essentially a snapshot of a container. Images are created with build command, and

    Administration 0 Comments

    How To Check The Running Time Of A Process In Linux/Unix ?

    This post was last updated on August 1st, 2020 at 10:13 amIn Unix like operating systems, a process is a program in execution. The running time of a process means

    Administration 0 Comments

    SAMBA in Linux – Administration Checklist

    This post was last updated on May 27th, 2020 at 05:04 pm Contents1 Samba startup guide checklist:2 Getting SWAT to work in RH70 Samba startup guide checklist: This applies to

    0 Comments

    No Comments Yet!

    You can be first to comment this post!

    Leave a Reply