Removing files and directories: rm

Removing files

To remove a file use the command:

   rm [option] filename

You cannot remove a file in another user's account unless they have set access permissions for the file which allow you to.

Use the -i (interactive) option which makes the command prompt you for confirmation that you want to remove each file.

**Caution**

To avoid inadvertantly deleting a file, always use the rm command together with its -i option.

   rm -i filename

This will prompt you for confirmation that you want to remove the file. You can make this the default action for the rm command by creating a command alias that uses this option.

Examples

To remove a single file:

   rm help.txt

This removes the file help.txt from the current directory.

To remove several files:

   rm artwork/figure[2-7]

This removes files figure2 through to figure7 from the subdirectory artwork.

To remove a file which you don't have write permission for:

   rm tmp/intro.txt
   rm: override protection 444 for intro.txt? y

This removes the file intro.txt from the subdirectory tmp. Because you don't have write permission for this file you are asked to confirm that you want to remove it. Entering a y removes the file, any other response leaves the file alone.

To remove files interactively:

   rm -i *.fm

This will prompt you to confirm that you want to remove a file from the current directory whenever that file has the suffix .fm.

Answering y will delete the file. The file is not deleted if any other response is given.

Removing files that have links

Remove a file that has hard links to it the same way as you would remove any other file:

   rm filename(s)

Any files that were created by making hard links to the filename will continue to exist in their own right.

Removing a file to which symbolic links have been made has a different effect. Although the linkname remains, the actual file that this name points to does not.

Trying to use a command on the linkname will give you the message:

   command: filename: No such file or directory


Removing directories

To remove a directory containing files, use the command:

   rm -r directory_name

This deletes all the contents of the directory including any subdirectories.

The rmdir command can be used to remove empty directories.

**Caution**

To avoid inadvertantly removing a directory, always use the rm command together with the -i option.

   rm -ir directory_name

Examples

To remove a directory containing files and subdirectories:

   rm -r projectX

This deletes the directory projectX and any files or subdirectories that it holds.

To remove a directory interactively:

   rm -ir plan9

This will prompt you for confirmation before removing each subdirectory, any files and the directory plan9 itself.


Removing directories created with the ln command

To remove a directory that is a symbolic link that points to another directory use the command:

   rm directory_name

Notice that the directory does not need to be empty and you do not have to use the rmdir command. You are not really removing a directory, just a named symbolic link that points to the "real" directory.


Removing linked directories that contain links

What happens if you remove a directory that is a symbolic link to another directory and in which you have created links to other files and/or directories?

Remember that the directory is just a linkname pointing to the "real" directory; so it is in this directory that the linknames of files and/or directories exist.

When you remove this type of directory, all that happens is that the linkname of the directory is removed. Any links you have made to the directory remain in the directory that this linkname pointed to.

Look at this example:

   pwd
       (print the user's working directory)
   /usr/tom/reports
   ls ../tmp/drafts
       (list the contents of this directory)
   expenditure targets
   ln -s $HOME/tmp/drafts quarterly
       (use a symbolic link to make the directory)
   cd quarterly
       (change directory to this linkname)
   ln $HOME/accounts/expenses
       (link a file to this linkname)
   ls
       (list the contents of the linked directory)
       (the linked file is listed)
   expenditure expenses targets
   cd ..; rm quarterly
       (go to the parent directory and remove the linkname)
   ls ../tmp/drafts
       (list the directory the linkname pointed to)
   expenditure expenses targets
       (the linked file is still there)


Problems

rm filename: No such file or directory
The file or directory you are trying to copy does not exist.
usage: rm [-rif] file ...
You have used a non-existent option or you have failed to give a filename or directory_name.
rm: override protection 600 for filename? y
rm: filename not removed: Permission denied
You don't have permission to remove this file or directory. Most likely you're trying to erase a system file or another user's file.