Networking / Beginners

The cp command

The cp command copies files. Here's the basic syntax:

cp [options] source-file destination-file

The following list describes the more important options for the ls command:

  • -a: The same as -dpR.
  • -b: Makes backup copies of existing files before they're overwritten. Sounds like a good plan to me.
  • -d: Copies links rather than the files the links point to.
  • -f: Removes files that will be overwritten.
  • -i: Interactively prompts for each file to be overwritten.
  • -l: Creates links to files rather than actually copying file contents.
  • -p: Preserves ownership and permissions.
  • -R: Copies the contents of subdirectories recursively.
  • -s: Creates symbolic links to files rather than actually copying file contents.
  • -u: Replaces destination files only if the source file is newer.

To make a copy of a file within the same directory, use cp like this:

$ cp sendmail.cf sendmail.cf.backup

If you want to copy a file to another directory without changing the filename, use cp like this:

$ cp sendmail.cf /home/doug

You can use wildcards to copy multiple files:

$ cp send* /home/doug

To include files in subdirectories of the source file, use the -R switch, like this:

$ cp -R /etc/*.cf /home/doug

In this example, all files in the /etc directory or any of its subdirectories that end with .cf are copied to /home/doug.

[Previous] [Contents] [Next]