previous contents up next

Unix for Advanced Users

6. Manipulating Files

6.3. How do I move, copy, and delete files?

These are some of the most simple tasks any user needs to execute. The commands to carry them out are correspondingly simple.

6.3.1. mv

mv moves a file to another location. A typical invocation is mv file directory. If the second argument is a file rather than a directory, mv will rename the first file, so mv junk spam will change the name of the file junk to spam. The command is unforgiving: If you mv a file to the name of a second file that already exists, that second file will be deleted. For that reason, you must use mv with caution.

mv can be used to move and rename directories as well as files.

6.3.2. cp

cp copies a file. It has the same syntax as mv, but leaves the original file in place. cp will also copy over files without prompting, so it, too, requires caution. cp -r will copy a directory, its subdirectories, and its subdirectories' subdirectories recursively.

6.3.4 rm

rm stands for remove; it is used to delete files. rm -r directory deletes recursively, just as cp -r copies recursively. However, a separate command, rmdir, serves to remove empty directories.

In general, it is safer to rm individual files and rmdir directories, rather than to use rm -r. It is easy to overlook important files when you delete recursively.

previous contents up next