Copying and moving

« Back to list of missions

Missions: Using command line shell

Copying and moving

cp: copy files and directories

The cp command will make a copy of a file for you. For example,

$ cp songs.txt music.txt

will make an exact copy of songs.txt and name it
music.txt (the file songs.txt
will still be there, too).

If you are copying a directory, you must use the option
-r (--recursive). For example,

$ cp -r songs music

copies all the contents of the songs directory to the
music directory recursively. Remember when we
explained how the recursive option worked when removing directories? Here,
“recursively” means we copy the directory and all its files and subdirectories,
and all the subdirectories’ files and subdirectories, and on and on (“recursively”)
until everything that was inside the songs directory has
been copied to the music directory.

You can also use source and destination paths:

$ cp source-path destination-path

For example,

$ cp songs.txt music/list/

will copy the file songs.txt to a subdirectory
list inside the music directory
(which is in the current working directory).

mv: move (rename) files

The mv command will rename a file or move a file to a
different location. Examples are as follows:

$ mv songs.txt music.txt

will rename the file songs.txt to music.txt.

$ mv songs.txt ~/Desktop

will move the file songs.txt to your
Desktop directory, but it will not rename it.
You must specify a new file name to rename a file.