Unix Copy Command Examples [ cp command ]

by on June 5, 2012 · 2 comments· LAST UPDATED March 6, 2013

in

How do I copy files under Unix operating system using ksh or csh or bash shell prompt?

You need to use the cp command to copies files and directories under Unix like operating systems. The following commands and common options should work with:

=> IBM AIX

Tutorial details
DifficultyEasy (rss)
Root privilegesYes/No
Requirementscp command
Estimated completion timeN/A

=> Apple OS X

=> Sun/Oracle Solaris

=> FreeBSD/OpenBSD/NetBSD

=> Linux

=> HP-UX

=> And other Unix like oses.

Unix: cp command syntax

The syntax is:

 
cp SOURCE DEST
cp SOURCE DIRECTORY
cp file1 file2
cp file1 new-file2
cp [options] file1 new-file2
 

By default cp will only copies files.

Unix cp command examples

In this example, copy a file called data.txt in the current directory into another directory, called /tmp. Open a terminal and type:

 
cp data.txt /tmp
 

Verify that file has been copied in /tmp, enter:

 
ls -l /tmp/data.txt
ls -l data.txt
 

Please note that the file(s) copied into the directory /tmp will all have the same names as the originals. You can copy multiple files into another directory as follows:

 
cp data.txt foo.tbz image.png /tmp
ls -l /tmp
 

How do I copy recursively?

You need to pass the -r or -R option (i.e., recursive option). It allows directories including all of their contents to be copied:

 
cp -r dir1 dir2
 

To see copy progress pass -v option to cp command:

 
cp -v file1 file2
 

Sample outputs:

`file1' -> `file2'

The -v cause cp to be verbose, showing files as they are copied. You can pass the -v option along with other options:

 
cp -v -r file1 /tmp
cp -v -r dir1 dir2
 

How do I copy all *.c files?

Copy copy all c source code file to a directory called /projects/backup/foo, from /home/vivek/devel/foo, enter:

 
cp /home/vivek/devel/foo/*.c /projects/backup/foo/
 

OR

 
cp -v /home/vivek/devel/foo/*.c /projects/backup/foo/
 

All the files in a directory can be copied to another directory by using the shell wildcard such as start willdcard or question wild card:

 
## copy all perl (*.pl) files to /tmp ##
cp *.pl /tmp
 
## copy all perl (*.pl) files starting with character a to /tmp ##
cp a*.pl /tmp
 
## copy all perl (*.pl) files starting with a,b, and c to /tmp ##
cp [abc]*.pl /tmp
 
## copy all perl (*.pl) files starting with a single file name such as  a.pl, b.pl, .., z.pl to /tmp ##
cp ?.pl /tmp
 
## copy all .html files to /var/www/html ##
## The star wildcard represents anything whose name ends with the .html extension ##
cp -v /home/vivek/website/*.html /var/www/html
 

How do I confirm file overwriting?

You need to pass the -i option to cp. It will prompt the user if file already existing in a destination directory so that file would be overwritten with confirmation:

 
cp -i /etc/resolv.conf /tmp
 

Sample outputs:

cp: overwrite `/tmp/resolv.conf'? y

You need to enter the letter y (both lower case or upper case with work) in response to the prompt causes the command to continue. Any other answer prevents the command from overwriting the file called /tmp/resolv.conf. Some user put the following alias in ksh startup file called $HOME/.kshrc:
$ vi ~/.kshrc
Append the following alias:

 
## prvent overwriting by default for cp command
alias cp='cp -i'
 

Save and close the file. Source the shell startup file to have the changes take immediate effect, enter:

 
. .kshrc
 

Tip: Preserve the file permission and other attributes

You need to pass the -p option to save the following file attributes of each source file as allowed by permissions:

  1. File modification time
  2. File access time
  3. File flags
  4. File mode
  5. File user ID and group ID

The syntax is:

 
cp -p file1 file2
 

Example: Copying a file without -p option

Type the following commands:

 
ls -l /etc/resolv.conf
cp /etc/resolv.conf $HOME
ls -l $HOME
 

Sample outputs:

Fig.01: Unix cp command without -p option

Fig.01: Unix cp command without -p option

Example: Copying a file with -p option to save file attributes

Type the following commands:

 
ls -l /etc/resolv.conf
## Note: running as root to save attributes ##
sudo cp -p -v /etc/resolv.conf $HOME
ls -l $HOME
 

Sample outputs:

Fig.02: Preserve in the copy as many of the modification time, access time, file flags, file mode, user ID, and group ID as allowed by permissions.

Fig.02: Preserve in the copy as many of the modification time, access time, file flags, file mode, user ID, and group ID as allowed by permissions.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 2 comments… read them below or add one }

1 Billy Larlad June 5, 2012 at 1:45 pm

Important to note that the -p option (as in cp -p) will preserve a file’s timestamp and the other metadata. From the OpenBSD cp man page:

“Preserve in the copy as many of the modification time, access time, file flags, file mode, user ID, and group ID as allowed by permissions.”

If you’re using cp for backups, this is good to know.

Reply

2 nixCraft June 6, 2012 at 12:15 pm

@Billy,

Yes, -p is a good option. Appreciate your comment.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: