0% found this document useful (0 votes)
252 views5 pages

12 SCP Command Examples To Transfer Files On Linux

Scp (Secure Copy) is a command line tool used to securely transfer files between hosts. It uses SSH in the background to perform file transfers. The basic syntax of scp is similar to the cp command, with the addition of specifying the source and destination hosts. Scp has many options to control aspects of the transfer like verbose output, transferring multiple files or directories recursively, limiting bandwidth usage, and preserving file attributes. While scp securely transfers files, rsync is generally preferred for synchronization as it intelligently transfers only modified files.

Uploaded by

raoch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
252 views5 pages

12 SCP Command Examples To Transfer Files On Linux

Scp (Secure Copy) is a command line tool used to securely transfer files between hosts. It uses SSH in the background to perform file transfers. The basic syntax of scp is similar to the cp command, with the addition of specifying the source and destination hosts. Scp has many options to control aspects of the transfer like verbose output, transferring multiple files or directories recursively, limiting bandwidth usage, and preserving file attributes. While scp securely transfers files, rsync is generally preferred for synchronization as it intelligently transfers only modified files.

Uploaded by

raoch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

scpcommandexamplestotransferfilesonLinux

Linux

BySilverMoon

OnMar15,2014

5Comments

Securecopy
Scp(SecureCopy)isacommandlinetooltocopyortransferfilesacrosshosts.Itusesthe
samekindofsecuritymechanismlikethesshprogram.Infactitusesansshconnectionin
thebackgroundtoperformthefiletransfer.scprefersbothtothe"protocol"thatdefines
howsecurecopyshouldworkandthe"program"(command)whichisinstalledasapartof
OpenSSHsuiteoftools.

Inthisquicktutorialweshalllookatafewexamplesthescpcommandandhowitcanbe
usedtotransferfilessecurely.

Installingscp
Scpisgenerallyinstalledbydefaultonmostlinuxdistrosasapartofopensshpackages.
Onubuntu/debianforexample,theopensshclientpackageprovidesthescpprogram.
$dpkgLopensshclient|grepscp
/usr/bin/scp

/usr/share/man/man1/scp.1.gz

ItstheOpenSSHpackagethatprovidesthessh,scp,sftpprogramsalongwithmanyother
tools.Sowedonothavetodoanythingextrainhere,excepttouseandlearntheprogram.

Usingscp
Thebasicsyntaxofscpisverysimpletomemorize.Itlookslikethis

$scpsource_file_pathdestination_file_path

Dependingonthehost,thefilepathshouldincludethefullhostaddress,portnumber,
usernameandpasswordalongwiththedirectorypath.
Soifyouare"sending"filefromyourlocalmachinetoaremotemachine(uploading)the
syntaxwouldlooklikethis

$scp~/my_local_file.txtuser@remote_host.com:/some/remote/directory

Whencopyingfilefromremotehosttolocalhost(downloading),itslooksjustthereverse

$scpuser@remote_host.com:/some/remote/directory~/my_local_file.txt
#justdownloadthefile
[email protected]:/some/path/file.txt.

Thatisprettymuchaboutusingscpforregulartasks.Apartfromit,thereareacoupleof
extraoptionsandfunctionsthatscpsupports.Letstakeaquickoverviewofthose.
Andyes,bydefaultscpwillalwaysoverwritefilesonthedestination.Ifyouneedtoavoid
that,useamorepowerfultoolcalledrsync.

1.Verboseoutput
Withverboseoutput,thescpprogramwouldoutputlotsofinformationaboutwhatitdoesin
thebackground.Thisisoftenusefulwhentheprogramfailsorisunabletocompletethe
request.Theverboseoutputwouldthenindicatetheexactpointwheretheprogramran
intoissues.

$scpv~/[email protected]:/root/help2356.txt
Executing:program/usr/bin/sshhost192.168.1.3,userroot,commandscpvt/root/help2
OpenSSH_6.2p2Ubuntu6ubuntu0.1,OpenSSL1.0.1e11Feb2013
debug1:Readingconfigurationdata/home/enlightened/.ssh/config
debug1:Readingconfigurationdata/etc/ssh/ssh_config
debug1:/etc/ssh/ssh_configline19:Applyingoptionsfor*
debug1:Connectingto192.168.1.3[192.168.1.3]port22.
debug1:Connectionestablished.
.....OUTPUTTRUNCATED

Theoutputwouldbebigandcontaindetailedinformationabouthowtheconnectionis
made,whatconfigurationandidentityfilesarebeingusedandsoon.

2.Transfermultiplefiles
Multiplefilescanbespecifiedseparatedbyaspacelikethis
$scpfoo.txtbar.txtusername@remotehost:/path/directory/

Tocopymultiplefilesfromremotehosttocurrentlocaldirectory
$scpusername@remotehost:/path/directory/\{foo.txt,bar.txt\}.
[email protected]:~/\{abc.log,cde.txt\}.

3.Copyentiredirectory(recursively)
Tocopyanentiredirectoryfromonehosttoanotherusetherswitchandspecifythe
directory
$scpvr~/[email protected]:/root/Downloads

4.Copyfilesacross2remotehosts
Scpcancopyfilesfrom1remotehosttoanotherremotehostaswell.
$scpuser1@remotehost1:/some/remote/dir/foobar.txtuser2@remotehost2:/some/remote/dir/

5.Speedupthetransferwithcompression
Asupercooloptiontospeedupthetransfertosavetimeandbandwidth.Allyouneedto
doisusetheCoptiontoenablecompression.Thefilesarecompressedontheflyand
decompressedonthedestination.
$scpvrC~/[email protected]:/root/Downloads

Intheaboveexamplewemovedtheentiredirectorywithcompressionenabled.Thespeed
gainwoulddependonhowmuchthefilescouldbecompressed.

6.Limitthebandwidthusage
Ifyoudonotwantscptotakeuptheentireavailablebandwidth,thenusetheloptionto
limitthemaximumspeedinKbit/s.
$scpvrCl400~/[email protected]:/root/Downloads

7.Connecttoadifferentportnumberonremotehost
Iftheremoteserverhassshdaemonrunningonadifferentport(defaultis22),thenyou
needtotellscptousethatparticularportnumberusingthe'P'option.
$scpvCP2200~/[email protected]:/some/path/test.txt

8.Preservefileattributes
The'p'option(smallcase),wouldpreservemodificationtimes,accesstimes,andmodes
fromtheoriginalfile.
$scpCp~/[email protected]:/some/path/test.txt

9.Quietmode
Inquietmode('q'option),thescpoutputwouldgetsuppressed,andwoulddisablethe
progressmeteraswellaswarninganddiagnosticmessages.
$scpvCq~/[email protected]:/some/path/test.txt

10.Specifyidentityfile

Whenusingkeybased(passwordless)authentication,youwouldneedtospecifythe
identityfilewhichcontainstheprivatekey.Thisoptionisdirectlypassedtothessh
commandandworksthesameway.
$scpvCqiprivate_key.pem~/[email protected]:/some/path/test.txt

11.Useadifferentssh_configfile
Usethe'F'optiontospecifyadifferentssh_configfile.
$scpvCF/home/user/my_ssh_config~/[email protected]:/some/path/test.txt

12.Usedifferentcipher
ScpbydefaultusestheAEScipher/encryption.Sometimesyoumightwanttousea
differentcipher.Usingadifferentciphercanspeedupthetransferprocess.Forexample
blowfishandarcfourareknowntobefasterthanAES(butlesssecure).
$scpcblowfishC~/local_file.txtusername@remotehost:/remote/path/file.txt

Intheaboveexampleweusetheblowfishcipheralongwithcompression.Thiscangive
significantspeedboostdependingonavailablebandwidth.

Summary
Althoughscpisveryefficientattransferringfilesecurely,itlacksnecessaryfeaturesofafile
synchronisationtool.Allitcandoiscopypasteallthementionedfilesfromonelocationto
another.
AmorepowerfultoolisRsyncwhichnotonlyhasallfunctionsofscpbutaddsmore
featurestointelligentlysynchronisefilesacross2hosts.Forexample,itcancheckand
uploadonlythemodifiedfiles,ignoreexistingfilesandsoon.

You might also like