To copy a file in the Linux command line interface (cli) then use the cp command. If you are familiar with DOS, cp is like the COPY command. To copy a file you need to write the name of the file you want to copy and the name of the newly copied file.
So, to make a copy of my file called joe and name the new file joe2, I type:
cp joe joe2
I then issue the ls command to show a list of the current files in the directory and I should see my old file joe and a new file called joe2 which is an exact copy of tom.
Now what if I wanted to copy the file joe, but put the copy in another directory? Well, then I simply specify which directory I want the copy to go into.
cp joe Documents/joe2
This will copy the file joe, name the new copy joe2, and place joe2 in the Documents directory. So with one simple command I copied, renamed, and moved a file. It took under a second. If I had done that in the GUI (graphical user interface) it would have taken at least a minute and a bunch of clicks.
What if I type this?: (hint: Documents is a directory.)
cp joe Documents/
Well, if you said a new copy of joe (named joe) would be created in the Documents directory, then you’d be right.
Since we did not specify a name for our copy, but did specify a directory, cp simply used the original name.
What if we do this?:
The output is this an error:
cp: missing destination file operand after `joe’
Try `cp –help’ for more information.
Oops. cp needs a destination, which is either a new name for the copied file, or another directory to place the copy into.
I hope this helps you understand the basics of the GNU Linux command cp. The more you learn about the different cli commands, the more you can do. Soon you’ll be saving tons of time using the cli to perform tasks that would have taken the GUI hundreds of clicks.
Please be careful, though, when starting out with the cli. The command line tools can perform just about any task quickly and efficiently. But it’s easy to damage your system if you accidentally delete something or move a file that shouldn’t have been moved. The cli is like a very sharp katana that can slice through just any problem you may have, but it can also do unintended harm if you aren’t careful.
So when starting out, it’s best to work in safe, “sandbox” directories, on files you have created specifically to learn cli commands. I usually make a new directory, then copy a few files in to play with, so if I screw up, it’s no problem.
Rand writes about web design, men’s health and nitrile gloves. Please check out his new website all about allergy free Nitrile Gloves for info and nitrile glove know how! Check out the Nitrile Gloves vs. Latex Gloves page to find out how nitrile stacks up against latex.