Linux Copy File Command [ cp Command Examples ]

by Vivek Gite on May 29, 2006 · 3 comments

How do I copy files under Linux operating systems? How do I make 2nd copy of a file?

To copy files and directories use the cp command under Linux, UNIX, and BSD like operating systems. cp is the command entered in a Unix / Linux shell to copy a file from one place to another, possibly on a different filesystem. The original file remains unchanged, and the new file may have the same or a different name.

cp Command Syntax

The syntax is as follows to copy files and directories using the cp command:

 
cp SOURCE DEST
cp SOURCE DIRECTORY
cp SOURCE1 SOURCE2 SOURCE3 SOURCEn DIRECTORY
cp [OPTION]  SOURCE DEST
cp [OPTION]  SOURCE DIRECTORY
 

Where,

  • In the first and second syntax you copy SOURCE file to DEST file or DIRECTORY.
  • In the third syntax you copy multiple SOURCE(s) (files) to DIRECTORY.

Linux Copy File Examples

To make a copy of a file called file.doc in the current directory as newfile.doc, enter:
$ cp file.doc newfile.doc
$ ls -l *.doc

To copy a file in your current directory into another directory, enter:
$ cp filename /tmp
$ ls /tmp/filename
$ cd /tmp
$ ls
$ rm filename

To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter:
$ cp -p filename /path/to/new/location/myfile

To copy all the files in a directory to a new directory, enter:
$ cp * /home/tom/backup

To copy a directory, including all its files and subdirectories, to another directory, enter (copy directories recursively):
$ cp -R * /home/tom/backup

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 3 comments… read them below or add one }

1 Genami Lakau June 5, 2010

My Oracle runs on UNIX plat form. It could be best to know the basiz linus command.

Reply

2 Anonymous June 5, 2010

I would like to know the basic UNIX / LINUS commands.

Reply

3 loki2441 September 4, 2010

cp -v file.doc{,.bak} will also be good

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 11 + 8 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: