Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Complete Idiot's Guide to Linux
(Publisher: Macmillan Computer Publishing)
Author(s): Manuel Ricart
ISBN: 078971826x
Publication Date: 12/22/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Copying Files: cp

To copy of a file, use the cp command. The cp has the following syntax:

     cp [options] source destination

source is the name of the file you want to copy, and destination is the name of the copy you create.

If destination is a directory (the path didn’t specify a filename), the file is copied with its original name into the specified directory.

The options argument is shown between brackets to say that options are optional. We’ll use an option on the cp command in the next section.

Here’s an example:

     [alberto@digital alberto]$ ls -F
     Xrootenv.0   afile        anotherlink  helloworld*
     adir/        alink@       hello

     [alberto@digital alberto]$ cp helloworld /tmp
     
     [alberto@digital alberto]$ ls -F /tmp
     fvwmrca00455  helloworld*   install.log   screens/

You can copy multiple files to a single directory in a single command by listing several source files separated by spaces. The only requirement is that the last argument (destination) be a directory.

Copying Directories

You copy directories in much the same way you copy files. When copying directories you need to tell cp that you want to copy recursively all the files and directories contained by the source directory. This is easily done with the -R option. The syntax looks like this:

     [alberto@digital alberto]$ ls
     Xrootenv.0   adir/        afile        alink@   anotherlink
       helloworld*
     [alberto@digital alberto]$ cp -R adir /tmp
     [alberto@digital alberto]$ ls /tmp
     adir/        install.log

Moving Files and Directories: mv

To move a file or directory, you use the mv command. Moving files is similar to copying files. The big difference is that the original file is removed after the new copy is created. The mv command follows this syntax:

     mv [options] source destination

     [alberto@digital alberto]$ ls
     Xrootenv.0   adir/        afile        alink@       anotherlink
       helloworld*
     [alberto@digital alberto]$ mv afile file2
     [alberto@digital alberto]$ ls
     Xrootenv.0   adir/        alink@       anotherlink  file2
       helloworld*

The mv command can also be used to move an entire directory tree.

The mv command provides several options. For more information, refer to its manual page.


Note:  
To read documentation on a UNIX tool or command, refer to Chapter 15, “Help Please.”


Techno Talk:  Symbolic Links
A link is a pointer to a file or folder that is somewhere else in the file system. A link looks and acts like the file or folder it points to. If you are viewing it through KFM, it will have the same icon (with the exception that it will have a little arrow next to it). If you specify the -F flag to the ls command, you’ll see an at symbol (@) attached to the end of the link’s filename. When you open a link, you see the same file contents of the file the link points to.

Links are very useful as an organizational tool. They allow you to conveniently access files that might be stored deeply in the file system (and therefore have a long and difficult-to-type file path) or even in a different part of the network. Instead of traversing a long path to access a file, you can just access a conveniently located link.

Links can also be used to save disk space. Instead of copying large files to have them handy, you can create links to them where convenient. Links take very little disk space, just a few bytes.

A symbolic link stores the file path of the file it points to. You can move and rename the link without affecting it. However, if the original file—the file that the link points to—is moved, deleted, or renamed, the link breaks. Broken links can be fixed by restoring the original file to its proper location and name. Otherwise they probably should be deleted. Symbolic links associate a file path with a file.



Techno Talk:  Hard Links
There’s another type of link called a hard link. A hard link is similar to a symbolic (or soft) link in that it provides a different name for accessing a file. However, that is where similarities end. Hard links have one advantage: Unlike soft links, which break if you move, delete, or rename the original file, hard links don’t.

Hard links don’t really point to a file path as the soft links do; instead, they point to the actual data stored on a disk. For this reason, all files have at the very least one link: the name assigned to the file when it was created. When you add another hard link to a file, the file system treats it as if the file had a different name, but the data the link points to is the same as the original file. If you delete the original file, the remaining link still points to the same data on the disk.

As you can see, hard links are tied to the actual disk storage where a file is written. For this reason, hard links cannot cross disk boundaries.

Another small difference is that hard links only reference files—never directories. The only way you can tell a hard link from a file when you use the option -i and -l to ls and ls reports a hard link count greater than one and the inode address printed is the same:

  [alberto@digital alberto]$ ls -il
   169688 -rwxrwxr-x   2 alberto  alberto        74 Aug 26 12:19 bye.pl
   169972 -rwxrwxrwx   1 alberto  alberto        65 Aug 30 14:49
    capture.pl
   169688 -rwxrwxr-x   2 alberto  alberto        74 Aug 26 12:19
    hello.pl

In the preceding example, both hello.pl and bye.pl have the same inode: 169688. Notice, too, that both files say they have two hard links. Both files are actually the same file with different names!



Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.