Wednesday, June 16, 2021

UNIX Shell Commands : File copy using various commands

 

1) SFTP Commands in Unix

The basic command used for connecting remote server using sftp in Unix

sftp username@servername

username:  Username for the remote server. This user should be created in remote server should have Public/private key present in the remote server.

servername :  server is the remote servername we need to connect.

After connecting to the sftp server, basically we will perfrom two operations 

a) Pulling the file from remote server.  For this we will use the get command

 get :  get will fecth the file from remote directory and place it to the current directory into the local server.

get will have two parameters . First parameter is mandatory which remote filepath and second parameter local directory path. if we do not provide the second variable it will copy int present directory

i ) get /opt/sw/unix/Remote/Test.txt  

ii) get /opt/sw/unix/Remote/Test.txt  /opt/sw/unix/local/Test.txt

The above commands will copy only the file. What if you need to copy the entire directory from remote server to local server using sftp command, We have abiltiy to copy enitre directory using -r option.

i ) get -r /opt/sw/unix/Remote

ii) get  -r /opt/sw/unix/Remote  /opt/sw/unix/local


 put :  put will place the file in remote directory from the current directory into the local server.

put will have two parameters . First parameter is mandatory which filepath and second parameter remote directory path. if we do not provide the second variable it will copy into present directory in remote server

i ) put /opt/sw/unix/Remote/Test.txt  

ii) put /opt/sw/unix/Remote/Test.txt  /opt/sw/unix/local.Test.txt

The above commands will place only the file. What if you need to place the entire directory from local server to remote server using sftp command, We have abiltiy to place enitre directory using -option.

i ) put -r /opt/sw/unix/local

ii) put -r /opt/sw/unix/local/  /opt/sw/unix/remote/

Above will copy the local directory

2) Copy Commands in Unix

The basic command used for Copying file from one server to other server. It accepts two parameters.

First parameter is madatory which source file. Second parmeter is target path.

cp /opt/sw/sw/SourcePath/Testfile.txt  .

Here . represents local path which is current direcotry. Above command will copy file to current directory. 


cp /opt/sw/sw/SourcePath/Testfile.txt /opt/sw/sw/TargetPath/Test.txt

This comand will copy file from /opt/sw/sw/SourcePath/ to  /opt/sw/sw/TargetPath/ Path

We can copy the entire directory using -R option. This will copy all directory files and folders inside the current directory


cp  -R /opt/sw/sw/SourcePath/   /opt/sw/sw/TargetPath

The above command will copy the SourcePath directory to TargetPath directory



3) Move Commands in Unix


The basic command used for moving file from one server to other server. It accepts two parameters.

First parameter is madatory which source file. Second parmeter is target path.

mv /opt/sw/sw/SourcePath/Testfile.txt  .

Here . represents local path which is current direcotry. Above command will move file to current directory. 


mv /opt/sw/sw/SourcePath/Testfile.txt /opt/sw/sw/TargetPath/Test.txt

This comand will move file from /opt/sw/sw/SourcePath/ to  /opt/sw/sw/TargetPath/ Path

We can move the entire directory using -R option. This will move all directory files and folders inside the current directory


mv -R /opt/sw/sw/SourcePath/   /opt/sw/sw/TargetPath

The above command will move the SourcePath directory to TargetPath directory

4) File Copy using scp in Unix

 The scp command can be used to copy securly in Unix. This is same as Copy command but copies the files securly to remote server. This will be used purely for copying the files.

scp -rp /opt/sw/sw/SourcePath/file.txt  username@remoteserver:/opt/sw/sw/TargetPath

The above command copy file from current seever to remote user and retain same permission as source server. (-rp for replicating same permission of source server)

to copy the files into current server from remote server we can use the below command


scp -rp  username@remoteserver:/opt/sw/sw/SourcePath/file.txt     /opt/sw/sw/TargetPath/  

No comments: