Copying & Moving Files
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
to view each command's man pages.
- 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.
Figure 21. Here's the cp command in action.
Figure 22. Here's the
mv command in use.
To copy a file to the same directory
Type cp source-file target-file and press

- The source-file and target-file names must be different.
- The source-file operand can be a file or a directory.
To copy files to another directory
Type cp source-file … target-directory and press

To copy files using a wildcard
Type cp followed by the wildcard search string for the source file and the name of the target directory and press

To rename a file
Type mv source target and press

To move files to another directory
Type mv source … directory and press

To move a file to another directory & rename it
Type mv source directory/filename and press
