Unix also includes commands for copying and moving files: cp and mv. These commands enable you to copy or move one or more source files to a target file or directory.
Why copy a file? Usually, to make a backup. For instance, before you edit a configuration file, you should create a backup copy of the original. This way you can revert back to the original if your edits "break" something in the file.
The mv command can also be used to rename a file.
The cp and mv commands support several options. You can learn more about them in the man pages for these commands. Type man cp or man mv and press
Unix does not confirm that a file has been copied or moved when you correctly enter a command (Figures 21 and
22 ). To check to see if a file has been copied or moved to the correct destination, you can use the ls command to get a listing for the target directory. The ls command is covered earlier in this chapter.
mv command in use.
Type cp
source-file target-file and press
For example, cp file.conf file.conf-orig would duplicate the file named
file.conf and assign the name
file.conf-orig to the duplicate copy.
The
source-file and
target-file names must be different.
The
source-file operand can be a file or a directory.
Type cp
source-file …
target-directory and press
For example, cp file.conf /Users/ronh/Documents would copy the file named
file.conf in the current directory to the directory named
Documents in my home folder.
Type cp followed by the wildcard search string for the source file and the name of the target directory and press
For example, cp *.conf Originals would copy all files ending with
.conf in the current directory to the subdirectory named
Originals .
Type mv
source target and press
For example, mv file.conf file.conf-backup would rename
file.conf as
file.conf-backup .
Type mv
source …
directory and press
For example, mv file.conf Documents would move the file named
file.conf in the current directory to the subdirectory named
Documents .
Type mv
source directory/filename and press