fredag 23 januari 2015

Linux Copy File Command [ cp Command Examples ]

by nixCraft on May 29, 2006 · 15 comments· LAST UPDATED September 14, 2011

in AIX, BASH Shell, FreeBSD
ow 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:
 
 SOURCE DEST
 SOURCE DIRECTORY
 SOURCE1 SOURCE2 SOURCE3 SOURCEn DIRECTORY
 OPTION  SOURCE DEST
 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
Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?
comments… read them below or add one
1 Genami Lakau June 5, 2010 at 11:33 pm
My Oracle runs on UNIX plat form. It could be best to know the basiz linus command.
Reply
2 Anonymous June 5, 2010 at 11:34 pm
I would like to know the basic UNIX / LINUS commands.
Reply
3 loki2441 September 4, 2010 at 8:27 am
cp -v file.doc{,.bak} will also be good
Reply
4 milind dhuri August 22, 2011 at 8:15 am
i am having server where user are created in home partition i want to copy one file to eache users desktop wheather i.e. possible with one command of cp if somebody knows please mail me
Reply
5 March 14, 2012 at 10:52 pm
actually this is wrong. It doesn’t copy subdirectories
To copy all the files in a directory to a new directory, enter:
$ cp * /home/tom/backup
Reply
6 Magesh M March 20, 2013 at 12:21 pm
Today i have copyed all the files and folder to new directory using below commands
$ cp -R * /home/tom/backup
$ cp * /home/tom/backup
Reply
7 sophea October 18, 2013 at 4:53 am
Dear all,
I want to copy folder1 to folder2 folder3 folder4 folder5… so what the command line should i type in a single line?
Thanks
Reply
8 nixCraft October 18, 2013 at 7:16 am
cp -avr folder{1,2,3,4,5} /path/to/dest
cp -avr folder* /path/to/dest
cp -avr folder1 folder2 folderN /path/to/dest
Read the cp syntax for more info.
Reply
9 sophea February 25, 2014 at 3:38 am
Dear NixCraf,
Its not mean that. suppose i have one folder named folder1 but i need 6-more folder.
usually i type command as bellow to do my job,
cp folder1 folder2
cp folder1 folder3
cp folder2 folder3
….
i don’t want to use for-loop so, have any option in cp command to do that?
thanks for your reply
sophea
Reply
10 February 4, 2014 at 6:39 am
what is meanning of cp -b command??
Reply
11 November 4, 2014 at 6:03 pm
If you need helping with the copy command (CP)….
At the command line (CLI)… type:
cp –help
This will display the list of switches used on how to copy. It also shows the format on how to copy single, multiple files and directories.
Reply
12 sarmaji April 13, 2014 at 4:14 pm
How to avoid the overwriting of the file while copying if the file is already existing ?
Reply
13 Nix Craft April 14, 2014 at 1:56 pm
cp -i file dest
See -i option.
Reply
14 July 16, 2014 at 12:49 pm
Hi dear,
I want to know how to create schedule copy script on red hat Linux. I mean database server to backup server.
Reply
15 October 28, 2014 at 3:32 pm
How does this command know what directory you are saving the newly copied file too? Where is that in the below example?
$ cp filename /tmp
$ ls /tmp/filename
$ cd /tmp
$ ls
$ rm filename
Reply

Inga kommentarer: