- Listing Files
- File Contents
- Manipulating Files
- Summary
- Terms
Manipulating Files
In the preceding sections, you looked at listing files and viewing their content. In this section, you will look at copying, renaming, and removing files using the cp, mv, and rm commands.
Copying Files (cp)
The cp command (short for copy) is used to make a copy of a file. The basic syntax of the command is
cp src dest
Here src is the name of the file to be copied (the source) and dest is the name of the copy (the destination). For example, the following command creates a copy of the file fruits in a file named fruits.sav:
$ cp fruits fruits.sav
If dest is the name of a directory, a copy with the same name as src is created in dest. For example, the command
$ cp fruits Documents/
creates a copy of the file fruits in the directory Documents.
It is also possible to specify multiple source files to cp, provided that the destination, dest, is a directory. The syntax for copying multiple files is
$ cp src1 ... srcN dest
Here src1 ... srcN are the source files and dest is the destination directory. As an example, the following command
$ cp fruits users Documents/
creates a copy the files fruits and users in the directory Documents.
Interactive Mode
The default behavior of cp is to automatically overwrite the destination file if it exists. This behavior can lead to problems. The -i option (short for interactive) can be used to prevent such problems. In interactive mode, cp prompts for confirmation before overwriting any files.
Assuming that the file fruits.sav exists, the following command
$ cp -i fruits fruits.sav
results in a prompt similar to the following:
overwrite fruits.sav? (y/n)
If y (yes) is chosen, the file fruits.sav is overwritten; otherwise the file is untouched. The actual prompt varies among the different versions of UNIX.
Common Errors
When an error is encountered, cp generates a message. Some common error conditions follow:
The source, src, is a directory.
The source, src, does not exist.
The destination, dest, is not a directory when multiple sources, src1 ... srcN, are specified.
A non-existent destination, dest, is specified along with multiple sources, src1 ... srcN.
One of the sources in src1 ... srcN is not a file.
The first error type is illustrated by the following command:
$ cp Downloads/ fruits
Because src (Downloads in this case) is a directory, an error message similar to the following is generated:
cp: Downloads: is a directory
In this example, dest was the file fruits; the same error would have been generated if dest was a directory.
The second error type is illustrated by the following command:
$ cp fritus fruits.sav cp: cannot access fritus: No such file or directory
Here the filename fruits has been misspelled fritus, resulting in an error. In this example dest was the file fruits.sav; the same error would have been generated if dest was a directory.
The third error type is illustrated by the following command:
$ cp fruits users fruits.sav usage: cp [-R [-H | -L | -P]] [-f | -i] [-p] src target cp [-R [-H | -L | -P]] [-f | -i] [-p] src1 ... srcN directory
Because dest, in this case fruits.sav, is not a directory, a usage statement that highlights the proper syntax for a cp command is presented. The output might be different on your system because some versions of cp do not display the usage information.
If the file fruits.sav does not exist, the error message is
cp: fruits.sav: No such file or directory
This illustrates the fourth error type.
The fifth error type is illustrated by the following command:
$ cp fruits Downloads/ users Documents/ cp: Downloads is a directory (not copied).
Although cp reports an error for the directory Downloads, the other files are correctly copied to the directory Documents.
Renaming Files (mv)
The mv command (short for move) can be used to change the name of a file. The basic syntax is
mv src dest
Here src is the original name of the file and dest is the new name of the file. For example, the command
$ mv fruits fruits.sav
changes the name of the file fruits to fruits.sav. There is no output from mv if the name change is successful.
If src does not exist, an error will be generated. For example,
$ mv cp fritus fruits.sav mv: fritus: cannot access: No such file or directory
Similar to cp, mv does not report an error if dest already exists. The old file is automatically overwritten. This problem can be avoided by specifying the -i option (short for interactive). In interactive mode, mv prompts for confirmation before overwriting any files.
Assuming that the file fruits.sav already exists, the command
$ mv -i fruits fruits.sav
results in a confirmation prompt similar to the following:
overwrite fruits.sav?
If y (yes) is chosen, the file fruits.sav is overwritten; otherwise the file is untouched. The actual prompt varies among the different versions of UNIX.
Removing Files (rm)
The rm command (short for remove) can be used to remove or delete files. Its syntax is
rm file1 ... fileN
Here file1 ... fileN is a list of one or more files to remove. For example, the command
$ rm fruits users
removes the files fruits and users.
Because there is no way to recover files that have been removed using rm, you should make sure that you specify only those files you really want removed. One way to ensure this is by specifying the -i option (short for interactive). In interactive mode, rm prompts before removing every file. For example, the command
$ rm -i fruits users
produces confirmation prompts similar to the following:
fruits: ? (n/y) y users: ? (n/y) n
In this case, you answered y (yes) to removing fruits and n (no) to removing users. Thus, the file fruits was removed, but the file users was untouched.
Common Errors
The two most common errors when using rm are
One of the specified files does not exist.
One of the specified files is a directory.
The first error type is illustrated by the following command:
$ rm users fritus hosts rm: fritus non-existent
Because the file fruits is misspelled as fritus, it cannot be removed. The other two files are removed correctly.
The second error type is illustrated by the following command:
$ rm fruits users Documents/ rm: Documents directory
The rm command is unable to remove directories and presents an error message stating this fact. It removes the two other files correctly.