mv
This command will move a file. You can use mv
not only to change the directory location of a file, but also to rename files. Unlike the cp
command, mv
will not preserve the original file.
Note: As with the cp
command, you should always use -i
to make sure you do not overwrite an existing file.
To rename a file named oldname
in the current directory to the new name newname
, enter:
To move a file named hw1
from a subdirectory named newhw
to another subdirectory named oldhw
(both subdirectories of the current directory), enter:
If, in this last operation, you also wanted to give the file a new name, such as firsthw
, you would enter:
mv -i newhw/hw1 oldhw/firsthw
cp
This command copies a file, preserving the original and creating an identical copy. If you already have a file with the new name, cp
will overwrite and destroy the duplicate. For this reason, it's safest to always add -i
after the cp
command, to force the system to ask for your approval before it destroys any files. The general syntax for cp
is:
To copy a file named meeting1
in the directory /home/dvader/notes
to your current directory, enter:
The .
(period) indicates the current directory as destination, and the -i
ensures that if there is another file named meeting1
in the current directory, you will not overwrite it by accident.
To copy a file named oldfile
in the current directory to the new name newfile
in the mystuff
subdirectory of your home directory, enter:
The ~
character (tilde) is interpreted as the path of your home directory.
Note: You must have permission to read a file in order to copy it.