Five ways to copy files in Unix : Transfer of data or copying data is indispensable in computer science. There are in-numerous application of this process in today’s world. In this post, I will explain five different ways to copy files in Unix operating system, locally as well as remotely.
Five ways to copy files in Unix
Copy file using cp
cp is Unix command or utility to copy files or directory from source path to destination. Simple command create a copy of a file in current directory.
cp test.txt test123.txt
Copy a file from source directory to target directory.
cp /tmp/config.txt /prod/conf
This is to copy file to parent directory.
cp /tmp/file.txt ..
Copy a directory(e.g. conf) recursively to home directory.
cp -r /prod/conf /home
Secure copy – scp
Scp is a secure remote copy program for Unix server. Using this we can copy file or directory from remote source to local host or local source to remote host. It copies files over the network using ssh, therefore uses password or passphrase for authentication.
Following command show secure copy from remote_host to local host
scp remote_host:/src/file.dat /tmp/
and to copy from local host to remote host use below command.
scp /tmp/hello.dat remote_host:/src/
FTP file transfer
FTP is file transfer protocol based on client server architecture. It is uses secure connection, secured with SSL/TLS. You can use ftp commands in here document to transfer file.
#!/bin/sh HOST='HOSTNAME' USER='user_name' PASSWD='your_password' FILE='file.txt' ftp $HOST <<END user $USER $PASSWD put $FILE quit END exit 0
Its a remote file copying tool, which is fast and versatile. It is versatile as it sync data rather copy entire data from the beginning.Below rsync command recursively copy bar directory from src path of a hostxy server to /data/tmp directory of local host. ‘-a’ specify archive mode that ensure that sym‐bolic links, devices, attributes, permissions, ownership, etc. are preserved in the transfer. ‘-z’ is specify compression mode, that ensure data compression during transfer. And, ‘-v’ is to increase verbosity.
rsync -avz hostxy:src/bar /data/tmp
Connect:Direct or NDM
Connect:Direct or NDM(Network data mover) is file transfer program, which copy file from source server to target.
We can use this in both Mainframe and Mid-range to transfer file. It uses TCP/IP to transfer file and more made file transfer routine and reliable.
Following a step in NDM to tranfer file.
remote_host=remote_server source_file=/opt/test/test_file.dat dest_file=new_file.dat ndmcli -x <<-EOF submit maxdelay=unlimited procname123 process pacct=""C:D testTransfer"" snode=$remote_host step01 copy from (file = $source_file) compress = extended to (file = $dest_file snode disp = new) pend; EOF