-->

Previous | Table of Contents | Next

Page 69

The mv command can work silently, or as with rm, you can use the -i (interactive) option, for example:


# mv -i file2 file3

# mv -i file2 file3

mv: replace `file3'? y

Here, the mv command asks if you want to overwrite the file (but will keep quiet if no overwriting takes place, even if you use -i). You can also combine the -b and -i options with


# mv -bi file2 file3

Now that you've seen how to delete, rename, or move your files, how do you copy files?

Copying with the cp Command

The cp, or copy, command is used to copy files or directories. This command has nearly 40 command-line options. They won't all be covered here, but you'll learn about some of the most commonly used options, which will save you time and trouble.

You'll most likely first use cp in its simplest form, for example:


# cp file1 file2

This creates file2, and unlike mv, leaves file1 in place. But you must be careful when using cp, because you can copy a file onto a second file, effectively replacing it! In this regard, cp can act just like mv. To show you how this can happen, try creating three files, each with a line of text, using the cat command:


# cat > file1

this is file1

# cat > file2

this is file 2

# cat > file3

this is the third file

# ls -l file*

-rw-rw-r--   1 bball    bball          14 Nov 13 16:12 file1

-rw-rw-r--   1 bball    bball          15 Nov 13 16:12 file2

-rw-rw-r--   1 bball    bball          23 Nov 13 16:12 file3

Now, copy a file onto another file, then check the file sizes and the contents of the new file:


# cp file1 file2

# ls -l file*

-rw-rw-r--   1 bball    bball          14 Nov 13 16:12 file1

-rw-rw-r--   1 bball    bball          14 Nov 13 16:14 file2

-rw-rw-r--   1 bball    bball          23 Nov 13 16:12 file3

# cat file2

this is file1

It should be obvious that file1 has replaced file2. To avoid this problem (unless you really want to overwrite the file), you can use the -b or -i options, which work just like mv. Here's an example:

Page 70


# cp -i file1 file2

cp: overwrite `file2'? n

# cp -bi file1 file2

cp: overwrite `file2'? y

# ls file*

file1   file2   file2~  file3

Note that file2, which was overwritten, was backed up. The cp command may also be used to copy a number of files at one time. The following example shows how to copy all of the files in directory tempdir1 to directory tempdir2:


# cp tempdir1/* tempdir2

# tree tempdir2

tempdir2

|-- temp1file1

|-- temp1file2

`-- temp1file3



0 directories, 3 files

Like the rm command, cp also has a -r, or recursive, option. You can use this option to copy one directory into another. For example, to copy the tempdir1 directory and its files into tempdir2, use this syntax:


# cp -r tempdir1 tempdir2

# tree tempdir2

tempdir2

|-- temp1file1

|-- temp1file2

|-- temp1file3

`-- tempdir1

    |-- temp1file1

    |-- temp1file2

    `-- temp1file3



1 directory, 6 files

Finally, the cp command has the -p option, which is similar to mkdir's -p option. Normally, when you copy a file inside several directories into another directory, only the file is copied. The following example will only copy temp1file1 into tempdir3:


# tree tempdir2

tempdir2

|-- temp1file1

|-- temp1file2

|-- temp1file3

`-- tempdir1

    |-- temp1file1

    |-- temp1file2

    `-- temp1file3



1 directory, 6 files

Page 71


# cp tempdir2/tempdir1/temp1file1 tempdir3

However, what if you'd like a file, along with its directory structure, copied? To do this, you can use the -P option:


# cp -P tempdir2/tempdir1/temp1file1 tempdir3

# tree tempdir3

tempdir3

`-- tempdir2

    `-- tempdir1

        `-- temp1file1



2 directories, 1 file

Not only has cp copied a single file, but it has also created the subdirectory structure.

Creating Hard and Symbolic Links
with the ln Command

Linux supports both hard and symbolic links. Although it is not important that you understand how links work in Linux, you should understand the difference between these two types of links and how to use links while you use Linux. To create hard or symbolic links, you use the ln, or link, command.

The ln command creates both types of links. If you use the ln command to create a hard link, you specify a second file on the command line you can use to reference the original file, for example:


# cat > file1

This is file1

# ln file1 file2

# ls -l file*

-rw-rw-r--   2 bball    bball          14 Nov 13 18:54 file1

-rw-rw-r--   2 bball    bball          14 Nov 13 18:54 file2

# cat file2

This is file1

You can see that file2 is exactly the same as file1. If you delete file1, file2 will remain. If you make changes to file1, such as adding text, these changes will appear in file2, and if you make changes to file2, file1 will also be updated. You should also know that although you can see two files, each 14 characters in size, only 14 characters of hard drive space are used (OK, technically more than that, but it depends on the block size of the partition or type of hard drive).

On the other hand, although a symbolic link can be useful, it also has a drawback. The next examples show you why. First, to create a symbolic link, use the ln command -s option:


# ln -s file1 file2

Previous | Table of Contents | Next