Java Reference
In-Depth Information
1.3.5
Do you know these commands?
File Copying
•
mv
•
cp
•
ln
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