6.0 Copying, Moving, And Removing Files

Three file-manipulation commands are cp, mv, and rm, which make copies of, rename, and remove a file (or set of files), respectively. The cp and mv commands expect two file names as arguments (the old and new names), while rm expects a list of one or more files to be removed.
To make a copy of filename.old while keeping the original file intact, type:
cp filename.old filename.new
and press RETURN. This will leave filename.old untouched with a new duplicate named filename.new. To copy it into a new directory, replace filename.new with the path of the directory
To rename filename.old as filename.new, type:
mv filename.old filename.new
and press RETURN. The result is filename.new--one file with a new name. CAUTION: If filename.new already exists, the mv command will overwrite that original file without warning; it will be lost. When you use the mv command, make sure that the new filename you assign does not already exist.

To remove filename.old, type rm filename.old and press RETURN.

Addition of the -i flag causes each utility to prompt with a ? before a destructive action is taken (one that would erase or overwrite a file); type y to continue at this prompt.

When the argument is a directory name (see Section 10.0, "Directory Structure"), the -r flag (not needed with mv) causes the action to be carried out recursively for all files and subdirectories within that directory. Thus typing cp -r oldDirectory. newDirectory will create a parallel directory, newDirectory, which exactly matches its structure, and to which all files and subdirectories in oldDirectory will be copied. Likewise, typing
rm -r directoryName
will delete all files and subdirectories within the directory called directoryName, as well as deleting that directory itself.

Next Section Previous Section Contents