Linux Headquarters
[ Register ]
[ About us ] [ Home Page ]

Advertisement
[ Kernel ] [ Documentation ] [ Links ] [ Books ]

Advertisement
next up previous contents index
Next: Pruning Back with rm Up: Moving Information Previous: Moving Information

cp Like a Monk

 

cp is a very useful utility under Unix, and extremely powerful. It enables one person to copy more information in a second than a fourteenth century monk could do in a year.

Be careful with cp if you don't have a lot of disk space. No one wants to see Error saving--disk full. cp can also overwrite existing files--I'll talk more about that danger later.

The first parameter to cp is the file to copy--the last is where to copy it. You can copy to either a different filename, or a different directory. Let's try some examples:

screen5168

The first cp command I ran took the file /etc/rc , which contains commands that the Unix system runs on boot-up, and copied it to my home directory. cp doesn't delete the source file, so I didn't do anything that could harm the system. So two copies of /etc/rc exist on my system now, both named rc, but one is in the directory /etc and one is in /home/larry.

Then I created a third copy of /etc/rc  when I typed cp rc frog--the three copies are now: /etc/rc, /home/larry/rc and /home/larry/frog. The contents of these three files are the same, even if the names aren't.

The above example illustrates two uses of the command cp. Are there any others? Let's take a look:

  • cp can copy files between directories if the first parameter is a file and the second parameter is a directory.
  • It can copy a file and change it's name if both parameters are file names. Here is one danger of cp. If I typed cp /etc/rc /etc/passwd, cp would normally create a new file with the contents identical to rc and name it passwd. However, if /etc/passwd already existed, cp would destroy the old file without giving you a chance to save it!
  • Let's look at another example of cp:

    screen5196

    How did I just use cp? Evidentally, cp can take more than two parameters. What the above command did is copied all the files listed (frog and rc) and placed them in the rc_version directory. In fact, cp can take any number of parameters, and interprets the first n-1 parameters to be files to copy, and the tex2html_wrap_inline8278 parameter as what directory to copy them too.

    You cannot rename files when you copy more than one at a time--they always keep their short name. This leads to an interesting question. What if I type cp frog rc toad, where frog and rc exist and toad isn't a directory? Try it and see.

One last thing in this section--how can you show the parameters that cp takes? After all, the parameters can mean two different things. When that happens, we'll have two different lines:

cp source destination-name
cp file1 file2 ...fileN destination-directory

 


next up previous contents index
Next: Pruning Back with rm Up: Moving Information Previous: Moving Information

Converted on:
Mon Apr 1 08:59:56 EST 1996
Comments: webmaster (at) linuxhq.com.
Advertising: banners (at) linuxhq.com.
Compilation ©1998-2008 Linux Headquarters, Inc.