cp

The Linux copy command (cp)

Linux file copy FAQ: How do I copy Linux files and directories? (Or, Can you share some cp command examples?)

You use the Linux cp command to copy Linux files and directories. Let's look at some copy examples to see how this works.

Using Linux cp to copy files

At its most basic, here's how you copy a Linux file:

cp Chapter1 Chapter1.bak

This cp command copies the original file named Chapter1 to the new file named Chapter1.bak. After issuing this command both your original file and the new file will be in the current directory.

A long Unix "file management" tutorial page

If you happen to be looking for a collection of Unix "file management" commands, look no further than the Unix/Linux file management page of my "Introduction to Unix" tutorial.

Linux cp command man page

This page shows the contents of the Linux cp comamnd man page. This cp command output was created on a CentOS Linux system.

You can see this same cp command man page output by entering this command on your own Linux system:

Error message: mv cannot unlink remove file, operation not permitted

I got this Unix/Linux error message today "mv: cannot unlink '/tmp/forms-1.2.1.jar': Operation not permitted" when trying to move (mv) the file forms-1.2.1.jar from the /tmp directory to another directory. The full error output looked like this:

Linux cp command examples

The Linux cp command lets you copy one or more files or directories. To be clear, the cp command makes a duplicate copy of your information, whereas the mv command moves your information from one location to another.

Let's take a look at some Linux cp command examples.

Simple file copying

Using a Linux or Unix system, to copy a file named "foo" to a new file named "bar" just type:

Linux tutorial, part 3 (ssh, cd, ls, cp, mv)

Logging in to a remote system

To login to that system I'll use a command named ssh, which stands for "secure shell". It's basically an encrypted login session to a remote system. To login to that remote system I'll type this command in my terminal window:

ssh al@foo.bar.com

(Of course everything after the ssh command there is made up. I don't have a login account on any systems named anything like that.)

Linux find example - how to copy one file to many directories

Did you ever need to take one file and copy it to a whole bunch of other directories? I had this problem recently when I changed some of the header files on the devdaily.com web site. I had a file named ddhead.html, and I needed to copy it to a bunch of subdirectories.

Using Unix, Linux, or Cygwin this turns out to be really easy. I just used the Linux find command, in combination with the cp command. Once I figured out the right syntax, I was able to copy the file to nearly 500 directories in just a few seconds.

Syndicate content