DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Working with files and directories

Removing a file

To remove (or destroy) a file, use the rm(C) command, as follows:

rm filename

Once a file is removed from the system, there is no way of getting it back unless a backup exists on tape or floppy disk, or the filename is a link, or versioning is available. Links are explained in ``Creating links to files and directories''; file versioning is explained in ``Retrieving deleted files''.

You can list several files to be removed, or use wildcards to select files. You cannot remove directories with this form of the rm command.


NOTE: It is potentially dangerous to use wildcards to remove files. Before doing so, you should confirm that the correct files have been selected: do so by running the ls command in place of rm. Because the expansion of any filename notation is handled by the shell and not by the individual command, the files selected by ls are the same as those that will be selected by rm.

To remove files interactively, use the -i option, as follows:

rm -i filename1 filename2 . . .

rm with the -i option asks for confirmation before removing a file. A question mark is displayed and you can either type ``y'' to remove the file, or ``n'' to not remove it. It is a good idea to use rm -i to reduce the risk of accidentally removing files. For example, to remove several files from the current directory:

   $ rm -i f*
   file1: ?y
   file2: ?y
   file3: ?y
   format.doc: ?n
As a further safeguard, it may be useful to create an alias, whereby executing rm -r * actually executes rm -ir *: the -i option causes rm to delete files interactively; that is, you must confirm the deletion of each file before it is carried out. See ``Using aliases'' for details of how to create an alias.

Note that using wildcards does not remove hidden files (those whose name begins with a dot); that is, typing rm * does not necessarily remove all the files in a directory. To list the hidden files, type ls -a. For example, if you have a file called .project, you can remove it by typing the following:

   $ rm .project
Remember that there are always at least two files that cannot be removed from a directory; ``.'' (the current directory), and ``..'' (the parent directory).

You can remove a file from a directory other than your current one if you have write permissions on that directory.

Removing files with difficult names

Occasionally, files are created by accident with awkward names. For example, they might contain a slash (/) or an asterisk (*) character. These files cannot be removed by normal means without the risk of destroying other files, because if you try to type their names, the shell will interpret the special characters as wildcards.

For example, suppose you have a directory that contains a corrupted file called all * file and a number of files called file1 and file2 that you want to keep. If you type rm all * file, rm will interpret the ``*'' in the filename as a wildcard, and attempt to execute the following:

rm all file1 file2 file

This command thereby inadvertently deletes file1 and file2.

To correctly remove files with corrupted names, the easiest solution is to use the rm -i option. In this case, rm will prompt you for confirmation before removing each of the specified files in the current directory; type ``n'' for each file other than the corrupt one you want to remove, as follows:

   $ rm -i a*
   all * file: ? y
Alternatively, specify the name of the file, surrounding it with single quotes:
   $ rm 'all * file'
The single quotes prevent the shell from expanding the special character ``*'' in the file's name.

If you have a file that begins with a hyphen (-), rm will mistake its name for an option of some kind. For example, if your file is called -myfile, rm -myfile will be mistaken for an invalid rm command. You can overcome this by invoking rm with the special option, --, which tells rm that the following argument is not an option:

   $ rm -- -myfile
See ``Regular expressions'' for an explanation of shell wildcards. See ``Filenaming conventions'' for an explanation of what constitutes an illegal filename.
Next topic: Comparing files
Previous topic: Moving or renaming a file

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003