rm Command

Purpose

Removes (unlinks) files or directories.

Syntax

rm [  -f ] [  -r ] [  -R ] [  -i ] [  -e ] File ...

Description

The rm command removes the entries for the specified File parameter from a directory. If an entry is the last link to a file, the file is then deleted. If you do not have write permission for a file and the standard input is a terminal, you are prompted with the file name and ask to confirm that you want to delete the file. If you type a y (for yes), the file is deleted, type any other character and the file is not deleted. You do not need read or write permission for the file you want to remove. However, you must have write permission for the directory containing the file.

If the file is a symbolic link, the link is removed, but the file or directory that the symbolic link refers to remains. You do not need write permission to delete a symbolic link, if you have write permission in the directory.

If either of the files . (dot) or .. (dot, dot) are specified as the base name portion of the File parameter, the rm command writes a diagnostic message to standard error and does nothing more with such parameters.

The rm command writes a prompt to standard error and reads a line from standard input if the -f flag is not specified, and either the File parameter does not have write permission and the standard input is a workstation, or the -i flag is specified. If the response is not affirmative, the rm command does nothing more with the current file and proceeds to the next file.

The files owned by other users cannot be removed if the sticky bit of the directory is set and the directory is not owned by the user.

Note: The rm command supports the (dash, dash) parameter as a delimiter that indicates the end of the flags.

An attempt to remove a file or directory that has been exported for use by the NFS version 4 server will fail with a message saying that the resource is busy. The file or directory must be unexported for NFS version 4 use before it can be removed.

Flags

Item Description
-e Displays a message after each file is deleted.
-f Does not prompt before removing a write-protected file. Does not display an error message or return error status if a specified file does not exist. If both the -f and -i flags are specified, the last one specified takes affect.
-i Prompts you before deleting each file. When you use the -i and -r flags together, the rm command also prompts before deleting directories. If both the -i and -f flags are specified, the last one specified takes affect.
Item Description
-r Permits recursive removal of directories and their contents when the File parameter is a directory. This flag is equivalent to the -R flag.
-R Permits recursive removal of directories and their contents when the File parameter is a directory. This flag is equivalent to the -r flag.

Exit Status

This command returns the following exit values:

Item Description
0 If the -f flag was not specified, all the named directory entries were removed; otherwise, all the existing named directory entries were removed.
>0 An error occurred.

Security

Attention RBAC users and Trusted AIX® users: This command can perform privileged operations. Only privileged users can run privileged operations. For more information about authorizations and privileges, see Privileged Command Database in Security. For a list of privileges and the authorizations associated with this command, see the lssecattr command or the getcmdattr subcommand.

Examples

  1. To delete a file, enter:
    rm myfile
    If there is another link to this file, then the file remains under that name, but the name myfile is removed. If myfile is the only link, the file itself is deleted.
  2. To delete a file without first receiving a confirmation prompt, enter:

    rm  -f core

    No confirmation prompt is issued before the rm -f command attempts to remove the file named core. However, an error message displays if the core file is write-protected and you are not the owner of the file or you do not have root authority. No error message displays when the rm -f command attempts to remove nonexistent files.
  3. To delete files one by one, enter:

    rm  -i mydir/*

    After each file name is displayed, enter y to delete the file, or press the Enter key to keep it.
  4. To delete a directory tree, enter:
    rm -ir manual
    This command recursively removes the contents of all subdirectories of the manual directory, prompting you regarding the removal of each file, and then removes the manual directory itself, for example:
    You:  rm -ir manual
    System: rm: Select files in directory manual? Enter y for yes.
    You:  y
    System: rm: Select files in directory manual/draft1? Enter y for yes.
    You:  y
    System: rm: Remove manual/draft1?
    You:  y
    System: rm: Remove manual/draft1/chapter1?
    You:  y
    System: rm: Remove manual/draft1/chapter2?
    You:  y
    System: rm: Select files in directory manual/draft2? Enter y for yes.
    You:  y
    System: rm: Remove manual/draft2?
    You:  y
    System: rm: Remove manual?
    You:  y
    Here, the rm command first asks if you want it to search the manual directory. Because the manual directory contains directories, the rm command next asks for permission to search manual/draft1 for files to delete, and then asks if you want it to delete the manual/draft1/chapter1 and manual/draft1/chapter2 files. The rm command next asks for permission to search the manual/draft2 directory. Then asks for permission to delete the manual/draft1, manual/draft2, and manual directories.

    If you deny permission to remove a subdirectory (for example, manual/draft2), the rm command does not remove the manual directory. Instead, you see the message: rm: Directory manual not empty.

Files

Item Description
/usr/bin/rm Contains the rm command.