Exercise 10: Copy a File (cp)
In this exercise you learn how to copy a file from one location to another with the cp command.
Do This
Linux/OSX
$ cd temp
$ cp iamcool.txt neat.txt
$ ls
iamcool.txt neat.txt
$ cp neat.txt awesome.txt
$ ls
awesome.txt iamcool.txt neat.txt
$ cp awesome.txt thefourthfile.txt
$ ls
awesome.txt iamcool.txt neat.txt thefourthfile.txt
$ mkdir something
$ cp awesome.txt something/
$ ls
awesome.txt iamcool.txt neat.txt something thefourthfile.txt
$ ls something/
awesome.txt
$ cp -r something newplace
$ ls newplace/
awesome.txt
$
Windows
> cd temp
> cp iamcool.txt neat.txt
> ls
Directory: C:\Users\zed\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 12/22/2011 4:49 PM 0 iamcool.txt
-a--- 12/22/2011 4:49 PM 0 neat.txt
> cp neat.txt awesome.txt
> ls
Directory: C:\Users\zed\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 12/22/2011 4:49 PM 0 awesome.txt
-a--- 12/22/2011 4:49 PM 0 iamcool.txt
-a--- 12/22/2011 4:49 PM 0 neat.txt
> cp awesome.txt thefourthfile.txt
> ls
Directory: C:\Users\zed\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 12/22/2011 4:49 PM 0 awesome.txt
-a--- 12/22/2011 4:49 PM 0 iamcool.txt
-a--- 12/22/2011 4:49 PM 0 neat.txt
-a--- 12/22/2011 4:49 PM 0 thefourthfile.txt
> mkdir something
Directory: C:\Users\zed\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 12/22/2011 4:52 PM something
> cp awesome.txt something/
> ls
Directory: C:\Users\zed\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 12/22/2011 4:52 PM something
-a--- 12/22/2011 4:49 PM 0 awesome.txt
-a--- 12/22/2011 4:49 PM 0 iamcool.txt
-a--- 12/22/2011 4:49 PM 0 neat.txt
-a--- 12/22/2011 4:49 PM 0 thefourthfile.txt
> ls something
Directory: C:\Users\zed\temp\something
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 12/22/2011 4:49 PM 0 awesome.txt
> cp -recurse something newplace
> ls newplace
Directory: C:\Users\zed\temp\newplace
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 12/22/2011 4:49 PM 0 awesome.txt
>
You Learned This
Now you can copy files. It's simple to just take a file and copy it to a new one. In this exercise I also make a new directory and copy a file into that directory.
I'm going to tell you a secret about programmers and system administrators now. They are lazy. I'm lazy. My friends are lazy. That's why we use computers. We like to make computers do boring things for us. In the exercises so far you have been typing repetitive boring commands so that you can learn them, but usually it's not like this. Usually if you find yourself doing something boring and repetitive there's probably a programmer who has figured out how to make it easier. You just don't know about it.
The other thing about programmers is they aren't nearly as clever as you think. If you overthink what to type, then you'll probably get it wrong. Instead, try to imagine what the name of a command is to you and try it. Chances are that it's a name or some abbreviation similar to what you thought it was. If you still can't figure it out intuitively, then ask around and search online. Hopefully it's not something really stupid like ROBOCOPY.
Do More
- Use the cp -r command to copy more directories with files in them.
- Copy a file to your home directory or desktop.
- Find these files in your graphical user interface and open them in a text editor.
- Notice how sometimes I put a / (slash) at the end of a directory? That makes sure the file is really a directory, so if the directory doesn't exist I'll get an error.