How to copy a file/directory in Unix?

Copy Command in Unix(cp command)


How do i copy files and directories under UNIX like operating systems?

You need to use the “cp” command to copies files and directories under the UNIX like operating systems

Syntax:To copy a file to a directory

#cp    <options>   <source >    <destination>

Options:

-r -recursive (use this option only when you need to remove the entire directory and all its contents)
-v-Verbose   
-f-Forcefully  

Note: I have mentioned some of the important options here ,you can refer the man page to know more about the available options

Ex:1 copy a file called “backup.txt” in to another directory called  /tmp.

First create a file with some contents

#echo  “This is my first line” >/backup.txt

Check the contents

#cat  /backup.txt

Sample output: This is my first line

 

Now copy this file to the /tmp directory

#cp  /backup.txt   /tmp

Successfully  the file copied to the directory /tmp

To confirm go the /tmp directory and verify

#cd  /tmp

#ls

Sample output: backup.txt

 

 

Ex:2 To copy multiple files in to a directory

syntax:

#cp   <source file>   <source file>   <destination directory>

Leave space between the files to copy multiple files to a directory

Let me create some couple of files with some contents :

#echo  “Buy lots of eggs” >/mydoc

#echo “Buy some chicken” >/mydata

#echo “Buy some  cake” >/myfile

we have successfully created three files with some contents .

Now copy all these files to a directory called /vasanth

#mkdir  /vasanth

#cp    -v   /mydoc   /mydata   /myfile     /vasanth

Here i have applied the verbose option (its not mandatory )

Sample output:mydoc -> /vasanth/mydoc `mydata -> `/vasanth/mydata` `myfile -> `/vasanth/myfile`

Go to the  /vasanth directory and confirm

#cd   /vasanth

#ls

Sample output: mydata mydoc myfile

Ex:3 To copy all directory and all its content to another directory:

A directory and all its content can be copied from source to destination with the recursive option -r

It allows directories including all of their contents to be copied:

syntax:

#cp   <option>   <source>   <destination>

Let me create a directory and add some files inside

#mkdir   /testdir

#cd  /testdir

#touch  f1  f2  f3  f4

#mkdir  d1  d2  d3  d4

#cd  /

Now the directory /testdir is having some couple of files and some sub directories inside

#mkdir    /output

This is the destination directory

#cp    -rvf    /testdir     /output

Here i have applied verbose and force option since while copying each and every file it will ask the confirmation from the user (Difficult  for the system administrators if the directory contains some 100 or 500 files), i have granted yes to all by using the -f option.

Go to the destination directory and confirm whether all the files and sub directories copied

#cd  /output

#ls   -l

Sample Output:  drwxr-xr-x  6 root  root  4096   dec 12  13:37  testdir

 

Ex:4  How to select and copy  all the files from a directory to another directory

Let us assume if a directory contains some files and sub directories if i want to select only the files from that folder you have to use a special wildcard character “*”(Asterisk),this will select only the files from the directory and skips all the directories

Create a directory with some files and sub directories inside

#mkdir   /nirmal

#cd  /nirmal

#touch   f1 f2  f3  f4  f5

#mkdir  d1  d2  d3  d4  d4

#cd   /

Now select all the files and copy

#cp  -v    /nirmal/*     /tmp

Ex:5  To avoid overwriting the existing file

Copy only when the destination file doesn’t exist

#cp  -n   <source>  <destination>

Let me create a file :

#mkdir  /test

#cd   /test

#touch result.txt

#ls

Now create a file in / directory with the same name

#touch  /result.txt

#echo “This is my first line”>/test/result.txt

#echo “This is my second line” > /result.txt

Read  both the file  contents :

#cat   /test/result.txt

#cat /result.txt

#cp  -n   /result.txt    /test/result.txt

#cat  /test/result.txt

Sample output: its a new content

Here as we can see that after copying file result.txt in test directory the original file result.txt in test folder is not changed since of “-n” option.

Ex:6  To confirm before overwriting

syntax:

#cp   -i   <source>  <destination>

#cp  -i  /result.txt   /test/result.txt

Sample output: cp:overwrite /test/result.txt?y

#cat /test/result.txt

Sample output:This is my SECOND line

Ex:7  To make a backup up of files,if copying file has the same name:

#cat    /result.txt

Sample output: Hello there

#cat   /test/result.txt

Sample output: Testing line 

use the option –backup before the destination file contents gets overwrite with the new contents

#cp  –backup  /result.txt    /test/result.txt

Now go to the test directory path and check you will see two files

#cd  /test

#ls

Sample output: result.txt   result.txt~

Now check the destination file content

#cd  /test

#cat  result.txt

Sample output: Hello there

#cat    result.txt~

Sample output: Testing line

As you can see here that when result.txt is copied from the source to destination in test directory,a backup of the original file result.txt in the same directory is made as result.txt~ and new file is copied in the result.txt as usual

 

Ex 8:  How to copy only when the source file is newer

In this let me show you how to copy only the newest files from the source location to the destination directory location,to do that we have option -u.

Let me create some files

#touch  /a.txt  /b.txt   /c.txt  /d.txt   /e.txt

Now i have some files in the / directory with 0 bytes size

#ls  -lt

 

let me create a directory

#mkdir  /etc/vasanth

inside /etc/ directory i have created another subdirectory vasanth

#cp   *.txt   /etc/vasanth

This will select and copy  all the files with the extension .txt

#cd  /etc/vasanth

#ls  -l

#touch /newdoc

#ls  -l   *.txt

 

Now  when we use -u option combine with -v option to see what is being done,cp command will only copy a file which is newer from destination directory.

#cp  -uv  *.txt   /etc/vasanth

Sample output:newdoc.txt ->  /etc/vasanth/newdoc.txt`

As the result, we see that only newdoc.txt is copied in to the /etc/vasanth directory

If you found this article useful, Kindly Subscribe here👉🏿👉🏿Click this link to Subscribe

About Author:

Hello readers! Let me introduce my self first. My name is Vasanth Nirmal Singh J S having 9+ years of experience in IT on all flavours of Unix operating systems ,Storage's and many more .. I would like to share my technical experience i have come across - can be help to other people. So in this blog, I'll post my thoughts related to ITIS. I'll share experiences that I've had while working in different environments. You can expect content related to Unix,Solaris,Linux,EMC Storeages,HP-UX and many others. I hope this blog can be useful for you! Your comments will be appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *