< Zurück | Inhalt | Weiter >

1.3.5 File Copying

Do you know these commands?




The mv command (short for “move”) lets you move a file from one place in the hierarchy of files to another—that is, from one directory to another. When you move the file, you can give it a new name. If you move it without putting it in a different directory, well, that’s just renaming the file.

mv Classy.java Nouveau.java

mv Classy.java /tmp/outamy.way

mv Classx.java Classz.java ..

mv /usr/oldproject/*.java .

The first example moves Classy.java to a new name, Nouveau.java, while leaving the file in the same directory.

The second example moves the file named Classy.java from the current directory over to the /tmp directory and renames it outamy.way—unless the file outamy.way is an already existing directory. In that case, the file Classy.java will end up (still named Classy.java) inside the directory outamy.way.

The next example just moves the two Java source files up one level, to the parent directory. The “..” is a feature of every Linux directory. Whenever you create a directory, it gets created with two links already built in: “..” points to its parent (the directory that contains it), and “.” points to the directory itself. A common question at this point is, “Why does a directory need a refer- ence to itself?” Whatever other reasons there may be, it certainly is a handy shorthand to refer to the current directory. If you need to move a whole lot of files from one directory to another, you can use the “.” as your destination.

That’s the fourth example.

The cp command is much like the mv command, but the original file is left right where it is. In other words, it copies files instead of moving them. So:

cp Classy.java Nouveau.java

will make a copy of Classy.java named Nouveau.java, and:

cp Classy.java /tmp

will make a copy of Classy.java in the /tmp directory, and:

cp *.java /tmp

will put the copies of all the Java sources in the current directory to the /tmp


If you run this command,

ln Classy.java /tmp

you might think that ln copies files, too. You will see Classy.java in your present working directory and you will see what appears to be a copy of the file in the /tmp directory. But if you edit your local copy of Classy.java and then look at the “copy” that you made in the /tmp directory, you will see the changes that you made to your local file now also appear in the file in the /tmp directory.

That’s because ln doesn’t make a copy. It makes a link. A link is just an-

other name for the same contents. We will discuss linking in detail later in the book (see Section 6.2.1).