cp
This subchapter looks at cp, a UNIX (and Linux) command.
cp is used to copy a file.
cp was part of the original 1969 version of UNICS (the original name for UNIX).
copy a file
Use the cp command to make a copy of a file. This example assumes you created the names file in the quick tour subchapter.
$ cp names saved_names
$
Notice that there is no confirmation of the file copy being made.
This silent behavior is typical of any UNIX shell. The shell will typically report errors, but remain silent on success. While disconcerting to those new to UNIX or Linux, you become accustomed to it. The original purpose was to save paper. When UNIX was first created, the terminals were mostly teletype machines and all output was printed to a roll of paper. It made sense to conserve on paper use to keep costs down.
You can use the ls command to confirm that the copy really was made. You wont be using up any paper.
$ ls
Desktop Movies Send registration
Documents Music Sites
Downloads Pictures names
Library Public saved_names
$
cp makes an exact copy of a file.
common errors
Common errors:
- the source is a directory
- the source does not exist
- attempt to copy a file to itself
overwriting files
The plain cp command will simply replace any existing file with a matching name. No warning.
$ cp names saved_names
$
In the above example, if the file save_names already existed, it will be replaced with a copy of names.
interactive confirmation
You can type cp -i to get an interactive confirmation before replacing a file. Enter n or just the RETURN key to stop the copy and prevent an overwrite. Enter y to go ahead with the copy and replace the old file.
$ cp -i names saved_names
overwrite saved_names? (y/n [n]) n
not overwritten
$
force overwrites
Use the -f option to force the copy to overwrite any file with a matching name. This is the same as the default action of the cp command.
$ cp -f names saved_names
copy to a directory
To copy a file to a different directory, name the directory with a leading slash (/) as the destination:
$ cp sourcefilename directory/
Try this example:
$ cp names testdir/
$ ls testdir
names
$
A copy of the file will be stored in the specified directory with the original name. You can add a new file name after the directory if you want to change the name of the copy at the same time you create it in the specified directory.
$ cp sourcefilename /directory/newname
Try this example:
$ cp names testdir/newnames
$ ls testdir
names newnames
$
copying an entire directory
To copy an entire source directory to a different directory, name each directory:
$ cp sourcedirectory destinationdirectory/
This does not work on all systems without using the -r or -R switch (see next item).
copying an entire directory and subdirectories
If you want to copy a directory and all of its subdirectories (the directory subtree), then use the recursive switch. The recursive switch will be either -r or -R. Check the man page for your system.
$ cp -r sourcedirectory destinationdirectory/
Mac OS X supports both the -r and the -R switches for compatibility with legacy scripts, but on Mac OS X the -r option will not correctly copy special files, symbolic links, or fifos, while the -R option will copy them correctly.
Try this example:
$ cp -r testdir newdir/
$ ls newdir
names newnames
$
copying multiple items to a directory
You can create a list of sources (both files and directories) that are all put into a single destination directory. The last item listed must be a directory and is the destination. You can optionally use the -r or -R flag (which ever is appropriate for your system) to recursively copy source directories with all of their subdirectories. SOme systems may require the -r or -R switch if you include directories as well as files int he source list.
$ cp sourcedirectory1/ sourcedirectory2/sourcefile destinationdirectory/
common error
A common error is that the destination folder already exists.
PC-DOS equivalent
cp -i is the UNIX equivalent of the MS-DOS or PC-DOS command COPY. You can add the PC-DOS equivalent to your shell session with the alias command. To make the change permanent, add the following line to the .bashrc file in your home directory. Note that this version adds interactive questioning before replacing a file, in a manner similar to PC-DOS. Note also that if you add this PC-DOS/MS-DOS equivalent, only add the all upper case version, because the lower case type is an important UNIX command that you will also need.
$ alias COPY="cp -i"
other
On November 8, 2010, Ramesh Natarajan named this the number 28 most frequently used UNIX/Linux command at this web page 50 Most Frequently Used UNIX / Linux Commands (With Examples).
In June 2009, Ken Milberg named this command as one of the Top 50 universal UNIX commands at this web page Top 50 Universal INIX commands. Note that this web page requires agreeing to be spammed before you can read it.
comments, suggestions, corrections, criticisms
free music player coding example
Coding example: I am making heavily documented and explained open source code for a method to play music for free almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).
View music player in action: www.musicinpublic.com/.
Create your own copy from the original source code/ (presented for learning programming).
Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.
Names and logos of various OSs are trademarks of their respective owners.