|Previous||Table of Contents||Next|
The external rm command removes (deletes) files or directories. The remove command actually removes the specified links to a file. Since a file can have more than one link, performing a remove may not delete the contents of the file. Instead only the reference to the file is lost. Another reference may still exist. If all links (filenames) to the file are removed, the file is deleted and the contents are lost. You cannot undelete a file on a UNIX System.
The formats of the rm command follow.
rm [ -fi ] file_list rm [ -fir ] directory_list [ file_list ]
|rm [ -fi ] [ - ] file_list
rm [ -fir ] [ - ] directory_list [ file_list ]
The following list describes the options used to control how rm functions.
|-f||Forces the removal of all files listed. Does not check to see if the file is writeprotected or not. Does not prompt for confirmation. If the directory is writeprotected the files are NEVER removed.|
|-i||Interactive file removal. Before each file is removed you are requested to respond with a confirmation. Any string beginning with a "y" removes the file. If used with the -r option, confirmation is asked for each directory. The -i option overrides the -f option and is effective even if the standard input is not your terminal keyboard.|
|-r||Recursively removes files and directories. All files are removed from each directory. Each directory is then removed. If a file is writeprotected (you do not have write permission) then rm prompts you for confirmation to remove the file.|
|-||Informs rm that the following arguments are filenames, not options. This allows you to remove files beginning with a - that would normally be interpreted as invalid options to remove.|
The following list describes the arguments that may be passed to the rm command.
|directory_list||One or more directories to be searched for files to remove. If the -r option is used, all files under the listed directory are removed and the directories themselves are deleted.|
|file_list||One or more files unlinked from the inode and removed from the directory file.|
The rm command removes the entries for one or more files from a directory, thus removing a link to the inode. If the last link pointing to the inode is removed, then the contents of the file can no longer be referenced. The inode and contents are still on the disk but you have no way to access them.
The system administrator may be able to use the fsdb program to relink the file if it is important and you do not have a backup. But don't bank on this possibility. Most system administrators are not familiar with fsdb. Plus, the file system has to be umounted immediately after the file is removed. The next file allocation request may use the inode you just freed, leaving absolutely no record that the file ever existed.
You do not have to have read or write permissions on a file to remove it. Although you do have to have write permission for the directory to remove a file from the directory. If you do not have write permissions for the file and you are using rm from your terminal, rm prompts with the file's permissions and waits for a confirmation. To perform the removal, type any string beginning with a "y."
The -r command must be present for rm to remove a directory.
TIP: If you are not sure how the shell expands an ambiguous file reference, use the echo command to evaluate which files are referenced. Then use rm -i to remove the desired files.
DIAGNOSTICS AND BUGS
The rm command will not allow you to remove your current directory (.) or your parent directory (..). For example, the following command
rm -r .*
will not remove your present working directory or your parent directory.
Refer to the chmod, ln, and rmdir commands described in modules 17, 76, and 115, respectively.
The rm command returns an exit code of zero if all files and directories are removed successfully. If any file or directory is not removed, then a nonzero status is returned.
The rm command is used to remove unwanted files. Old versions or copies of files can be discarded. Multiple links to a single file can be reduced to only one link for the file. It can be used to clean up entire directory tree structures after the structure has been moved to a new location or saved to tape. You will use rm repeatedly to clean up disk space.
Be sure you reference only the files you no longer need. Once you remove a file on UNIX it is gone forever. The only way to recover it is from a tape or floppy backup!
In this activity you use the rm command to remove a file in a subdirectory but leave the directory intact. Begin at the shell prompt.
cj> rm -ri letters letters/calendar? y letters/file1? y letter?
|Previous||Table of Contents||Next|