rm — remove files or directories#
rm command, you can remove (or delete) files
The basic format of the command is:
A safer approach is to add the
-iv options to the
rm -iv source
rm will require your
confirmation before deleting a file or directory.
-v (verbose) will print the command’s actions on
For convenience, you can add an alias.
To follow the examples below, you will need to:
Copy the following files into your home directory using the cp command:
cp -v /usr/share/dict/words ~ cp -rv /usr/share/doc/bash ~
~is a shortcut for home directory.
Create an empty directory:
To remove a file, for example, the
copied above, you can use:
rm -iv words
rm: remove regular file 'words'? y removed 'words'
Removing empty directories#
If the directory is empty, you can remove it using the
rm -d empty-dir
Alternatively, you can use the rmdir command:
Removing directories with content#
If the directory has some content i.e., files or
subdirectories, you will need to add the
For example, using the
bash directory copied
rm -ivr bash
This command will ask for your confirmation for deleting every file in the directory and then delete it:
rm: descend into directory 'bash'? y rm: remove regular file 'bash/RBASH'? y removed 'bash/RBASH' ... rm: remove regular file 'bash/README.gz'? y removed 'bash/README.gz' rm: remove directory 'bash'? y removed directory 'bash'
-i, you could use the
which will only prompt once, when removing directories
rm -Ivr bash
rm: remove 1 argument recursively? y removed 'bash/RBASH' ... removed 'bash/README.gz' removed directory 'bash'
If you are completely sure you do not need the
directory and its contents, you can force its removal
rm -vrf bash
rm will delete the directory without confirmation.
Adding an alias for
Rather than typing
rm -iv, every time you need to use
the command, you can add an alias
for the command in your
alias rm='rm -iv'
Now, when you type
rm, you will actually be running