Copy Files


cp

Making copies of files is one of those things that computer users, no matter the OS, find themselves doing all the time. One of the most venerable commands used by the Linux shell is cp, which copies files and directories. The easiest way to use cp is simply to type in the command, followed by the file you're wanting to copy, and then the copied file's new name; think of the command's structure as "cp file-you're-copying-from file-you're-copying-to." Another common way to express that relationship is "cp source target."

$ pwd /home/scott/libby $ ls libby.jpg $ cp libby.jpg libby_bak.jpg $ ls libby_bak.jpg libby.jpg 


This example is pretty simple: The picture is copied into the same directory as the original file. You can also copy files to another directory, or even copy files from a directory in which you're not currently located to another directory located somewhere else on your file system.

$ pwd /home/scott $ ls ~/libby libby_bak.jpg libby.jpg $ cp pix/libby_arrowrock.jpg libby/arrowrock.jpg $ ls ~/libby arrowrock.jpg libby_bak.jpg libby.jpg 


The same filename, libby_closeup.jpg, is used in this example, which is okay because the file is being copied into another directory entirely. In the first example for cp, however, you had to use a new name, libby_bak.jpg instead of libby.jpg, because you were copying the file into the same directory.

If you want to copy a file from another directory into your working directory (the one in which you currently find yourself), simply use .. (Remember how you learned earlier in this chapter that . means "the current directory?" Now you see how that information can come in handy.) Of course, you can't change the name when you use . because it's a shortcut for the original filename.

$ pwd /home/scott/libby $ ls libby_bak.jpg libby.jpg $ cp pix/libby_arrowrock.jpg . $ ls arrowrock.jpg libby_bak.jpg libby.jpg 


You don't need to specify a filename for the target if that target file is going to reside in a specific directory; instead, you can just provide the directory's name.

$ ls -l drwxr-xr-x   224 2005-10-20 12:34 libby drwxr-xr-x   216 2005-09-29 23:17 music drwxr-xr-x  1.6K 2005-10-16 12:34 pix $ ls libby arrowrock.jpg libby.jpg $ cp pix/libby_on_couch.jpg libby $ ls libby arrowrock.jpg libby.jpg libby_on_couch.jpg 


In the previous example, you need to be certain that a directory named libby already exists for libby_on_couch.jpg to be copied into, or you would have ended up with a file named libby in your home directory.



Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net