Linux Useful CP and SCP command options with example

Useful CP and SCP command options with example


Be it you are an Administrator or an general User of Linux, copying files are something you do frequently. cp command is used to copy the files locally and scp (secure copy) to do it remotely . Both commands are default to Linux base operating system.


In this how-to article we will demonstrate few useful cp and scp command examples. 



CP command: 


Copy file to a different directory



root@unisonunix01:~# cp /etc/fstab /tmp

Here we are coping fstab (source) file to /tmp directory (destination) 

Let's verify whether it is copied or not.


root@unisonunix01:~# ls -l /tmp
total 4
-rw-r--r-- 1 root root 2410 Sep 29 17:10 fstab
root@unisonunix01:~#


Copying the files interactively (-i)

Either you use cp or cp -i it'll do the same thing as cp is being an alias to "cp -i" by default. It triggers an question whether you want to overwrite if the file already exists. You need to press ‘y’ to allow the copy operation


root@unisonunix01:~# cp -i /etc/fstab /tmp
cp: overwrite '/tmp/fstab'? y
root@unisonunix01:~#



Copying multiple files at one go



root@unisonunix01:~# cp /etc/a /etc/b /etc/c /tmp
root@unisonunix01:~#
Here we are coping  file a,b, and c to /tmp directory.





if you want to see more elaborative Information on the copy option, then add -v. 

root@unisonunix01:~# cp -v /etc/fstab  /tmp
'/etc/fstab' -> '/tmp/fstab'
root@unisonunix01:~#


If you want to copy directories recursively then use -R or -r or --recursive

[root@unisonunix01 ~]# cp -r /etc/ /tmp

Verify that data is copied or not?
[root@unisonunix01 ~]# ls /tmp
etc                                                   
fstab                                                 
[root@unisonunix01 ~]#
In above example we have copied /etc directory to /tmp file system.




If you want to copy the file only when source file is newer than the target file or when the destination file is missing, you need to use "-u" option.

Let's Understand that with an Example:
create a file in /etc and add some text to it and copy it over.
[root@unisonunix01 ~]# echo "some text" >> /etc/testfile.txt
[root@unisonunix01 ~]# cp -uv /etc/testfile.txt /tmp
‘/etc/testfile.txt’ -> ‘/tmp/testfile.txt’

It is able to copy , but if you do same thing again,it would do nothing as both the conditions are failed. 
[root@unisonunix01 ~]# cp -uv /etc/testfile.txt /tmp
[root@unisonunix01 ~]#

Now add some text into that file and copy it over . You would see the file is copied as the source file is updated.
[root@unisonunix01 ~]# echo "some text" >> /etc/testfile.txt
[root@unisonunix01 ~]# cp -uv /etc/testfile.txt /tmp
cp: overwrite ‘/tmp/testfile.txt’? y
‘/etc/testfile.txt’ -> ‘/tmp/testfile.txt’
[root@unisonunix01 ~]#


Creating symbolic links using cp command.



[root@unisonunix01 ~]# cp -s /etc/testfile.txt /tmp/newtest.txt
[root@unisonunix01 ~]# ls -latr /tmp/newtest.txt
lrwxrwxrwx 1 root root 17 Oct  1 15:50 /tmp/newtest.txt -> /etc/testfile.txt
[root@unisonunix01 ~]#


We want to preserve timestamps,ownerships and modes while copying, then use "-p" option, which  is same as --preserve=mode,ownership,timestamps



[root@unisonunix01 ~]# echo "some tests" >> preservetest.txt
[root@unisonunix01 ~]# ls -la preservetest.txt
-rw-r--r-- 1 root root 11 Oct  1 16:01 preservetest.txt
[root@unisonunix01 ~]#
[root@unisonunix01 ~]# cp -pr preservetest.txt /tmp/
[root@unisonunix01 ~]#
[root@unisonunix01 ~]# ls -latr /tmp/preservetest.txt
-rw-r--r-- 1 root root 11 Oct  1 16:01 /tmp/preservetest.txt
[root@unisonunix01 ~]#


For Example if you want to Preserve only mode and ownership but timestamps can change, then you can specify Following options:



[root@unisonunix01 ~]# cp --preserve=mode,ownership preservetest.txt /tmp/
[root@unisonunix01 ~]# ls -latr /tmp/preservetest.txt
-rw-r--r-- 1 root root 11 Oct  1 16:03 /tmp/preservetest.txt
[root@unisonunix01 ~]#


SCP Command:



Basic syntax of SCP

scp source_file_name username@destination_host:destination_folder

SCP command will copy the files in background. User will see nothing unless there is an error. You can use “-v” parameter to print debug information into the screen. 


[root@unisonunix01 ~]# scp /tmp/preservetest.txt unisonunix02:/tmp/
preservetest.txt                                                                              100%   11     0.6KB/s   00:00
[root@unisonunix01 ~]#
[root@unisonunix01 ~]# scp -pv /tmp/preservetest.txt unisonunix02:/tmp/
Executing: program /usr/bin/ssh host unisonunix02, user (unspecified), command scp -v -p -t /tmp/
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *


If you want to Preserve modification times, access times, and modes from the original file then use -p options.



[root@unisonunix01 ~]# scp -p /tmp/preservetest.txt unisonunix02:/tmp/
preservetest.txt                                                                              100%   11     1.5KB/s   00:00
[root@unisonunix01 ~]#


If you want to Recursively copy entire directories then use -r option.



[root@unisonunix01 ~]# scp -pr /etc/ unisonunix02:/tmp/
fstab                                                                                         100%  508    32.0KB/s   00:00
crypttab                                                                                      100%    0     0.0KB/s   00:00
mtab                                                                                          100%    0     0.0KB/s   00:00
resolv.conf                                                                                   100%   68     1.5KB/s   00:00


If you want limit the bandwidth usage while copying then use -l option. It will be helpful if you don't want the scp consume all the bandwidth you have.



[root@unisonunix01 ~]# scp -p -l 800 test.img unisonunix02:/tmp/
test.img                                                                                      100%   10MB 100.2KB/s   01:39
[root@unisonunix01 ~]#

 we limit the bandwidth for SCP process to 100 KB/sec. SCP counts in Kilobyte/sec (KB/s). If you want to limit your bandwidth for SCP maximum to 100 KB/s, you need to set it into 100 x 8 = 800.

0 Response to "Linux Useful CP and SCP command options with example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel